Forum Moderators: coopster

Message Too Old, No Replies

Unable to Execute Query / General Help

PHP Inserting data into different tables

         

CyberRoyal

6:12 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Hi I am new to php and although I can read the code quite happily I am having trouble with the following script:


<?php

mysql_connect("localhost", "username", "password")
or die("Sorry We are unable to Connect to the Database at this time");

mysql_select_db("webvisits")
or die("We are unable to locate the required table for this operation");

$message = 'Username';
$checkusername = $_POST['username'];
$check = mysql_query("SELECT username FROM auth_users WHERE username = '$checkusername'");
$check2 = mysql_num_rows($check);

if ($_POST) {
if ($check2 != 0) {
$message = 'Username taken. Please try another';
}
else
{
$_POST['password'] = md5($_POST['password']);

if (!get_magic_quotes_gpc()) {
$_POST['password'] = addslashes($_POST['password']);
$_POST['username'] = addslashes($_POST['username']);
}
$insert = "INSERT INTO auth_users (username, password)
VALUES ('".$_POST['username']."', '".$_POST['password']."')
INSERT INTO users_detail (username, first_name)
VALUES ('".$_POST['username']."', '".$_POST['firstname']."')";

$add_member = mysql_query($insert)
or die ("Unable to Execute Query");

If ($add_member) {header( "Location:loginform.html");}
}
}
?>

For some reason the query does not execute resulting in the die message "unable to execute Query" I have checked the db, table and field names and they all match (assuming I have not overlooked something). Can anyone assist.....also any general comments or pointers always welcome.

Many thanks for your time.

P.S Also can anyone tell me a better way to insert sample code onto the forum as to retain formatting of the code (its been a long day) :)

LifeinAsia

6:21 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



It would be more helpful to printout the exact error message the database is throwing.

CyberRoyal

6:32 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Good Idea :)

Here it is...

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 'INSERT INTO users_detail (username, first_name) VALUES ('John', 'John'' at line 3

does this mean line 3 of the insert section or line 3 of the script (Line 3 on my script is currently a comment - taken out for the purpose of posting here)

Thanks

inveni0

6:38 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



I didn't check everything, but I don't think placing two INSERT commands in a row works. Do each INSERT separately. Also, make sure you clean your $_POST variables with mysql_real_escape_string() before inserting the values.

So, change this:


$insert = "INSERT INTO auth_users (username, password)
VALUES ('".$_POST['username']."', '".$_POST['password']."')
INSERT INTO users_detail (username, first_name)
VALUES ('".$_POST['username']."', '".$_POST['firstname']."')";

$add_member = mysql_query($insert)
or die ("Unable to Execute Query");

to this:


$insert1 = "INSERT INTO auth_users (username, password)
VALUES ('".$_POST['username']."', '".$_POST['password']."')";
$insert2 = "INSERT INTO users_detail (username, first_name)
VALUES ('".$_POST['username']."', '".$_POST['firstname']."')";

$add_member = mysql_query($insert1)
or die ("Unable to Execute Query 1");

$add_member = mysql_query($insert2)
or die ("Unable to Execute Query 2");

It's hard to read what I'm writing on here, but that should work.

CyberRoyal

6:46 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Thanks Inveni0,

I will give it a try a bit later as I have got to nip out...if you or anyone else has any suggestions about the script, all constructive feedback welcome. I will post up later if it worked or not.

Thanks

LifeinAsia

6:51 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



VALUES ('John', 'John'' at line 3

That sounds to me like it may be a problem with your quote marks. But also try splitting the 2 inserts into separate calls as inveni0 suggested to help narrow down the error.

inveni0

3:25 pm on Apr 9, 2009 (gmt 0)

10+ Year Member



LifeinAsia, that's just an extra quote from the mySQL error. You can see that there's one before INSERT, also.

LifeinAsia

4:49 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Right, but the why is why isn't a close parenthesis included in the error statement before the quote? Shouldn't the error message be something like the following?
'INSERT INTO users_detail (username, first_name) VALUES ('John', 'John')' at line 3.

You should print out the exact value of $insert to make sure that the constructed statement is exactly what you expect.

CyberRoyal

10:17 am on Apr 10, 2009 (gmt 0)

10+ Year Member



Thanks inveni0, the double insert worked fine and with a bit of tweeking its working fine. However what did you mean by

make sure you clean your $_POST variables with mysql_real_escape_string() before inserting the values.

Thanks

CyberRoyal

10:32 am on Apr 10, 2009 (gmt 0)

10+ Year Member



Also with this form it reloads should the username already be in use - which is fine but all the other data is not remembered and has to be re-entered.

Can any one point me in the right direction in to how the data can be remembered. At this time in my head I think it will have something to do with a cookie or using POST.

Thanks for all your assistance.