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 />"; }

 

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>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.