PDA

Click to See Complete Forum and Search --> : Inserting into db


Michael_Kamen
Aug 7th, 2004, 12:27 PM
Hi,

I'm trying to insert these values into my mysql database, but my browser throws in an error message at this line:

$query = "INSERT INTO staf_temp VALUES (', $_POST['achternaam'], $_POST['voornaam'],$_POST['tussenvoegsel'],$_POST['adres'],$_POST['postcode'], $_POST['woonplaats'],$_POST['telefoon'],, $_POST['email'],,$_POST['gebdat1'] . $_POST['gebdat2']. $_POST['gebdat3'],$_POST['voornaam'], Date('d m Y'), '3', $_POST['wachtwoord1']');

The error is:


Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in blahblah/aanmelding.php on line 66

Michael_Kamen
Aug 7th, 2004, 12:28 PM
The first value is the ID, so I left that one blank. (hence only the ',' ) :)

visualAd
Aug 7th, 2004, 05:50 PM
You had a lot of errors in that line. If you want to include a variable array in a string you need to enclose it in curly brackets. I.e.:

$string = "{$_POST['var']}"

If you dont do this then it will just print the varaible name as it is in the string.

Have you checked your POST variables? Making sure all integers are integers and all strings have SQL meta characters escaped? - if not I suggest you do, otherwise your scriptwill be open to SQL injections.

Also when you are writing an SQL query for MySql, enclose all all values in single quotes, even dates and numbers (mysql will convert them as necessary) and if you are omiting a field it is a good idea to put NULL in its place to show its being omitted. Although it makes your query bigger it is also a good idea to explicitly state only the fields you are going to insert. This that if you add or remove a column from your table , you won't need to change the query.

I have corrected the line for you and it should work:

$query = "INSERT INTO staf_temp VALUES (NULL, '{$_POST['achternaam']}', '{$_POST['voornaam']}', " .
"'{$_POST['tussenvoegsel']}', '{$_POST['adres']}', '{$_POST['postcode']}', " .
"'{$_POST['woonplaats']}','{$_POST['telefoon']}', NULL, '{$_POST['email']}', NULL, " .
"'{$_POST['gebdat1']}{$_POST['gebdat2']}{$_POST['gebdat3']}', '{$_POST['voornaam']}', '" .
Date('d m Y') . "', '3', '{$_POST['wachtwoord1']}')";

john tindell
Aug 7th, 2004, 05:54 PM
you forgot to close the string.

try this


$query = "INSERT INTO staf_temp VALUES (', $_POST['achternaam'], $_POST['voornaam'],$_POST['tussenvoegsel'],$_POST['adres'],$_POST['postcode'], $_POST['woonplaats'],$_POST['telefoon'],, $_POST['email'],,$_POST['gebdat1'] . $_POST['gebdat2']. $_POST['gebdat3'],$_POST['voornaam'], Date('d m Y'), '3', $_POST['wachtwoord1']')";


also try using this syntax for your SQL statement


INSERT INTO table (name1, name2) VALUES ("value1", "value2")

It means you only need to supply information for data you want to insert and not make mistakes with column name order (if that made sense:lol: )

The Hobo
Aug 8th, 2004, 03:56 PM
Michael, just curious -- what text editor do you use? Does it have syntax-coloring?

Michael_Kamen
Aug 8th, 2004, 04:19 PM
I used notepad. Just switched to Crimson Editor.

The Hobo
Aug 8th, 2004, 05:04 PM
I used to use Notepad, and then I switched to EditPlus. One thing I noticed is that the syntax highlighting really helps catch errors like this. Like in the code above, since the semicolon is red like the text, you know something's up.

The Hobo
Aug 8th, 2004, 05:06 PM
Originally posted by Michael_Kamen
The first value is the ID, so I left that one blank. (hence only the ',' ) :)

Also, in your code you have this:

VALUES (',

That's going to generate an error on the MySQL end. You want:

VALUES ('',

Two of them.

ober0330
Aug 9th, 2004, 01:26 PM
Macromedia's Homesite 5.5 has excellent code coloring. It's a little expensive, but well worth it, especially if your company can buy it for you.

visualAd
Aug 9th, 2004, 01:28 PM
gVIM - Open source and supports syntax highlighting for every language under the sun. :bigyello:

The Hobo
Aug 9th, 2004, 02:19 PM
I'd recommend EditPlus. It's not free, but it doesn't force you to purchase it.