Current location: Hot Scripts Forums » Programming Languages » PHP » deleting multiple records from MYSQL database using PHP

deleting multiple records from MYSQL database using PHP

Reply
  #1 (permalink)  
Old 05-08-06, 02:10 AM
sujata_ghosh sujata_ghosh is offline
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-09-06, 08:16 PM
Skeleton Man Skeleton Man is offline
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 !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-10-06, 09:18 AM
sujata_ghosh sujata_ghosh is offline
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-10-06, 09:28 AM
Grabber's Avatar
Grabber Grabber is offline
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-10-06, 11:58 PM
sujata_ghosh sujata_ghosh is offline
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> &nbsp; &nbsp; <a href=\"#\" onclick=\"setCheckboxesRange('fr1', false, 'deldata', 0, ".$i."); return false;\">Uncheck All</a>";
		
echo "<tr><td>&nbsp;</td><td>&nbsp;</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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-04-09, 03:07 AM
love_choc love_choc is offline
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="">&nbsp;</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" />&nbsp;
<input type="reset" name="reset" value="Reset" onClick="if(confirm('Reset all data?'))return true;else return false;" />&nbsp;
<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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php Mysql Bug??? tranquilraven PHP 4 03-01-06 03:06 AM
Need help with some php mysql TheTinkeringToad PHP 9 02-01-06 10:56 AM
Clear my misunderstanding: 1 MySQL database for many programs? iKwak PHP 3 08-21-05 12:50 PM
Deleting data from a database using php hsob PHP 1 02-04-04 01:58 PM
Importing to a mysql database via php gulfan PHP 1 01-23-04 04:44 PM


All times are GMT -5. The time now is 10:27 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.