Jump to content

[SOLVED] Using HTML Buttons to update SQL Server


digitalELP

Recommended Posts

Hi all,

 

I'm trying to figure out how to go about this task. I have a database table with multiple news items, which I would like to read out on a page, and then have a "Show/Hide" button next to each. Show and hide should update an expiration date field for that record (ie. clicking show would update the field to 0, and hide would update the field to 1). The only problem is, I have no idea how to go about tying in the UPDATE statement to the buttons.

 

This is what I have so far..

 

$query="SELECT * FROM news ORDER BY news.newsID DESC";
$result=mysql_query($query) or die ("ERROR! ".mysql_error());
                
while($Row=mysql_fetch_array($result)){
echo "<tr><td>".$Row['title']."</td>
<td> <span class=\"h4\">".date('F d\, Y', $Row['postDate'])."</span></td>
<td><input type=\"Submit\" name=\"show\" value=\"Show\"></td>
<td><input type=\"Submit\" name=\"hide\" value=\"Hide\"></td></tr><br />"; }

 

Link to comment
Share on other sites

You need to include the SQL ID code for that news item. It would be a lot easier of you didn't have to user buttons ( ie HTML forms ).

 

Here is an example that just uses links. You will need to modifiy it to include the name of your row id field and your URLS.

<?php

if (isset($_GET['show']))
{
$id = 0 + $_GET['show'];
$query = "UPDATE news SET expiration_date_field = 0 WHERE id = " . $id;
mysql_query($query);
}
elseif(isset($_GET['hide']))
{
  $id = 0 + $_GET['hide'];
  $query = "UPDATE news SET expiration_date_field = 1 WHERE id = " . $id;
  mysql_query($query);
} 

$query="SELECT * FROM news ORDER BY news.newsID DESC";
$result=mysql_query($query) or die ("ERROR! ".mysql_error());
                
while($Row=mysql_fetch_array($result))
{
echo "<tr><td>".$Row['title']."</td>" . 
  "<td> <span class=\"h4\">" . date('F d\, Y', $Row['postDate']).  "</span></td>" .
  "<a href='your_page.php?show=" . $Row['id'] . "'>Show</a><a href='your_page.php?hide=" . $Row['id'] . "'>Hide</a>";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.