Page 1 of 2
INSERT INTO doesn't work.[sorted at last. thanks wayne]
Posted: Mon May 14, 2007 4:15 pm
by toby_c500
Hi.
This has been bugging me for ages now. Finally got over a few problems.
INSERT INTO doesn't seem to work. I check my DB and no new data is added. Any ideas?
the:
if (!$insert){
echo "insert into not working";
exit;
}
shows true. I have double checked the names of the database and tables. Also all the form values are correct.
Is it syntax?
Code: Select all
<?php
$link = mysql_connect('localhost', 'root', 'root');
$db = mysql_select_db('jobs4alltrades', $link);
$insert = mysql_query("INSERT INTO members (loginid, password, firstname, surname, email,
trade, address1, address2, address3, address4,
postzip, country, yearsexp, about)
VALUES ('".$_POST['loginid']."', '".$_POST['password']."', '".$_POST['firstname']."',
'".$_POST['surname']."', '".$_POST['email']."', '".$_POST['trade']."', '".$_POST['address1']."',
'".$_POST['address2']."', '".$_POST['address3']."','".$_POST['address4']."', '".$_POST['post']."',
'".$_POST['country']."','".$_POST['yearsexp']."','".$_POST['about']."')");
if (!$insert){
echo "insert into not working";
exit;
}
$result = mysql_query($insert, $link);
if(!$link){
echo "Sorry, we are having a few problems with our system. Please try again later. link";
exit;
}
else{
echo "Your details have been stored in our database. Use the the navigation bar at the top to look for jobs.";
}
?>
Posted: Mon May 14, 2007 4:20 pm
by guitarlvr
not positive but i dont think you need to concatenate all that, just put the variable in quotes and separate them by commas.
Code: Select all
$insert = mysql_query("INSERT INTO members (loginid, password, firstname, surname, email,
trade, address1, address2, address3, address4,
postzip, country, yearsexp, about)
VALUES ('$_POST['loginid']', '$_POST['password']', '$_POST['firstname']',
'$_POST['surname']', '$_POST['email']', '$_POST['trade']', '$_POST['address1']',
'$_POST['address2']', '$_POST['address3']', '$_POST['address4']', '$_POST['post']',
'$_POST['country']', '$_POST['yearsexp']', '$_POST['about']')");
Wayne
Posted: Mon May 14, 2007 4:55 pm
by Weirdan
Posted: Mon May 14, 2007 5:12 pm
by CoderGoblin
Code: Select all
$insert = mysql_query("INSERT INTO members (loginid,password,
firstname,surname,
email,trade,
address1,address2,
address3,address4,
postzip,country,
yearsexp,about)
VALUES ('{$_POST['loginid']}', '{$_POST['password']}',
'{$_POST['firstname']}', '{$_POST['surname']}',
'{$_POST['email']}', '{$_POST['trade']}',
'{$_POST['address1']}', '{$_POST['address2']}',
'{$_POST['address3']}', '{$_POST['address4']}',
'{$_POST['post']}', '{$_POST['country']}',
'{$_POST['yearsexp']}', '{$_POST['about']}')");
Is another way of making the string without concatenation. At a guess one of your variables has a quote in it. Never trust user input. You should always validate and use
mysql_escape_string at a minimum.
Posted: Mon May 14, 2007 5:22 pm
by RobertGonzalez
You are checking boolean true/false against the query your are trying to pass to the db, not the actual result identifier. Because there is a value to that string, it will always evaluate to true. This is your code:
Code: Select all
<?php
$link = mysql_connect('localhost', 'root', 'root');
$db = mysql_select_db('jobs4alltrades', $link);
$insert = mysql_query("INSERT INTO members (loginid, password, firstname, surname, email,
trade, address1, address2, address3, address4,
postzip, country, yearsexp, about)
VALUES ('".$_POST['loginid']."', '".$_POST['password']."', '".$_POST['firstname']."',
'".$_POST['surname']."', '".$_POST['email']."', '".$_POST['trade']."', '".$_POST['address1']."',
'".$_POST['address2']."', '".$_POST['address3']."','".$_POST['address4']."', '".$_POST['post']."',
'".$_POST['country']."','".$_POST['yearsexp']."','".$_POST['about']."')");
// Notice, you haven't sent the query to the database yet?
if (!$insert){
echo "insert into not working";
exit;
}
// Now you are sending it
$result = mysql_query($insert, $link);
// This is checking your database connection
if(!$link){
echo "Sorry, we are having a few problems with our system. Please try again later. link";
exit;
}
else{
echo "Your details have been stored in our database. Use the the navigation bar at the top to look for jobs.";
}
?>
Maybe you can try something like this:
Code: Select all
<?php
$link = mysql_connect('localhost', 'root', 'root');
// This is checking your database connection
if(!$link){
echo "Sorry, we are having a few problems with our system. Please try again later. link";
exit;
}
else{
echo "Your details have been stored in our database. Use the the navigation bar at the top to look for jobs.";
}
$db = mysql_select_db('jobs4alltrades', $link);
if (!$db) die('could not select the database');
$insert = mysql_query("INSERT INTO members (loginid, password, firstname, surname, email,
trade, address1, address2, address3, address4,
postzip, country, yearsexp, about)
VALUES ('".$_POST['loginid']."', '".$_POST['password']."', '".$_POST['firstname']."',
'".$_POST['surname']."', '".$_POST['email']."', '".$_POST['trade']."', '".$_POST['address1']."',
'".$_POST['address2']."', '".$_POST['address3']."','".$_POST['address4']."', '".$_POST['post']."',
'".$_POST['country']."','".$_POST['yearsexp']."','".$_POST['about']."')");
$result = mysql_query($insert, $link);
if ($result === false){
echo "insert into not working";
exit;
}
?>
Posted: Mon May 14, 2007 5:26 pm
by Weirdan
Everah wrote:
Code: Select all
$insert = mysql_query("INSERT INTO members (.....) values (......)"); <====== mysql_query HERE!
// ....
// Notice, you haven't sent the query to the database yet?
if (!$insert){
//....
}
Everah, you need to get some sleep

