INSERT INTO doesn't work.[sorted at last. thanks wayne]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

INSERT INTO doesn't work.[sorted at last. thanks wayne]

Post 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.";
	}

 ?>
Last edited by toby_c500 on Tue May 15, 2007 4:17 pm, edited 1 time in total.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

add

Code: Select all

var_dump(mysql_error());
after your

Code: Select all

echo 'insert not working';
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
}
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post 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.
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post 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.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

Does he need === in the last statement or will == work?

Wayne
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post by toby_c500 »

I've tried that Wayne. Thanks mate. I was a little unsure to. Still no joy.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post 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.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post 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.
Post Reply