PDA

View Full Version : SQL Query Not Inserting data into MySQL


XtremeGamer99
08-16-2004, 01:13 AM
Hey. I can't figure out what is wrong with this script. The INSERT dosen't seem to be working.

Note: This is a very basic script, mainly to see if it would work. There are a lot of unnessasary thing like the "\", but I will get to that later.

I have this same script working with other pages (add author, add game), but this one doesn't put it into MySQL.

Can anyone help me?


index.php
<?
$authorID = $_GET['authorID'];
$gameID = $_GET['game'];
echo ('
Search for a Author:
...



...
";
}

if (isset($game))
{
echo "

<form method=\"get\" action=\"addcheat.php\">
Add new cheat to the database.
<input type=\"hidden\" name=\"cheatID\" value=\"NULL\">
<input type=\"hidden\" name=\"author\" value=\"$authorID\">
<input type=\"hidden\" name=\"game\" value=\"$gameID\">
Game: $gameID<br />
Author: $authorID<br />
Cheat Title: <input type=\"text\" name=\"cheattitle\"><br>
Cheat Text:<br /><textarea name=\"cheattext\" rows=\"5\" cols=\"50\"></textarea><br />
<select name=\"db\" size=\"1\">
<option value=\"gamecube\" />GCN
<option value=\"gba\" />GBA
<option value=\"ps2\" />P2
<option value=\"xbox\" />Xbox
<option value=\"pc\" />PC
</select><input type=\"submit\" value=\"Add\" />
</form>
";
}
?>

addcheat.php
<?
$authorID = $_GET['authorID'];
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "DELETED";
$DBName = $_GET['db'];
$cheattitle = $_GET['cheattitle'];
$cheattext = $_GET['cheattext'];
$author = $_GET['author'];
$game = $_GET['game'];
$table = "cheats";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select
database $DBName");

$sqlquery = "INSERT INTO $table VALUES ('$cheatID','$cheattitle',$cheattext', CURDATE( ) ,'$author','$game')";

$results = mysql_query($sqlquery);

mysql_close();

print "<center><table border=\"0\" width=\"500\"><tr><td>";
print "<p><font face=\"verdana\" size=\"+0\"> <center><a href=\"index.php\">You just finished adding a cheat to the database. Click here to add another one.</a></td></tr></table>
</center></BODY></HTML>";
?>

(Note: this line: <input type=\"hidden\" name=\"cheatID\" value=\"NULL\"> is NULL because the MySQL sets it as a auto number.)

raf
08-16-2004, 09:26 AM
You don't need to include a value for the autonum column.

Change your query so that you get both a fieldlist and valuelist. Like

$sql="INSERT INTO table (var1, var2, var3) VALUES('". $value1 ."','". $value2 ."','". $value3 ."')"

and for debugging, change your queryexecution to:

$results = mysql_query($sqlquery) or die ('Queryproblem inserting record:' . mysql_error());

(remove the .mysql_error() ) on implementation.)

This will show you the error that causes it to not insert.

XtremeGamer99
08-17-2004, 12:00 AM
Now I can see why you are a "Helpfull Member". I have asked so many questions on this board, and you have been very helpfull indeed. Still need to work out a few quriks here and there, but my site now looks ready to be populated with content.

Thanks agian raf

raf
08-17-2004, 02:31 AM
You're welcome. Happy coding :thumbsup: