Generating a random result & updating it using buttons...
Posted: Thu Mar 03, 2011 8:02 am
Hi
I generate a random result from a MySQL database, but I need a button that updates the results so it is not displayed again. My problem lies with the UPDATE command. How do I update the id that was generated to change 'used' to 'yes'?
Table structure is a simple 'id, riskid, used' structure
Unfortunately I know incredibly little about programming and I am not sure how to complete the php code to work. Help would be very much appreciated!
Code snippets below:
I generate a random result from a MySQL database, but I need a button that updates the results so it is not displayed again. My problem lies with the UPDATE command. How do I update the id that was generated to change 'used' to 'yes'?
Table structure is a simple 'id, riskid, used' structure
Unfortunately I know incredibly little about programming and I am not sure how to complete the php code to work. Help would be very much appreciated!
Code snippets below:
Code: Select all
<?php
include 'includes/db_write.php';
$conn = @mysql_connect($db_host,$db_usr,$db_pass);
if (!$conn) {
echo( "<P>Unable to connect to the database server (".$db_host.") at this time.</P>" );
exit();
}
@mysql_select_db($db_name, $conn);
if (! @mysql_select_db($db_name) ) {
echo( "<P>Unable to locate the ".$db_name." database at this time.</P>" );
exit();
}
$sql = "SELECT * FROM idgen where used='no' ORDER BY RAND() limit 1";
$result = mysql_query($sql);
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
?>
<table align="center" class="table2" style="width:300px">
<tr><td class="td1">Risk ID</td></tr>
<tr>
<?php
while ( $row = mysql_fetch_array($result) ) {
$msg .= "<td class='td2'>".$row['riskid']."</td>\r\n";
$msg .= "</tr>\r\n";
}
$msg .= "</table>\r\n";
echo "<p>". $msg ."</p>";
?>
<br>
<table width="800" border="0" align="center">
<tr><td align="center"><form action="index.php" method="post"><input type="submit" value="Use This ID" />
<?php
include 'includes/db_write.php';
$conn = @mysql_connect($db_host,$db_usr,$db_pass);
if (!$conn) {
echo( "<P>Unable to connect to the database server (".$db_host.") at this time.</P>" );
exit();
}
@mysql_select_db($db_name, $conn);
if (! @mysql_select_db($db_name) ) {
echo( "<P>Unable to locate the ".$db_name." database at this time.</P>" );
exit();
}
$sql = "UPDATE `idgen` SET `used` = 'yes' WHERE `riskid` = ??????????"; // <-------- How do I complete this? And how do I make sure it only executes the update if the button is pressed?
$result = mysql_query($sql,$conn) or die("Could not execute sql!");
?>
</form></td></tr>
</table>
<table width="800" border="0" align="center">
<tr><td align="center"><form><input type=button value="Generate New ID" onClick="window.location.reload()"></form></td></tr>
</table>