Forum Moderators: coopster

Message Too Old, No Replies

problem with deleting rows from a mysql table

PHP , MySQL, DELETE

         

naiquevin

1:23 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



What would be the syntax if I want to delete a particular row from a table if the criteria is passed from the earlier page as a hidden form field

as in,

$contact_name = $_POST["contact_name"]; //contact_name was passed as a hidden form field when this file was targeted

$query = "DELETE FROM table_name WHERE Name='$contact_name'";

$result = mysqli_query($con,$query);

if(!$result)
{
die('error deleting from the database:' . mysqli_error());
}

else echo " 1 contact deleted";

I am using the above code and nothing is happening. Is the syntax for the bold part correct ?>

blang

1:38 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



There's no error returned?

Are you certain the form element name is "contact_name"? If you view the HTML source, is there a value for that element?

Does the `Name` field exist in the database? Or is it `name`?

Does the value you're passing to the script in the POST element exist? i.e. you type "Bob", and there is a record with a `Name` field value of "Bob"?

piznac

1:43 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



Do you have a unqID for the contacts? It would be better to delete off that.

You can echo back the $contact_name and see what it returns,. that might help to see where the problem may be.

blang

2:23 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



I agree with piznac, it would be a better idea to match on a unique ID value (assuming your `Name` column is not the PRIMARY KEY or set UNIQUE). Typically this means an INT type column.

Something else to consider if that form is public; I can create a script to automatically test against a rainbow table of names, POSTing to your server as much as I like and trying to destroy all records in your database. The same thing could be said for a numeric index form - heck, I'd start at 0 and go to 100000 in no time! Hopefully this is in a protected admin section somewhere.

naiquevin

2:23 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



no error is returned.. i am certain about everything else

Here is the scenario

I have a page that displays data from a MySQL table into HTML table using a while loop..
The last column of the HTML table contains a delete button.. ie each row has a delete button to delete that particular record..
here the hidden form field is passed as follows
echo "<td><form action=....> <input type='submit'.../> <input type='hidden' name='contact_name' value='<?php echo $row[Name]; ?>'</form> </td>";

this form targets a page that asks for confirmation

On confirm page there are two form fields - yes button to delete and cancel button to return to homepage (which is working properly)

The yes button targets delete.php and it opens a page.. the css and everything is loaded but the record is not deleted..no error is displayed..

At the same time I am also passing user variable selected from a drop down list on the very first page as a hidden form field.. as in $_POST["user"] in all the subsequent pages..

There is a diffeent table for each user and in delete.php, if and else if statements are used to work with specific user tables
if($_POST["user"]=='knownuser')
{
}

else if ($_POST["user"]=='knownuser2')
{
}

I know its all messed up... thanks for bearing with me

[edited by: naiquevin at 2:27 pm (utc) on Mar. 12, 2009]

naiquevin

2:26 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



yes 'Name' is a PRIMARY KEY in the table .. is this causing the problem?

Jsyvanne

2:47 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



Double check that the Post parameter is passed to the query. echo the query before executing it and exit the script there, for example. I think there wouldn't be any error message if for some reason the $contact_name would be empty. Just 0 rows affected.

blang

3:38 pm on Mar 12, 2009 (gmt 0)

10+ Year Member



this form targets a page that asks for confirmation

Wait..what? You have this form target an intermediate script? Well then the POST values are lost, unless you're using a session. As soon as you POST to the intermediary script, the value is gone.

You can confirm this by placing this code at the top of each receiving script:

print_r($_POST);

Additionally, make sure you look at the actual HTML source in your browser, not the HTML in your PHP script.

naiquevin

5:11 am on Mar 13, 2009 (gmt 0)

10+ Year Member



I have been using hidden form field method to carry forward the user information all this while.. And now as the application is building up its starting to get a bit confusing .. I guess I ll learn abt PHP sessions first and then get back to the script..

And how can I show a warning message in a dialogue box and eliminate the intermediate confirmation page /? Can it be done using PHP?