deleting multiple records from MYSQL database using PHP

05-08-06, 02:10 AM
|
|
Wannabe Coder
|
|
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
deleting multiple records from MYSQL database using PHP
Hi!
I am trying to delete at a time multiple recodes stored in MySQL database using PHP script. the problem first i track the chosen rows using checkboxes and track those records using JavaScript and passing those to a php script, first split these values and stored each of them in each array keys and using foreach statement to delete selected rows from database generating Sql statement.
but the problem is, when i split the values which javascript passing and storing each of these split values in each key of an array. The last array key remains empty and due to that the foreach statement doesn't completed as it found a black array key with no value.
can any one please advice any solution. OR how do I delete last blank array key so that others will work fine.
OR any other idea highly appreciated.
If any one wants to see the code please let me know.
Thanks in advance.
|

05-09-06, 08:16 PM
|
|
Community Liaison
|
|
Join Date: Jun 2003
Location: Australia
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Use array_pop() to delete the last key/value pair in the array.
PHP Code:
array_pop($array);
http://www.php.net/array_pop
__________________
Chris (aka Skeleton Man)
ProgrammingTalk's Perl/PHP Moderator !
|

05-10-06, 09:18 AM
|
|
Wannabe Coder
|
|
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks!
the code has deleted the last array element, but still i can't able to delete single row or multiple row value in the database.
if you want i can show you the code which i am using.
thanks once more for your time and help.
|

05-10-06, 09:28 AM
|
 |
Newbie Coder
|
|
Join Date: Apr 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the best way to accomplish this could be dynamic array of checkboxes in php and then using foreach loop for deleting more than one entry, anyhow, please show us some code.
|

05-10-06, 11:58 PM
|
|
Wannabe Coder
|
|
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks!
here is the code:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<script language="javascript">
function loud1(basenm, min, max)
{
var mt1 = new Array;
fr1.data.value="";
var e, picked = 0;
for(var i = min; i < max; i++)
{
if (document.fr1.elements[basenm + i].checked==true)
{
mt1[i] = document.fr1.elements[basenm + i].value;
//confirm(mt1[i]);
e = document.fr1.elements[basenm + i]
picked++;
fr1.data.value = fr1.data.value + "\"" + e.value + "\";";
}
}
fr1.data.value = fr1.data.value;
}
function setCheckboxesRange(the_form, do_check, basename, min, max)
{
for (var i = min; i < max; i++)
{
if (typeof(document.forms[the_form].elements[basename + i]) != 'undefined')
{
document.forms[the_form].elements[basename + i].checked = do_check;
}
if (typeof(document.forms[the_form].elements[basename + i + 'r']) != 'undefined')
{
document.forms[the_form].elements[basename + i + 'r'].checked = do_check;
}
}
return true;
}
</script>
</head>
<body text="#333333" link="#333333" vlink="#333333" alink="#333333">
<?php
$next = $HTTP_SERVER_VARS['QUERY_STRING'];
$mode1 = isset($HTTP_GET_VARS['user']);
$filename = $HTTP_GET_VARS["next"];
$filename1 = $HTTP_GET_VARS["next1"];
$link = mysql_connect('xxxx', 'xxxx', 'xxxx');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('xxxxx');
if (empty($HTTP_GET_VARS["next"]))
{
global $data;
$sql = ("SELECT * FROM userreg LIMIT 0 , 30");
$result = mysql_query($sql);
$num = mysql_numrows($result);
echo "<form name=\"fr1\" action=\"deldata.php?next=deletedata&next1=$data\" method=\"POST\" onsubmit=\"javascript:loud1('deldata', 1, ".$num.");\"><table>";
for($i=1; $i<$num+1; $i++)
{
$row = mysql_fetch_array($result);
echo("<tr bgcolor=\"#ccffee\"><td><input type=\"checkbox\" name=\"deldata".$i."\" value=\"DELETE FROM `userreg` WHERE `username` = '".$row['username']."' LIMIT 1\">".$row['username']."</td><td>".$row['password']."<br></td></tr>");
}
echo "<a href=\"#\" onclick=\"setCheckboxesRange('fr1', true, 'deldata', 0, ".$i."); return false;\">Check All</a> <a href=\"#\" onclick=\"setCheckboxesRange('fr1', false, 'deldata', 0, ".$i."); return false;\">Uncheck All</a>";
echo "<tr><td> </td><td> </td></tr><tr><td align=right>";
echo "<input type=hidden name=\"data\" value=\"\">";
echo "<input type=hidden name=action value=3>";
echo "<input type=\"submit\" name=\"deletedata\" value=\"DELETE\">";
echo "</td>";
echo "</tr>";
echo "</table></form><br><br>";
}
if ($action == "3")
{
$picks = urldecode(stripslashes($data));
$data1=split(";",$picks);
array_pop($data1);
foreach ($data1 as $k => $v )
{
echo "row to be deleted: (".$data1[$k].") <br><br>";
$sql = ($data1[$k]);
$result = mysql_query($sql);
}
$sql1 = ("SELECT * FROM userreg LIMIT 0 , 30");
$result1 = mysql_query($sql1);
$num = mysql_numrows($result1);
echo "<table>";
for($i=0; $i<$num; $i++)
{
$row = mysql_fetch_array($result1);
echo("<tr bgcolor=\"#ccffee\"><td><input type=\"checkbox\" name=\"deldata\" value=\"".$row['username']."\">".$row['username']."</td><td>".$row['password']."<br></td></tr>");
}
echo "</table>";
}
?>
</body>
</html>
hope this will help you to understand.
|

