0

Using the following code:

if (isset($_POST['delete']) && isset($_POST['id']))
{
    $first = get_post('first');
    $query = "DELETE FROM user_master WHERE id='$id'";
    if(!mysql_query($query, $db_server))
        echo "DELETE failed: $query<br />" . 
        mysql_error() . "<br /><br />";
}

$query="SELECT * FROM user_master";
$result= mysql_query($query);

if(!result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

for ($j=0 ; $j<$rows ; ++$j)
{
    $row = mysql_fetch_row($result);
    echo <<<_END
    <pre>
    ID      $row[0]
    First   $row[1]
    Last    $row[2]
    Email   $row[3]
    User    $row[4]
    </pre>
    <form action="willingLog.html" method="post">
    <input type="hidden" name="delete" value="yes" />
    <input type="hidden" name="id" value="$row[0]" />
    <input type="submit" value="DELETE RECORD" /></form>
_END;

I'm unable to delete a record form the table.

All the records form the table are printing on the screen, including the "DELETE RECORD" button. When I hit the button, however, nothing happens. I've checked the actual table on phpmyadmin, and the table is unaffected as well.

I'm pulling this stuff from a book, so I don't understand why it's not working.

12
  • 2
    I'd suggest getting a better book. :) From the looks of it the one you're using was published years ago. Commented Jun 26, 2012 at 16:55
  • 2
    There is no delete sql in this example Commented Jun 26, 2012 at 16:55
  • I don't see any code anywhere that comes even close to doing what you want. $_POST? DELETE FROM? Where are they? Commented Jun 26, 2012 at 16:55
  • What does your delete button do? Right now it seems the form sends the user to willingLog.html... In order to delete the actual row, this page would have to process the filled in form and run a mysql delete query. Commented Jun 26, 2012 at 16:56
  • 1
    What does this code do $first = get_post('first');? Is it setting a variable? Add some debugging and echo out your SQL statement to make sure it looks like what you think it looks like. Commented Jun 26, 2012 at 16:59

5 Answers 5

2

I do not see where you set the $id variable.

if (isset($_POST['delete']) && isset($_POST['id']))
{
    //$first = get_post('first');   #not sure what this does
    $id = sanitize($_POST['id'], 'int');  //protect from sql injection
    $query = "DELETE FROM user_master WHERE id=$id";
    if(!mysql_query($query, $db_server))
        echo "DELETE failed: $query<br />" . 
        mysql_error() . "<br /><br />";
}

Please be sure to sanitize any data sent across the network, i.e. $_POST. Also, if id is of a number type, do not surround the value in quotes or mysql will interpret it as a string.

Lastly, look into using MySqli or PDO, as MySql API has been deprecated.

Sign up to request clarification or add additional context in comments.

Comments

1

Here i defined a variable from that variable i delete the values

$id = 2;

    $query = mysql_query("DELETE FROM table_name WHERE id="'.$id.'"");

note: Delete query id is important otherwise it delete whole rows defaulty.

Comments

1

$query = mysql_query("DELETE FROM table_name WHERE id=1");

Comments

0
$query = "DELETE FROM user_master WHERE id='$id'";

This should probably be:

$query = "DELETE FROM user_master WHERE id='" . $_POST['id'] "'";

But please do some research into SQL injection, and why you should never pass information directly from the user into the database.

2 Comments

That's not a very good example if you are warning him about sql injection lol.
Oh, I don't know. I think it's an excellent example; just possibly not in the way he'd like.
0

code should read:

$id = get_post('id');

in the first line of the curly braces under the first "if" statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.