FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Trouble with submitting an integer to MySQL





DjinniFire
Ok I'm having trouble with submitting an integer value into MySQL
Code:

$add=mysql_query("INSERT INTO testtable SET num=$num");

I've also tried
Code:

$add=mysql_query("INSERT INTO `testtable` SET `num`=`$num`");

and
Code:

$add=mysql_query("INSERT INTO testtable SET num='$num'");


I get
Quote:
Error adding entry: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'num=5' at line 1


I've been entering 5 as my number in the input box and I get the value num from the form and I've tried the following for the POST variable
Code:

$num=$_POST['num'];
$num=intval($_POST['num'], 10);
$num=(int)$_POST['num'];


I've even changed type on the input box to integer o.O but text/integer both have same result
Code:
<input type="integer" name="num" value="" />


anybody can help?
I started learning PHP a week ago and I can't get the INSERT for MySQL to work for an integer. I can submit text and such, but the integer I'm having trouble with :/
DanielXP
Code to add the number to your database

Code:
$num = (int)$_POST["num"]; //(int) makes sure the number is a number. Stop the SQL injections.
$addcode = mysql_query("INSERT INTO `testtable` (`num`) VALUES('$num')");


Then for the form
Code:
<form method="post" action="next_page.php">
<input type="text" name="num" />
<input type="submit" value="Submit" />
<form>


Change the value of the action in the form to go to the php page what will add the sql.

Hope this helps
Fire Boar
Yup. SET is the syntax for UPDATE TABLE. VALUES () is the syntax for INSERT.

INSERT INTO table (field1, field2, field3) VALUES ('value1', 'value2', 'value3');

UPDATE table SET field1='value1' field2='value2' field3='value3' WHERE field1='xyz';

Quite easy to confuse, yet these are pretty useful so learn them!
DjinniFire
Ahh thanks a lot. I think the difference with what I've tested was the single quote versus double quotes for $_POST["num"].
Related topics
Trouble using PHP5 with mysql
MySQL DB Backup script - minor problem
Simple membership system needed
Trouble connecting to MySQL
Problem connecting to MySQL database with PHP [RESOLVED]
MySQL Problem
VB + MYSQL
MySQL adapter "mysql" is not available.
whats wrong with this mysql query?
How can I check...
"Drop Down List Box" Using PHP and MySql
PHP code to know decimal character in MYSQL
Storing large integer in MySQL database
CMS without MYSQL
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.