简体   繁体   中英

Update within mysql query results

hoping you can help with what I expected to be a simple function.

I'd like to allow users to remove (ie change published=y to published=n) a query result but can't for the life of me figure out how to do it. Here's the query as is:

$result = mysql_query("SELECT * FROM discussion WHERE publish='y' ORDER BY timestamp DESC");
while($row = mysql_fetch_array($result))
{
    echo ("<tr><td>" . $row['name'] . "<span class=\"remove\">(<a href=\"#\">REMOVE</a>)</span></td>
    <td width=\"300\">" . nl2br($row['question']) . "</td>
    <td>" . $row['author'] . "</td>
    <td>" . $row['timestamp'] . "</td>
    <td><a href=\"discussion.php?discussionID=" . $row['discussionID'] . "\">View Discussion</a></td></tr>");
}

The query I'd like to run when the user clicked 'REMOVE' (within the above query):

mysql_query("UPDATE discussion SET publish='n' WHERE discussionID='XXXXX");

Any ideas from the fine people at SO?

New to php/mysql so forgive the ignorance.

the same way you do a link to go to the discussion ( a href=\\"discussion.php?discussionID=" . $row['discussionID'] . "\\">View Discussion ).

a href=\\"delete_discussion.php?discussionID=" . $row[ 'discussionID' ] . "\\">REMOVE and create the delete_discussion.php file to run your update query

I think the simplest option here would be to insert your remove button into a form.

Try adding this form into your table. I am echoing the discussionID into the checkbox so that you can distinguish between the multiple checkboxes in the table:

<form method="post" action="">
     <input type="checkbox" id="removeCheckbox" name="removeCheckbox" value="<?php echo $row['discussionID']; ?>" />
     <input id="submit" name="submit" value="Submit" type="submit" />
</form>

And then in your PHP, you update the database with the value of the checkbox:

<?php 

if (isset($_POST['removeCheckbox'])) {
    $checkboxValue = $_POST['removeCheckbox'];
    mysql_query("UPDATE discussion SET publish='y' WHERE discussionID='$checkboxValue'");
}

?>

Does that make sense?

EDIT: This should now work! Also updated to include Dan's suggestion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM