Page 1 of 2

MYSQL INSERT

Posted: Tue Jun 07, 2005 1:47 pm
by Parody
Im having trouble with a MYSQL insert query. I checked the code against some examples I found and from that it looked fine. But when I run the file it is in I get an error, a parse error, this to be precise:

Code: Select all

Parse error: parse error in c:\program files\eas1yphp1-8\www\registertmp.php on line 75
This is the line:

Code: Select all

mysql_query("INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '$rand', "$regdate", '".$_POST['email']."')";

And this is the block around it:

Code: Select all

mysql_connect("localhost", "(myusername)", "(mypassword)") or die(mysql_error());
mysql_select_db("users") or die(mysql_error());
$regdate = date('m d, Y');
mysql_query("INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '$rand', "$regdate", '".$_POST['email']."')";
mysql_query("INSERT INTO stats('".$_POST['uname']."','$chrctr', '100', '2000', '001', '01', 000, '01', '01', '01', '1', '0', '00000000', '00000000', '00000000', '00000000')");
echo mysql_errno() . ": " . mysql_error() . "<br><br>";
mysql_close();
Any ideas :?:

Posted: Tue Jun 07, 2005 1:54 pm
by Burrito
your $regdate is inside a terminated string.

you need to concat it with the rest of the string using "."

ie:

Code: Select all

$query = "blalh blah blah ".$regdate." more blah";

Posted: Tue Jun 07, 2005 2:07 pm
by Parody
Still get the same error :(

Posted: Tue Jun 07, 2005 2:41 pm
by timvw
Need to add ' ' around the value too....

Posted: Wed Jun 08, 2005 10:44 am
by Parody
So the line looks like this?:

Code: Select all

mysql_query("INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '$rand', '.$regdate.', '".$_POST['email']."')";
If that is what you mean, it didn't work :(

Posted: Wed Jun 08, 2005 10:46 am
by Burrito

Code: Select all

mysql_query("INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '".$rand."', '".$regdate."', '".$_POST['email']."')";

Posted: Wed Jun 08, 2005 10:47 am
by malcolmboston
do this

Code: Select all

$query = "INSERT INTO users (username, password, regdate, email) VALUES ({$_POST['uname']}, {$rand}, {$regdate}, {$_POST['email']})";
print $query;
#$result = mysql_query($query) or die (mysql_error());
this will tell you exactly what the query is in case theres some formatting errors that MySQL doesnt like, also i think it is better to seperate query from call anyway

Posted: Wed Jun 08, 2005 10:55 am
by Parody
I still get the same error :(

Posted: Wed Jun 08, 2005 10:56 am
by malcolmboston
you shouldnt get the error because your not even executing the query, your simply printing it out to check its all looking right

Posted: Wed Jun 08, 2005 11:01 am
by Burrito
try what I posted did you?

need the single quotes in there you do to insert properly to MySQL.

Agree with malcom I do in that you should probably create your query string outside of your actual insert function so that you can "debug" it...but the one I posted should work for you.

edit: updated to Yoda...it's Wednesday and forgot I did 8O

Posted: Wed Jun 08, 2005 11:06 am
by Parody
I tried just printing it, it doesn't work. It keeps saying parse error :(

Posted: Wed Jun 08, 2005 11:08 am
by Parody
Oh, wait it works :) But that doesnt insert my data :(

Posted: Wed Jun 08, 2005 11:11 am
by Burrito

Code: Select all

$qry = "INSERT INTO users (username, password, regdate, email) VALUES ('".$_POST['uname']."', '".$rand."', '".$regdate."', '".$_POST['email']."')";
echo $qry;
mysql_query($qry)
  or die(mysql_error());

Posted: Wed Jun 08, 2005 11:14 am
by Parody
No errors(for that problem), so I think that works, but I now have another error :(

This is the piece:

Code: Select all

<?php
$to = ".$_POST['email'].";
$subject = "(subject)";
$body = "Your password is: "$rand"";
if (mail($to, $subject, $body))
?>
The error is this:

Code: Select all

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\program files\eas1yphp1-8\www\registertmp123.php on line 84
Line 84 is this line :

Code: Select all

$to = ".$_POST['email'].";

Posted: Wed Jun 08, 2005 11:21 am
by John Cartwright

Code: Select all

<?php
$to = $_POST['email'];
$subject = "(subject)";
$body = "Your password is: ".$rand;
if (mail($to, $subject, $body))
?>