PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hello, im done writing a program now i just need to find a way to enter the data through a form. ive been working on it but i just cant figure out whats wrong.
<?php
$db = mysql_connect("localhost", "", "");
mysql_select_db("games",$db);
if (empty($gameName)) {
#put query inside the loop so it doesnt get run if the form has been submitted
$result = mysql_query("SELECT * FROM gamesinfo ORDER BY gameName",$db);
echo "<h1>Edit Game</h1>";
echo "<form method="post">";
echo "<select name="gameName">";
while($row = mysql_fetch_assoc($result)) {
extract($row);
echo "<option value="$gameName">$gameName</option>\n";
}
echo "</select>";
echo "<input type="submit" value="Edit Game">";
}
else
{
$result = mysql_query("SELECT * FROM gamesinfo WHERE gameName = '".$_POST['gameName']."' LIMIT 1");
$sql = mysql_query("UPDATE gamesinfo SET gameName = '$_POST[gamename]' AND minPlayer = '$_POST[minplayer]' AND maxPlayer = '$_POST[maxplayer]' AND prvPrice = '$_POST[prvprice]' AND pubPrice = '$_POST[pubprice]' AND WHERE id = '$id'");
$result = mysql_fetch_assoc($result);
#the game data is now held in the $result variable
$id = $result[id];
$gamename = $result[gameName];
$minplayer = $result[minPlayer];
$maxplayer = $result[maxPlayer];
$prvprice = $result[prvPrice];
$pubprice = $result[pubPrice];
echo "<h1>Below are the record details</h1><br><br>";
echo "<form method="post">";
echo "
<table border="0"><tr>
<td>ID:</td><td><input name="$id" type="text" value="$id"> </td></tr>
<td>Game Name:</td><td><input name="$gamename" type="text" value="$gamename"> </td></tr>
<tr><td>Minimum Players:</td><td><input name="$minplayer" type="text" value="$minplayer"></td></tr>
<tr><td>Miximum Players:</td><td><input name="$maxplayer" type="text" value="$maxplayer"></td></tr>
<tr><td>Private Price:</td><td><input name="$prvprice" type="text" value="$prvprice"></td></tr>
<tr><td>Public Price:</td><td><input name="$pubPrice" type="text" value="$pubprice"></td></tr>
</tr></table> ";
echo "<br><br><input type="submit" value="Edit Game">";
echo "<input type="button" onClick="history.back()" value="Back">";
echo "</form>";
}
?>
The first drop down menu works then when i submit it it brings me to another form which displays the data then i click the 'edit game' button i want it to edit the info, its just that the second form dosnt enter the info, any help?
<?php
//...
$result = mysql_query("SELECT * FROM gamesinfo WHERE gameName = '".$_POST['gameName']."' LIMIT 1") or die('Error on query #1: ' . mysql_error());
$sql = mysql_query("UPDATE gamesinfo SET gameName = '$_POST[gamename]' AND minPlayer = '$_POST[minplayer]' AND maxPlayer = '$_POST[maxplayer]' AND prvPrice = '$_POST[prvprice]' AND pubPrice = '$_POST[pubprice]' AND WHERE id = '$id'") or die('Error on query #2: ' . mysql_error());
//...
?>
and tell us if you get any "Error on query #X" displayed, specially #1.
2 - Also, I wonder where does the variable $id come from (WHERE id = '$id').
Well, latter we'll discuss the register_globals being on.
Then you'll have to know where this $id should come from... But it leads me to think that this $id is that $id you copy from $result[id] few lines down. Then the sequence of lines would change to this:
<?php
//...
$result = mysql_query("SELECT * FROM gamesinfo WHERE gameName = '".$_POST['gameName']."' LIMIT 1") or die('Error on query #1: ' . mysql_error());
$result = mysql_fetch_assoc($result);
#the game data is now held in the $result variable
$id = $result[id];
$gamename = $result[gameName];
$minplayer = $result[minPlayer];
$maxplayer = $result[maxPlayer];
$prvprice = $result[prvPrice];
$pubprice = $result[pubPrice];
$sql = mysql_query("UPDATE gamesinfo SET gameName = '$_POST[gamename]' AND minPlayer = '$_POST[minplayer]' AND maxPlayer = '$_POST[maxplayer]' AND prvPrice = '$_POST[prvprice]' AND pubPrice = '$_POST[pubprice]' AND WHERE id = '$id'") or die('Error on query #2: ' . mysql_error());
//..
?>
Well, since the first query returns no error, we should see how much rows are being returned by it:
<?php
//...
$result = mysql_query("SELECT * FROM gamesinfo WHERE gameName = '".$_POST['gameName']."' LIMIT 1") or die('Error on query #1: ' . mysql_error());
$result = mysql_fetch_assoc($result);
// Test to see if there is a row returned
if (mysql_num_rows() != 1)
die('Oops, rows returned: ' . mysql_num_rows());
// Show the contents of $result, for reference: [php_man]print_r[/php_man]()
print_r($result);
#the game data is now held in the $result variable
$id = $result[id];
$gamename = $result[gameName];
$minplayer = $result[minPlayer];
$maxplayer = $result[maxPlayer];
$prvprice = $result[prvPrice];
$pubprice = $result[pubPrice];
$sql = mysql_query("UPDATE gamesinfo SET gameName = '$_POST[gamename]' AND minPlayer = '$_POST[minplayer]' AND maxPlayer = '$_POST[maxplayer]' AND prvPrice = '$_POST[prvprice]' AND pubPrice = '$_POST[pubprice]' AND WHERE id = '$id'") or die('Error on query #2: ' . mysql_error());
//..
?>
Show us the output of the code with the above modifications.
<?php
//...
$result = mysql_query("SELECT * FROM gamesinfo WHERE gameName = '".$_POST['gameName']."' LIMIT 1") or die('Error on query #1: ' . mysql_error());
$row = mysql_fetch_assoc($result);
// Test to see if there is a row returned
if (mysql_num_rows($result) != 1) // [php_man]mysql_num_rows[/php_man]()'s reference
die('Oops, rows returned: ' . mysql_num_rows($result));
// Show the contents of $row, for reference: [php_man]print_r[/php_man]()
print_r($row);
#the game data is now held in the $row variable
$id = $row['id'];
$gamename = $row['gameName'];
$minplayer = $row['minPlayer'];
$maxplayer = $row['maxPlayer'];
$prvprice = $row['prvPrice'];
$pubprice = $row['pubPrice'];
$result = mysql_query("UPDATE gamesinfo SET gameName = '$_POST[gamename]' AND minPlayer = '$_POST[minplayer]' AND maxPlayer = '$_POST[maxplayer]' AND prvPrice = '$_POST[prvprice]' AND pubPrice = '$_POST[pubprice]' AND WHERE id = '$id'") or die('Error on query #2: ' . mysql_error());
//..
?>