By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
461,995 Members | 484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 461,995 IT Pros & Developers. It's quick & easy.

Delete MySql Rows Problem

imarkdesigns
P: 46
Hello everyone.. a newbie programmer here wants to ask why is my codes for deleting rows from my databse won't work.... seems that this codes spread to the net and no one refuse to correct it...

can someone correct this...

#-------

<!-- Editable Area -->
<?php

include ("../dbconnection/connect.php");

$tbl_name="schedule"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>

<?php
while($rows=mysql_fetch_array($result)){
?>
<form name="form1" action="" method="post" enctype="multipart/form-data">
<table id="alter_rows" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="admincheckbox"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"> </td>
<td class="admindate"> <? echo $rows['date']; ?> </td>
<td class="admintime"> <? echo $rows['id']; ?> </td>
<td class="adminevents"> <? echo $rows['events']; ?> </td>
<td class="adminvenue"> <? echo $rows['venue']; ?> </td>
<td class="adminlocations"> <? echo $rows['locations']; ?> </td>
</tr>

</table>
<?php
}
?>
<!-- Editable Area -->
<table cellpadding="0" cellspacing="0">
<tr>
<td><input name="delete" id="delete" type="submit" value="Delete Schedule" /></td>
</tr>
</table>
<?
// Check if delete button active, start this

//$delete = $_REQUEST['delete'];
//
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM schedule WHERE id='$del_id' LIMIT 1";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=deleterow.php\">";
}
}
mysql_close();
?>
</form>


------#

when a DELETE button hits nothing happens...
Jul 25 '07 #1
Share this Question
Share on Google+
6 Replies

10K+
P: 13,262
Hello everyone.. a newbie programmer here wants to ask why is my codes for deleting rows from my databse won't work.... seems that this codes spread to the net and no one refuse to correct it...

can someone correct this...

#-------

<!-- Editable Area -->
<?php

include ("../dbconnection/connect.php");

$tbl_name="schedule"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>

<?php
while($rows=mysql_fetch_array($result)){
?>
<form name="form1" action="" method="post" enctype="multipart/form-data">
<table id="alter_rows" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="admincheckbox"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"> </td>
<td class="admindate"> <? echo $rows['date']; ?> </td>
<td class="admintime"> <? echo $rows['id']; ?> </td>
<td class="adminevents"> <? echo $rows['events']; ?> </td>
<td class="adminvenue"> <? echo $rows['venue']; ?> </td>
<td class="adminlocations"> <? echo $rows['locations']; ?> </td>
</tr>

</table>
<?php
}
?>
<!-- Editable Area -->
<table cellpadding="0" cellspacing="0">
<tr>
<td><input name="delete" id="delete" type="submit" value="Delete Schedule" /></td>
</tr>
</table>
<?
// Check if delete button active, start this

//$delete = $_REQUEST['delete'];
//
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM schedule WHERE id='$del_id' LIMIT 1";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=deleterow.php\">";
}
}
mysql_close();
?>
</form>


------#

when a DELETE button hits nothing happens...
I'm going to have to copy this to the PHP forum as well.
Jul 25 '07 #2

dafodil
100+
P: 392
Please check if this variable exists in the file you included:
$db_name

I suggest you to use this after you delete instead of detecting $result.
Expand|Select|Wrap|Line Numbers
  1.  
  2. if((mysql_affected_rows()!=0)&&(mysql_affected_rows()!=-1)){
  3. //your statements goes here
  4. echo "delete successful!"
  5.  
  6. }
  7.  
Jul 25 '07 #3

imarkdesigns
P: 46
Please check if this variable exists in the file you included:
$db_name

I suggest you to use this after you delete instead of detecting $result.
Expand|Select|Wrap|Line Numbers
  1.  
  2. if((mysql_affected_rows()!=0)&&(mysql_affected_rows()!=-1)){
  3. //your statements goes here
  4. echo "delete successful!"
  5.  
  6. }
  7.  

looks working on the live site... but not in localhost...??

um, is there any problem in my localhost or something?
[i need more rice to eat before i totally understand this LOL!]

.. and yes i include the db_name...
Jul 25 '07 #4

mwasif
Expert 100+
P: 801
You have 2 problems with the code
  1. Wrong positioning of <form> tag. It should be outside while() and should not contain enctype="multipart/form-data" if you not uploading a file e.g.
    [PHP]<form name="form1" action="" method="post">
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <table id="alter_rows" align="center" cellpadding="0" cellspacing="0">[/PHP]
  2. Change [PHP]if($delete)[/PHP] to [PHP]if(isset($_POST["delete"]))[/PHP]. I am sure live server has register_globals on.
Jul 25 '07 #5

imarkdesigns
P: 46
yeah i guess...

but in the live site it is working... and in my local server where i use xampp, its not working...

well, it seems ok in the live other than local server... thanks for the help guys! you rock all !!
Jul 26 '07 #6

dafodil
100+
P: 392
yeah i guess...

but in the live site it is working... and in my local server where i use xampp, its not working...

well, it seems ok in the live other than local server... thanks for the help guys! you rock all !!
I think you need to configure something in your local server.
Post again if you have problems.
Jul 27 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.