wheelbarrow Posted December 16, 2003 Share Posted December 16, 2003 I just got a web server with PHP and MySQL and for some reason when I try to send data through a form to db it gives me no error messages but nothing happens in the database. Heres the script <? # Sample Database Connection Syntax for PHP and MySQL. # Connect To Database $hostname="localhost"; $username="user"; $password="pass"; $dbname="user"; $usertable="test"; $item=$HTTP_POST_VARS[item]; $name=$HTTP_POST_VARS[name]; MYSQL_CONNECT($hostname,$username, $password) OR DIE ("<html><script language=\'JavaScript\'>alert(\'Unable to connect to database! Please try again later.\'),history.go(-1)</script></html>"); @mysql_select_db($dbname) or DIE ("<html><script language=\'JavaScript\'>alert(\'Unable to use database! Please try again later.\'),history.go(-1)</script></html>"); # Check If Record Exists $query = "SELECT * FROM $usertable WHERE item = \'$item\'"; $result = MYSQL_QUERY($query); $number = MYSQL_NUMROWS($result); # Insert Record into Table $query = "INSERT INTO $usertable (item,name) VALUES(\'$item\',\'$name\')"; $result = MYSQL_QUERY($query); ?> That is the sample script they gave me so I cant see what the problem is Quote Link to comment https://forums.phpfreaks.com/topic/1514-post-vars/ Share on other sites More sharing options...
DylanBlitz Posted December 16, 2003 Share Posted December 16, 2003 You need to get used to doing some error checking. You don\'t see any error because you aren\'t telling PHP to let you know if it doesn\'t work. Give this a shot and it should help ya out. [php:1:b146549fac] <? # Sample Database Connection Syntax for PHP and MySQL. # Connect To Database $hostname=\"localhost\"; $username=\"user\"; $password=\"pass\"; $dbname=\"user\"; $usertable=\"test\"; $item=$HTTP_POST_VARS[item]; $name=$HTTP_POST_VARS[name]; MYSQL_CONNECT($hostname,$username, $password) OR DIE (\"<html><script language=\'JavaScript\'>alert(\'Unable to connect to database! Please try again later.\'),history.go(-1)</script></html>\"); @mysql_select_db($dbname) or DIE (\"<html><script language=\'JavaScript\'>alert(\'Unable to use database! Please try again later.\'),history.go(-1)</script></html>\"); # Check If Record Exists $query = \"SELECT * FROM $usertable WHERE item = \'$item\'\"; $queryresult = MYSQL_QUERY($query); if (!$queryresult) { echo mysql_errno().\": \".mysql_error().\"n\"; exit; } $number = MYSQL_NUMROWS($result); # Insert Record into Table $query = \"INSERT INTO $usertable (item,name) VALUES(\'$item\',\'$name\')\"; $insertresult = MYSQL_QUERY($query); if (!$insertresult) { echo mysql_errno().\": \".mysql_error().\"n\"; exit; } ?>[/php:1:b146549fac] Quote Link to comment https://forums.phpfreaks.com/topic/1514-post-vars/#findComment-4979 Share on other sites More sharing options...
wheelbarrow Posted December 16, 2003 Author Share Posted December 16, 2003 I got these two errors Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /usr/local/4admin/apache/vhosts/force-g.net/httpdocs/spheres/signup.php on line 27 1064: You have an error in your SQL syntax near \'table) VALUES(\'sgd\',\'sdgsd\')\' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/1514-post-vars/#findComment-4983 Share on other sites More sharing options...
DylanBlitz Posted December 16, 2003 Share Posted December 16, 2003 oh duh, my bad hehe change $number = MYSQL_NUMROWS($result); to $number = MYSQL_NUMROWS($queryresult); Quote Link to comment https://forums.phpfreaks.com/topic/1514-post-vars/#findComment-4984 Share on other sites More sharing options...
wheelbarrow Posted December 16, 2003 Author Share Posted December 16, 2003 Im still getting the SQL syntax error though its somewhere on this line $query = "INSERT INTO $usertable (item,table) VALUES (\'$item\',\'$table\')"; Quote Link to comment https://forums.phpfreaks.com/topic/1514-post-vars/#findComment-4985 Share on other sites More sharing options...
DylanBlitz Posted December 16, 2003 Share Posted December 16, 2003 ah, that code is different then what you posted? You can\'t use the name \"table\" as a field in mysql. It\'s a reserved name and mysql doesn\'t like it. rename it to something like $query = \"INSERT INTO $usertable (item,mytable) VALUES (\'$item\',\'$mytable\')\"; And make sure the database tables are setup right. Here is a list of field names that are reserved and you can\'t use http://www.vineyard.net/vni/docs/mysql/man...rved_words.html Quote Link to comment https://forums.phpfreaks.com/topic/1514-post-vars/#findComment-4986 Share on other sites More sharing options...
wheelbarrow Posted December 17, 2003 Author Share Posted December 17, 2003 thanks Quote Link to comment https://forums.phpfreaks.com/topic/1514-post-vars/#findComment-4987 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.