...

View Full Version : Error querying database :( HELP!



DivineComedian
12-24-2010, 10:59 PM
Hey guys, I was just wondering if anyone could point out the mistake I'm making. Everything seems pretty okay to me but I ALWAYS get an error when the script try's to query the database. Sad. Any Ideas?

<html>
<head><title>Add Email</title></head>
<body>
<?php
//START---->GET THE DATA
$first = $_POST['firstname'];
$last = $_POST['lastname'];
$email = $_POST['emailaddress'];
//END

//CONNECT TO DATA BASE
$dbc = mysqli_connect('address','user','pass','database');
if(!$dbc){
die("Connection unsucessful...");
}
echo "Connection successful..";
//CREATE QUERY

$q = "INSERT INTO email_list('first_name','last_name','email')" . "VALUES('$first', '$last', '$email')";
//SEND QUERY
$result = mysqli_query($dbc, $q)
or die('Oops! Error querying database.');
//CLOSE
mysqli_close($dbc);

//ECHO SUCCESSFUL
echo "Data has been sent!";

?>
</body>
</html>
NOTE: I always get Oops! Error querying database.

Dormilich
12-24-2010, 11:15 PM
I'd say, look at the query you use (echo it out), there are spaces missing. mysqli_error() can certainly tell you more.

PS. congratulation for not using the old mysql_* functions

kbluhm
12-24-2010, 11:17 PM
Change this line:


or die('Oops! Error querying database.');

...to this:


or die(
'<pre>MySQL Error: ' . mysql_error( $dbc ) . "\n\n" .
'MySQL Query: ' . $q . '</pre>'
);

...and paste the message here.

DivineComedian
12-24-2010, 11:40 PM
Change this line:


or die('Oops! Error querying database.');

...to this:


or die(
'<pre>MySQL Error: ' . mysql_error( $dbc ) . "\n\n" .
'MySQL Query: ' . $q . '</pre>'
);

...and paste the message here.
I get this:

Connection successful..

{MySQL Error: 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 ''first_name','last_name','email') VALUES ('Random_name1', 'Random_name2', 'Rando' at line 1

MySQL Query: INSERT INTO email_list('first_name','last_name','email') VALUES ('Random_name1', 'Random_name2', 'Random_email@flymail.com' )}

So the syntax I'm using doesn't correspond with the version of the MYSQL server I'm using? I got that right?
P.S:
My MySQL server version is 5.1.36-community-log btw.

DJCMBear
12-24-2010, 11:53 PM
Remove the quotes

Change
('first_name','last_name','email')
To
(first_name,last_name,email)

DivineComedian
12-25-2010, 01:40 AM
Remove the quotes

Change
('first_name','last_name','email')
To
(first_name,last_name,email)
Nope, that didn't work either :(

DivineComedian
12-25-2010, 02:09 AM
Thank you everyone!!! After examining the code line by line, I found the problem! I should have put single quotes around the variables:firstname,lastname and email :)

EDIT:
And not around the table names.

DJCMBear
12-25-2010, 03:56 AM
Thank you everyone!!! After examining the code line by line, I found the problem! I shouldn't have put single quotes around the variables:firstname,lastname and email :)

That's what I said your problem was and you said it wasn't :confused:

DivineComedian
12-25-2010, 06:26 AM
That's what I said your problem was and you said it wasn't :confused:
LOL! Oh right, poor me...I believe I must have dropped the single quotes around the variables aswell. In away I'm glad it happened 'cause I learnt a lot! Thanks again!! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum