BluwAngel Posted September 28, 2011 Share Posted September 28, 2011 hello since im in rush with project i found some already finished program for deleting entries from atble but i cant find out why it wont work i have problem with delete here's the code problem is on line 46 (if($delete){ that row) can you help me to fix it please <?php include "spoj.php"; $db_name="osnovna_skola"; // Database name $tbl_name="nastavnici"; // Table name $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td bgcolor="#FFFFFF"> </td> <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td> </tr> <tr> <td align="center" bgcolor="#FFFFFF">#</td> <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td> <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> <td bgcolor="#FFFFFF"><? echo $rows['id_nastavnik']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['ime_prezime']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td> </tr> <?php } ?> <tr> <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <? // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } // if successful redirect to delete_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">"; } } mysql_close(); ?> </table> </form> </td> </tr> </table> or if you have any other solution that will work for multiple deleting would be ok Quote Link to comment https://forums.phpfreaks.com/topic/248016-delete-problem/ Share on other sites More sharing options...
AyKay47 Posted September 28, 2011 Share Posted September 28, 2011 Where do you establish your mysql server connection? Where do you set a value for $delete? If delete is supposed to be the submit button, which I assume it is, you will want to set $delete = $_POST['delete']; first, then change your if condition to if(isset($delete)) { } Avoid using PHP short tags.. Quote Link to comment https://forums.phpfreaks.com/topic/248016-delete-problem/#findComment-1273485 Share on other sites More sharing options...
BluwAngel Posted September 28, 2011 Author Share Posted September 28, 2011 it is connected in spoj.php but i trid few solutions and non of them working on the other hand i found other code but have minor problem there it works when i work with original files original files can be found here http://www.phpeasystep.com/mysql/8.html but i dont know what do i need to change to work in my database - dont know what to edit i got this "Notice: Use of undefined constant del - assumed 'del' in F:\xampp\htdocs\multipledelete\delete.php on line 12" view.php <?php /* view.php */ $host = "localhost"; $username = "root"; $password = ""; $database = "osnovna_skola"; $connection = mysql_connect($host, $username, $password); mysql_select_db($database, $connection) or die("MysQL Error"); $sql = mysql_query("select * from nastavnici"); ?> <html><body> <form action="delete.php" method="POST"> <?php echo "<table border=1>"; echo "<tr><th> </th><th>name</th><th>position</th><th>level</th></tr>"; while ($result = mysql_fetch_array($sql)) { echo "<tr><td><input type=checkbox name=del[] id=del value=$result[id_nastavnik]></td><td>$result[ime_prezime] </td></tr>"; } ?> </table> <input type="submit" name="justdel" value="Delete !!" id="justdel"> </form> </body></html> delete.php <?php /* delete.php */ $host = "localhost"; $username = "root"; $password = ""; $database = "osnovna_skola"; $connection = mysql_connect($host, $username, $password); mysql_select_db($database, $connection) or die("MysQL Error"); $id_nastavnik = $_POST[del]; $count = count($id_nastavnik); if($_POST['justdel']) { for ($i=0; $i<$count; $i++) { $sql = mysql_query("delete from tabledelete where id=$id_nastavnik[$i]"); } if ($sql) { print "Record successfully deleted<br>"; print "<a href=view.php>Click here to go back</a>"; } } ?> ps i have same eror with his files but on his it works - entries deleted Quote Link to comment https://forums.phpfreaks.com/topic/248016-delete-problem/#findComment-1273491 Share on other sites More sharing options...
AyKay47 Posted September 28, 2011 Share Posted September 28, 2011 the notice will not affect your code in this case.. your index should be $id_nastavnik = $_POST['del']; instead of $id_nastavnik = $_POST[del]; notice that "del" needs to be wrapped in quotes.. however the parser assumes this so it will not affect your code.. no other errors received? try outputting $sql to see if your sql statement is what you expect.. Quote Link to comment https://forums.phpfreaks.com/topic/248016-delete-problem/#findComment-1273497 Share on other sites More sharing options...
BluwAngel Posted September 28, 2011 Author Share Posted September 28, 2011 thanks its solved works great now heres final code if some looking delete.php <?php /* delete.php */ $host = "localhost"; $username = "root"; $password = ""; $database = "osnovna_skola"; $connection = mysql_connect($host, $username, $password); mysql_select_db($database, $connection) or die("MysQL Error"); $id_nastavnik = $_POST['del']; $count = count($id_nastavnik); if($_POST['justdel']) { for ($i=0; $i<$count; $i++) { $sql = mysql_query("delete from nastavnici where id_nastavnik=$id_nastavnik[$i]"); } if ($sql) { print "Record successfully deleted<br>"; print "<a href=view.php>Click here to go back</a>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248016-delete-problem/#findComment-1273500 Share on other sites More sharing options...
AyKay47 Posted September 28, 2011 Share Posted September 28, 2011 nice, please mark as solved, button on button of page Quote Link to comment https://forums.phpfreaks.com/topic/248016-delete-problem/#findComment-1273507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.