0

Sorry if this is a noob question, but I'm still getting up to speed with PHP and can't find an answer to this one.

I have a php script that queries a mySQL table and then builds an HTML table from the results. This all works just fine. As part of that script, I add a <td> to each <tr> that gives the user a chance to delete each specific record from the database, one by one, if they so choose.

To make this work, I have to be able to pass over to the php script the unique identifier of that record, which exists as one of the values. Problem is, I don't know how to pass this value.

Here is my php script that builds the HTML table:

    while ($row = mysql_fetch_array($result)) {
        echo 
        "<tr class=\"datarow\">"  .  
            "<td id=\"id_hdr\">"    . $row['id']    . "</td>" .
            "<td id=\"name_hdr\">"  . $row['name']  . "</td>" .

            "<td id=\"btn_delete\">
                 <form action=\"delete_item.php\">
                     <input type=\"image\" src=\"images/delete.png\">
                 </form>
             </td>" .
         "</tr>";
    }

So, somehow I either need to explicitly pass 'id' along with "delete_item.php" and/or find a way on the php side to capture this value in a variable. If I can accomplish that I'm home free.

EDIT: Trying to implement both suggestions below, but can't quite get there. Here is how I updated my form based on how I read those suggestions:

"<td id='btn_delete'>".
    "<form action='scripts/delete_item.php'>".
        "<img src='images/delete.png'>".
        "<input type='hidden' id='uid' value='" . $row['id']    . "'>".
        "<input type='submit' value='Submit'/>".                            
    "</form>".
"</td>" .

Then, in delete_item.php, I have this:

$id = $_POST['uid'];
$sql = "DELETE FROM myTable WHERE id=$id";
$result = mysql_query($sql);
if (!$result) {
    die("<p>Error removing item: " . mysql_error() . "</p>");
}

But when I run it, I get the error:

Error removing item: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

And one final thing: this approach gives me a button with the word 'submit' directly under my image. I'd prefer not to have this if possible.

Thanks again!

2 Answers 2

2
<form action=\"delete_item.php\">
<input type=\"hidden\" value=\"$row['id']\" name=\"uid\" >
<input type=\"image\" src=\"images/delete.png\">
</form>

The unique id is placed in a hidden input. You can get this value using

$_POST['uid']

But you need to submit the form

<input type=\"submit\" name=\"submit\" value=\"delete\" ">
Sign up to request clarification or add additional context in comments.

Comments

2

You could use an anchor tag with parameter for id. ie, www.example.com/delete.php?id=20

Now you could get that id on page delete.php as $_GET['id']

Using that you could delete the data from the table and return to the required page by setting up header

If you required you could use the same logic with AJAX and with out a page reload you could permenently delete that data. I would recommend AJAX

1 Comment

Thanks. I'm just getting up to speed with HTML, PHP, CSS, and JavaScript. I'm more than a little overwhelmed at the moment, so I'm trying to save AJAX for another day.

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.