Posted: Mon May 14, 2007 5:36 pm
by RobertGonzalez
Dammit. Slap me, someone just plain slap me.
Try not to follow any more of advice of mine until tomorrow morning. Follow my advice at your own peril.
Posted: Tue May 15, 2007 12:41 pm
by toby_c500
Thanks SO much for all your help.
just telling my girlfriend how amazing this site is. You guys are awesome. I will try your advice.
Thanks again.
Posted: Tue May 15, 2007 2:42 pm
by toby_c500
This is seriously driving me up the wall!
I just can't seem to sort this out. I have followed the advice above in every combination possible to no avail.
Could this be a problem with mysql? I have set up a table called 'members' with fields EXACTLY the same as in the INSERT INTO. So surely the script should work.
I'm getting:
Your details have been stored in our database. Use the the navigation bar at the top to look for jobs. insert into not working
from:
Code: Select all
<?php
$link = mysql_connect('localhost', 'root', 'root');
if(!$link){
echo "Sorry, we are having a few problems with our system. Please try again later. link";
exit;
}
else{
echo "Your details have been stored in our database. Use the the navigation bar at the top to look for jobs.";
}
$db = mysql_select_db('jobs4alltrades', $link);
if (!$db) die('could not select the database');
$insert = mysql_query("INSERT INTO members (loginid, password, firstname, surname, email,
trade, address1, address2, address3, address4,
postzip, country, yearsexp, about)
VALUES ('".$_POST['loginid']."', '".$_POST['password']."', '".$_POST['firstname']."',
'".$_POST['surname']."', '".$_POST['email']."', '".$_POST['trade']."', '".$_POST['address1']."',
'".$_POST['address2']."', '".$_POST['address3']."','".$_POST['address4']."', '".$_POST['post']."',
'".$_POST['country']."','".$_POST['yearsexp']."','".$_POST['about']."')");
$result = mysql_query($insert, $link);
if ($result === false){
echo "insert into not working";
exit;
}
?>
Sorry to be a pain. But can anyone help? I'm going mad here. It all starts to sink in and then you think you've got it...NOPE. It doesn't work. I'm going to be bald by the time I've figured this out.
Thanks.
Posted: Tue May 15, 2007 2:50 pm
by guitarlvr
Does he need === in the last statement or will == work?
Wayne
Posted: Tue May 15, 2007 2:58 pm
by toby_c500
I've tried that Wayne. Thanks mate. I was a little unsure to. Still no joy.
Posted: Tue May 15, 2007 3:05 pm
by guitarlvr
what happens when you run this after putting in this code?
Code: Select all
$result = mysql_query($insert, $link) or die("Query: $insert\n<br /.> MySQL Error: " . mysql_error());
Wayne
Posted: Tue May 15, 2007 3:14 pm
by toby_c500
Ha, thats interesting:
Query:
MySQL Error: Query was empty.
Why is that then? The var is not containing the data. So that means that there is defo a problem with the INSERT INTO. Why is the var not holding the data?
Or have I got this wrong?
Thanks Wayne.
Posted: Tue May 15, 2007 3:19 pm
by guitarlvr
Try this:
Code: Select all
$insert = 'INSERT INTO members (loginid, password, firstname, surname, email,
trade, address1, address2, address3, address4,
postzip, country, yearsexp, about)
VALUES ('".$_POST['loginid']."', '".$_POST['password']."', '".$_POST['firstname']."',
'".$_POST['surname']."', '".$_POST['email']."', '".$_POST['trade']."', '".$_POST['address1']."',
'".$_POST['address2']."', '".$_POST['address3']."','".$_POST['address4']."', '".$_POST['post']."',
'".$_POST['country']."','".$_POST['yearsexp']."','".$_POST['about']."')")';
Wayne
Posted: Tue May 15, 2007 3:24 pm
by toby_c500
Whats different there then?
Parse error: syntax error, unexpected '"' in /Applications/MAMP/htdocs/register.php on line 25
which is the first line of VALUES.