06-04-09, 03:07 AM
|
|
New Member
|
|
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi im trying to insert multiple records(which consists of many rows) into database..
But, only the first row is inserted meanwhile other rows do not..
can anyone please help me..
i will provide my coding here~
//this ia add.php
PHP Code:
<HTML> <HEAD> <TITLE> Add/Remove dynamic rows in HTML table </TITLE> <SCRIPT language="javascript"> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var iteration = rowCount; var cell1 = row.insertCell(0); var element1 = document.createElement("input"); element1.type = "checkbox"; cell1.appendChild(element1); <?php $sql_tag= "SELECT * FROM cattagdesc"; $rkd = query($sql_tag); $total = sqlCount($row); $loop = 0; ?> var cell2 = row.insertCell(1); var element2 = document.createElement("select"); <?php while($record = fetchArray($rkd)) { ?> element2.options[<?php echo $loop; ?>] = new Option('<?php echo $record['tagID'] ?>', '<?php echo $record['tagID'] ?>'); <?php $loop++; } ?> cell2.appendChild(element2); var cell3 = row.insertCell(2); var element3 = document.createElement("input"); element3.type = "text"; cell3.appendChild(element3); var cell4 = row.insertCell(3); var element4 = document.createElement("input"); element4.type = "text"; cell4.appendChild(element4);
var cell5 = row.insertCell(4); var element5 = document.createElement("input"); element5.type = "text"; cell5.appendChild(element5); var cell6 = row.insertCell(5); var element6 = document.createElement("input"); element6.type = "text"; cell6.appendChild(element6); } function deleteRow(tableID) { // grab the element again! var table = document.getElementById(tableID); // grab the length! var lastRow = table.rows.length; // delete the last row if there is more than one row! if (lastRow > 2) table.deleteRow(lastRow - 1); } </SCRIPT> </HEAD> <?php
$sql_tag= "SELECT * FROM cattagdesc"; $rkd = query($sql_tag); $total = sqlCount($row);
?>
<?php
if(isset($_POST["button"])&&$_POST["button"]=="Submit") { include("insert.php"); //include("view1.php"); } else{
?> <body>ADDITION FORM <form method="post" enctype="multipart/form-data">
<TABLE id="dataTable" width="350px" border="1"> <tr> <th></th> <th>Tag Id</th> <th>IND1</th> <th>IND2</th> <th>DETAILS</th> <th>DESCRIPTION</th> </tr> <TR> <TD><INPUT type="checkbox" name="chk"/></TD> <TD> <select name="tagID" id="tagID"> <option value=""> </option> <?php while($record = fetchArray($rkd)) { ?> <option value="<?php echo $record['tagID']; ?>"<?php if ($_POST['tagID'] == $record['tagID']) echo "selected" ?>><?php echo $record['tagID']; ?></option> <?php } ?> </select> </TD> <TD><INPUT type="text" name="ind1"/></TD> <TD><INPUT type="text" name="ind2"/></TD> <TD><INPUT type="text" name="detail"/></TD> <TD><INPUT type="text" name="name"/></TD> </TR> <tr> <br> <br> <INPUT type="button" value="Add Row" onClick="addRow('dataTable')" /> <INPUT type="button" value="Delete Row" onClick="deleteRow('dataTable')" /> </TABLE> <br> <input type="submit" onClick="if(check())return true; else return false;" name="button" value="Submit" /> <input type="reset" name="reset" value="Reset" onClick="if(confirm('Reset all data?'))return true;else return false;" /> <input type="button" name="cancel" value="Cancel" onClick="if(confirm('Back to View uitmlib?'))location='index1.php?page=view1';else return false;" />
</form> <?php } ?></body> </HTML>
//this is insert.php
PHP Code:
<?php
$sql1= "insert into catbiborgdesc(bib_name) values('')"; insertData($sql1,$dbName); $sql2 = "SELECT MAX(bibID) AS max_value FROM catbiborgdesc"; $rkd = viewRecord($sql2,$dbName); $next_bib_id = $rkd->max_value; $sql3= "select tagID from cattagdesc WHERE tagID='".$_POST["tagID"]."'"; if($result = mysql_query($sql3,$db)) { if(mysql_num_rows($result)>0) { if ($_POST["tagID"]=='100'){ // INSERT INTO TABLE AUTHOR $sql4 ="insert into catauthortag(bibID,tagID,ind1,ind2,detail,name) values ($next_bib_id,'".$_POST["tagID"]."','".$_POST["ind1"]."','".$_POST["ind2"]."','".$_POST["detail"]."','".$_POST["name"]."')"; insertData($sql4,$dbName); }else if($_POST["tagID"]=='245'){ // INSERT INTO TABLE TITLE $sql4 ="insert into cattitletag(bibID,tagID,ind1,ind2,detail,name) values ($next_bib_id,".$_POST["tagID"].",'".$_POST["ind1"]."','".$_POST["ind2"]."','".$_POST["detail"]."','".$_POST["name"]."')"; insertData($sql4,$dbName); }else if($_POST["tagID"]=='020'){ // INSERT INTO TABLE ISBN $sql4 ="insert into catisbntag(bibID,tagID,ind1,ind2,detail,name) values ($next_bib_id,'".$_POST["tagID"]."','".$_POST["ind1"]."','".$_POST["ind2"]."','".$_POST["detail"]."','".$_POST["name"]."')"; insertData($sql4,$dbName); } } } ?>
Last edited by Nico; 06-04-09 at 08:47 AM.
Reason: Wrappers.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|