what is wrong with this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
verymeanguy
Forum Newbie
Posts: 6
Joined: Sat Jul 19, 2008 5:40 am

what is wrong with this

Post by verymeanguy »

I have an SQL code (but it is a part of my php code) that can not be executed by mysql. When I printed it with

Code: Select all

print $sql;
It goes like this:

Code: Select all

INSERT INTO "lunch" ("id", "title", "name", "text") VALUES ("","fourth test","Jason","this is the fourth test testing from an html form.");
It seems to be syntactically correct, and the field id is to be autoincremented. Why can't it be executed?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: what is wrong with this

Post by JAB Creations »

I've really been making a lot of progress with MySQL in the past week and I'm pretty sure what you want to do is along the lines of this...

Code: Select all

$result = mysql_query("INSERT INTO "lunch" ("id", "title", "name", "text") VALUES ("","fourth test","Jason","this is the fourth test testing from an html form.");");
 if (!$result) {$_SESSION['error'] = mysql_error(); header("location:this_file_name.php?step=2&error");}
 else {echo 'check your DB in PhpMyAdmin, the query appears to have executed just fine.';}
It will execute the $result variable automatically (if it's nested in something else then those conditions will have to be met as well obviously) and it essentially checks for true/false. It's worked beautifully for me, let me know if this is what you need please.

*edit* - PS - If you disable error reporting you can still manually report any MySQL errors by echoing the session variable 'error'. I find this very useful as I code everything as actual XHTML.
verymeanguy
Forum Newbie
Posts: 6
Joined: Sat Jul 19, 2008 5:40 am

Re: what is wrong with this

Post by verymeanguy »

Thank you for replying. But what I need to know is, why doesn't my SQL statement work on MySQL? Why can't it get executed when it seems to be syntactically correct. Can you tell me why?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: what is wrong with this

Post by Benjamin »

Remove the id field completely from the query.
Post Reply