|
-
Jun 9th, 2004, 05:07 AM
#1
Thread Starter
Hyperactive Member
insert into db - resolved
Hi Guys.
im trying to insert a line into mysql with the follong code
PHP Code:
mysql_connect("localhost","root","InsertYourPasswordHere");
mysql_select_db ("mywakeupcall");
$query = "INSERT INTO database(fname,lname,alarm,phone) VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['atime']."','".$_POST['telephone']."')";
if (! mysql_query($query))
die ('Query Failed: ' . mysql_error());
echo 'Item Entered.';
however the error msg that comes back is
Query Failed: 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 'database(fname,lname,alarm,phone) VALUES('samantha','murray','1
andI cant find out whatthe problem is. fname and lname is text and the alarm and phone is numbers.
any help would be great thanks
chrisio
Last edited by Chrisio; Jun 9th, 2004 at 09:11 AM.
-
Jun 9th, 2004, 06:41 AM
#2
You have a table named "database"? I suggest changing it to something else. Database is most likely a reserved keyword, and shouldn't be used in tables/fields names.
-
Jun 9th, 2004, 06:58 AM
#3
Thread Starter
Hyperactive Member
ok. table name changed and nodifference i get the same error message as before.
PHP Code:
<?php
$temp1 = $_POST['firstname'];
$temp2 = $_POST['lastname'];
$temp3 = $_POST['atime'];
$temp4 = $_POST['telephone'];
mysql_connect("localhost","root","InsertYourPasswordHere");
mysql_select_db ("mywakeupcall");
$query = "INSERT INTO tblmain ('fname', 'lname', 'alarm', 'phone' VALUES ($temp1, $temp2, $temp3, $temp4)";
if (! mysql_query($query))
die ('Query Failed: ' . mysql_error());
echo 'Item Entered.';
?>
error message :
Query Failed: 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 ''fname', 'lname', 'alarm', 'phone' VALUES (samantha, murray, 10
im very puzzled.
if i also just use
INSERT INTO tblmain VALUES ($temp1, $temp2, $temp3, $temp4)";
it throws back this error message
Query Failed: Unknown column 'samantha' in 'field list'
the field names in the db are as follows
fname (varchar20)
lname (varchar20)
alarm (int4)
phone (int11)
thanks again
-
Jun 9th, 2004, 07:06 AM
#4
Frenzied Member
You can't just throw variables into a string without putting them in quotes. And when you are inserting, you have to enclose values in single quotes. Hence: singlequote|doublequote|dot|variablename|dot|doublequote|singlequote
PHP Code:
<?php
mysql_connect("localhost","root","InsertYourPasswordHere");
mysql_select_db ("mywakeupcall");
$query = "INSERT INTO tblmain ('fname', 'lname', 'alarm', 'phone') VALUES ('" . $_POST['firstname'] . "', '" . $_POST['lastname'] . "', '" . $_POST['atime'] . "', '" . $_POST['telephone'] . "')";
$pass = @mysql_query($query) or die('Query Failed: ' . mysql_error());
if ($pass)
echo 'Item Entered.';
?>
-
Jun 9th, 2004, 08:07 AM
#5
Thread Starter
Hyperactive Member
Originally posted by ober0330
You can't just throw variables into a string without putting them in quotes. And when you are inserting, you have to enclose values in single quotes. Hence: singlequote|doublequote|dot|variablename|dot|doublequote|singlequote
PHP Code:
<?php
mysql_connect("localhost","root","InsertYourPasswordHere");
mysql_select_db ("mywakeupcall");
$query = "INSERT INTO tblmain ('fname', 'lname', 'alarm', 'phone') VALUES ('" . $_POST['firstname'] . "', '" . $_POST['lastname'] . "', '" . $_POST['atime'] . "', '" . $_POST['telephone'] . "')";
$pass = @mysql_query($query) or die('Query Failed: ' . mysql_error());
if ($pass)
echo 'Item Entered.';
?>
hi.
I have tried that and get the exate same error msg so imcompleaty stuck now.
help?
-
Jun 9th, 2004, 08:44 AM
#6
Frenzied Member
Try this:
PHP Code:
<?php
mysql_connect("localhost","root","InsertYourPasswordHere");
mysql_select_db ("mywakeupcall");
$query = "INSERT INTO tblmain (fname, lname, alarm, phone) VALUES ('" . $_POST['firstname'] . "', '" . $_POST['lastname'] . "', '" . $_POST['atime'] . "', '" . $_POST['telephone'] . "')";
$pass = @mysql_query($query) or die('Query Failed: ' . mysql_error());
if ($pass)
echo 'Item Entered.';
?>
-
Jun 9th, 2004, 09:11 AM
#7
Thread Starter
Hyperactive Member
PERFECT THANKS VERY MUCH
-
Jun 9th, 2004, 09:31 AM
#8
Frenzied Member
You're welcome.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|