Jump to content

post vars


wheelbarrow

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

Link to comment
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]

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.