0

I want to insert a result in mysql, i use this query:

$sql= "INSERT into {test} WHERE Id = $currentID (soortstage)VALUES('%s')";

that is the error:

* user warning:

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  `WHERE Id =
(Soortstage)VALUES('132')'` at line 1
query: `INSERT into test WHERE  Id =
(Soortstage)VALUES('132') in
C:\xampp\htdocs\testplanning\modules\planning\planning.module on line 425.`

* warning: 

Invalid argument supplied for
foreach() in
`C:\xampp\htdocs\testplanning\includes\form.inc
on line 1213.`

Have someone a idea? Thanks!

4
  • what's the value of $currentID? Seems to be null. Commented Mar 15, 2011 at 13:19
  • 1
    INSERT INTO WHERE? Do you want an UPDATE or an INSERT? Commented Mar 15, 2011 at 13:21
  • I want to insert values in a created row. the $currentID is the row that I will insert a value to. In these row there are some fields empty and I will just insert 1 value. Commented Mar 15, 2011 at 13:24
  • So you want to UPDATE the row, maybe you should learn the basics of SQL first, you won't regret doing that... Commented Mar 15, 2011 at 14:30

2 Answers 2

1

You might be looking for REPLACE, which will either UPDATE or INSERT based on whether or not the primary ID of the record exists.

REPLACE INTO {test} WHERE Id = $currentID (soortstage) VALUES('%s')

But, frankly, you mention Drupal, which has a nice helper for this: http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_write_record/6

Sign up to request clarification or add additional context in comments.

2 Comments

More importantly, this query should use proper placeholders for $currentID (aka %d), this is not safe. Also, REPLACE is MySQL specific. To be compatible with PostgreSQL, you need to something like api.drupal.org/api/drupal/includes--bootstrap.inc/function/…
Indeed: When @Yannick wants to make a proper, re-usable Drupal-module, the query should be generic and not MySQL-specific. But since he stated he uses MySQL, I thought that to be less important :)
0

You need an update query

$sql= "UPDATE {test} SET soortstage = '%s' WHERE Id = $currentID ";

2 Comments

almost good solution, but it gives this error: * user warning: 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 '' at line 1 query: UPDATE test SET Soortstage = '162' WHERE Id = in C:\xampp\htdocs\testplanning\modules\planning\planning.module on line 426. * warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\testplanning\includes\form.inc on line 1213.
it is because $currentID does not have any value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.