Jump to content

delete problem


BluwAngel

Recommended Posts

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

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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>";
}

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.