Yuki Kutsuya
Yuki Kutsuya

Reputation: 4088

MySQL insert errors

It's pretty much one of my first times working with MYSQL, and I can't seem to fix this one error I keep getting. I'm trying to store data to a table which has an auto_increment on its id (first column). The error I keep getting is this:

"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 'voorletters ='asd', tussenvoegsel ='', achternaam ='', roepnaam ='', adres ='', ' at line 1"

I just filled the textboxes with a little bit of rubish, there are no columns that require data either. Here is the code I use:

if(isset($_POST['save']))
{   
    $voorletters = $_POST['voorletters'];
    $tussenvoegsel = $_POST['tussenvoegsel'];
    $achternaam = $_POST['achternaam'];
    $roepnaam = $_POST['roepnaam'];
    $adres = $_POST['adres'];
    $postcode = $_POST['postcode'];
    $plaats = $_POST['plaats'];
    $geslacht = $_POST['geslacht'];
    $emailadres = $_POST['emailadres'];
    $telefoonnummer = $_POST['telefoonnummer'];
    $mobielenummer = $_POST['mobielenummer'];
    $geboortedatum = $_POST['geboortedatum'];
    $bsn = $_POST['bsn'];

    mysql_query("INSERT INTO `naw` "
            . "voorletters ='$voorletters', "
            . "tussenvoegsel ='$tussenvoegsel', "
            . "achternaam ='$achternaam', "
            . "roepnaam ='$roepnaam', "
            . "adres ='$adres', "
            . "postcode ='$postcode', "
            . "plaats ='$plaats', "
            . "geslacht ='$geslacht', "
            . "emailadres ='$emailadres', "
            . "telefoonnummer ='$telefoonnummer', "
            . "mobielenummer ='$mobielenummer', "
            . "geboortedatum ='$geboortedatum', "
            . "bsn ='$bsn' "
            . "WHERE id = '$id'")
            or die(mysql_error()); 

If this isn't enough information, please tell me. I've tried a lot of things, but I can't seem to figure it out.

Upvotes: 0

Views: 65

Answers (4)

Remi Hirtz
Remi Hirtz

Reputation: 144

You should remove the `` around naw, it's ok in phpmyadmin but quite messy almost every where else. And you souldn't concatenate every line, do it in one "..." and use backspace to make it more readable.

So:

mysql_query("INSERT INTO naw
            VALUES('$voorletters', 
                    '$tussenvoegsel',
                    ... ,
                     WHERE id = '$id'");//you can't do that, maybe you should use an UPDATE

Upvotes: 0

tisuchi
tisuchi

Reputation: 129

Just use following code. Make sure that you are inserting data for every field sequentially-

mysql_query("INSERT INTO `naw` VALUES(
            '".$voorletters."',
            '".$tussenvoegsel."',
            '".$achternaam."',
            '".$roepnaam."',
            '".$adres."',
            '".$postcode."',
            '".$plaats."',
            '".$geslacht."',
            '".$emailadres."',
            '".$telefoonnummer."',
            '".$mobielenummer."',
            '".$geboortedatum."',
            '".$bsn."')")
            or die(mysql_error());

Upvotes: 0

genespos
genespos

Reputation: 3311

You have a wrong syntax

The INSERT syntax is

INSERT INTO `YourTableName`(`Field1`, `Field2`, `Field3`, `Field4)
VALUES ('value-1','value-2','value-3','value-4')

The UPDATE syntax is

UPDATE `YourTableName` 
SET `Field1`='value-1',`Field2`='value-2',`Field3`='value-3',`Field4`='value-4'
WHERE YourConditions

Upvotes: 0

juergen d
juergen d

Reputation: 204766

You mix up insert and update syntax. Replace

INSERT INTO `naw` voorletters ='$voorletters'...

with

UPDATE `naw` set voorletters ='$voorletters'....

And you should really use Prepared Statements to avoid syntax errors and SQL injections due to user input.

Upvotes: 5

Related Questions