Jump to content

problem deleting form mysql database


roondog
 Share

Recommended Posts

here is my form

<html><head><style type="text/css">img{width: 100px;height: 100px;border: 1px solid black;}</style></head><body><?php$con = mysql_connect('localhost', '******', '*****');if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db('roondog_gallery', $con);$result = mysql_query("SELECT * FROM test"); echo "<form action='picdelete.php' method='post'>";while($row = mysql_fetch_array($result)){echo "<img src='$row[picname]'  />";echo "<input type='checkbox' name='remove[]' value='$row[picname]' />";}echo "<input type='submit' value='delete picture'  />";echo "</form>";?></body></html>

and here is my code

<?php$con = mysql_connect('localhost', '******', '******');if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db ('roondog_gallery', $con);if($POST['picname']){mysql_query("DELETE FROM test WHERE picname='".$_POST['remove']."'");}print_r($_POST);mysql_close($con);?>

This should be fairly simple but it just doesn't seem to work.

Link to comment
Share on other sites

You are using $_POST['picname'] and $_POST['remove'] and just $_POST which I assume all refer to the same variable?Why not take the $_POST value right at the start of the picdelete page eg $filename=$_POST['picname'] and use it whenever needed. Its quicker than typing $_POST['xxx'] all the time and makes sure you don't accidentally mistype the $_POST name later down the script.Once you have got $filename, print it out as part of your debugging process so you can see exactly what value it has before trying to do any mysql queries with it.You can also make your mysql query simpler to read by putting the delete variable in braces eg.mysql_query="DELETE FROM test WHERE picname='{$filename}'";

Link to comment
Share on other sites

The form has a list of checkboxes called remove, and the code checks for $_POST['picname'] (which I don't see in the form. If boxes were selected, you can delete them all like this:

if (count($_POST['remove']) > 0){  $ids = join("," $_POST['remove']);  mysql_query("DELETE FROM test WHERE picname IN ({$ids})");}

That's not the most secure way and assumes that each checkbox value is a number, but if you have an array of IDs that will delete them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...