Jump to content

Archived

This topic is now archived and is closed to further replies.

wheelbarrow

post vars

Recommended Posts

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

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

oh duh, my bad hehe

 

change

 

$number = MYSQL_NUMROWS($result);

 

to

 

$number = MYSQL_NUMROWS($queryresult);

Share this post


Link to post
Share on other sites

Im still getting the SQL syntax error though its somewhere on this line

$query = "INSERT INTO $usertable (item,table) VALUES (\'$item\',\'$table\')";

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.