digitalELP Posted April 25, 2008 Share Posted April 25, 2008 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 />"; } Quote Link to comment https://forums.phpfreaks.com/topic/102902-solved-using-html-buttons-to-update-sql-server/ Share on other sites More sharing options...
dptr1988 Posted April 25, 2008 Share Posted April 25, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/102902-solved-using-html-buttons-to-update-sql-server/#findComment-527262 Share on other sites More sharing options...
GingerRobot Posted April 25, 2008 Share Posted April 25, 2008 If you wanted to do this 'behind the scenes' i.e. without a page reload, you'd need to use some AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/102902-solved-using-html-buttons-to-update-sql-server/#findComment-527270 Share on other sites More sharing options...
digitalELP Posted April 25, 2008 Author Share Posted April 25, 2008 Thank you so much! This is exactly what I was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/102902-solved-using-html-buttons-to-update-sql-server/#findComment-527442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.