Stefto
Stefto

Reputation: 149

my sql query doesn't insert anything, what am i doing wrong?

i'm trying to insert a query into a database, however for some reason it's not working, perhaps you guys can see something i don't.
i know the enrties is right (as the checking bit does work on another page and so does the db selection.
it's starting to drive me nuts by now, and so is my project mate.
the query is used in PHP, after having filled a form. (on a different page).

$insert_query = "INSERT INTO enrties(
datum,
naam Relatie,
ContactPersoon,
bezoekreden)
VALUES (
'$_SESSION[Datum]',
'$_SESSION[RelatieNaam]',
'$_SESSION[ContractPersoon]',
'$_SESSION[redenBezoek]')";

 mysql_query($insert_query);

my thanks in advance.

p.s: i'm using php my admin

EDIT: none of them did the trick, but i solved it because there was a , to much somewhere else >.<

Upvotes: 0

Views: 168

Answers (4)

Frank
Frank

Reputation: 323

you cannot have a field name with space so change naam Relatie to naam_Relatie that might can help you

Upvotes: 0

jayshields
jayshields

Reputation: 405

mysql_error() will probably point you in the right direction as others have said.

Another point to note is that you shouldn't have array elements directly in your strings without enclosing them in curly braces, and field names with spaces in them should be enclosed in backticks.

My best guess for why it is failing though is that you have spelled the table name wrong. It should probably be "entries".

I would try this:

$insert_query = "INSERT INTO `entries` (`datum`,
`naam Relatie`,
`ContactPersoon`,
`bezoekreden`)
VALUES (
'{$_SESSION['Datum']}',
'{$_SESSION['RelatieNaam']}',
'{$_SESSION['ContractPersoon']}',
'{$_SESSION['redenBezoek']}')";

mysql_query($insert_query) or die(mysql_error());

Upvotes: 0

SW4
SW4

Reputation: 71230

$insert_query = "
INSERT INTO enrties
(`datum`,`naam Relatie`,`ContactPersoon`,`bezoekreden`)
VALUES ('$_SESSION[Datum]','$_SESSION[RelatieNaam]','$_SESSION[ContractPersoon]','$_SESSION[redenBezoek]')";

mysql_query($insert_query);

You should wrap field names in ` , and strings in '

Upvotes: 2

Marc B
Marc B

Reputation: 360872

naam Relatie is not a valid field name. Field names must be a single word, or escaped to "hide" the space. Beyond that, fieldnames with spaces in the name are bad practice, and as you can see, are VERY prone to causing just such problems.

Upvotes: 3

Related Questions