Jump to content


Photo

post vars


  • Please log in to reply
6 replies to this topic

#1 wheelbarrow

wheelbarrow
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 16 December 2003 - 08:06 PM

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

#2 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 16 December 2003 - 08:50 PM

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]

#3 wheelbarrow

wheelbarrow
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 16 December 2003 - 09:47 PM

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

#4 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 16 December 2003 - 10:12 PM

oh duh, my bad hehe

change

$number = MYSQL_NUMROWS($result);

to

$number = MYSQL_NUMROWS($queryresult);

#5 wheelbarrow

wheelbarrow
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 16 December 2003 - 11:30 PM

Im still getting the SQL syntax error though its somewhere on this line
$query = "INSERT INTO $usertable (item,table) VALUES (\'$item\',\'$table\')";


#6 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 16 December 2003 - 11:44 PM

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....rved_words.html

#7 wheelbarrow

wheelbarrow
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 17 December 2003 - 12:17 AM

thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users