Page 1 of 1

little help pls

Posted: Sat Aug 07, 2004 5:01 pm
by SidewinderX
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.

Code: Select all

<?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?

Posted: Sat Aug 07, 2004 5:15 pm
by scorphus
Here we go, step-by-step...

First: what does the following script return?

Code: Select all

<?php
echo '<pre>';
echo 'PHP Version: '.phpversion()."\n";
echo 'Display Errors: '.(ini_get('display_errors') == '1' ? 'On' : 'Off')."\n";
echo 'Error Level: '.(ini_get('error_reporting') == '2047' ? 'E_ALL' : 'Not E_ALL')."\n";
echo 'Register Globals: '.(ini_get('register_globals') == '' ? 'Off' : 'On')."\n";
echo '</pre>';

/*
PHP Version: 4.3.4
Display Errors: On
Error Level: E_ALL
Register Globals: Off
*/
?>

Posted: Sat Aug 07, 2004 5:29 pm
by feyd
re:

Code: Select all

$id = $result[id];
$gamename = $result[gameName];
$minplayer = $result[minPlayer];
$maxplayer = $result[maxPlayer];
$prvprice = $result[prvPrice];
$pubprice = $result[pubPrice];
quote the named indices :P

Posted: Sat Aug 07, 2004 8:09 pm
by SidewinderX
First: what does the following script return?
PHP Version: 4.3.8
Display Errors: On
Error Level: Not E_ALL
Register Globals: On

Posted: Sun Aug 08, 2004 3:08 am
by scorphus
Ok. Now second:

1 - Change two lines of the script like the following:

Code: Select all

<?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.

-- Scorphus

Posted: Sun Aug 08, 2004 12:07 pm
by SidewinderX
and tell us if you get any "Error on query #X" displayed, specially #1.
Error on query #2: You have an error in your SQL syntax near 'WHERE id = ''' at line 1

Also, I wonder where does the variable $id come from (WHERE id = '$id')
I dont kow where that variable comes from..hehe, I was just copying off of a demo script i found, and got a little help from another friend.

Posted: Mon Aug 09, 2004 1:55 am
by scorphus
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:

Code: Select all

<?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:

Code: Select all

<?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.

-- Scorphus

Posted: Mon Aug 09, 2004 5:03 pm
by SidewinderX
if i did it right here is what i got...

Warning: Wrong parameter count for mysql_num_rows() in c:\apache\htdocs\edit\edit.php on line 24

Warning: Wrong parameter count for mysql_num_rows() in c:\apache\htdocs\edit\edit.php on line 25
Oops, rows returned:

Posted: Mon Aug 09, 2004 5:04 pm
by feyd
you need to pass it the original $result that was returned from mysql_query

Posted: Tue Aug 10, 2004 1:10 pm
by scorphus
Yeap... sorry for not being precise :oops:

Corrections made:

Code: Select all

<?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());
//..
?>
-- Scorphus


feyd | added quotes for array indices!