Jump to content

[SOLVED] mySQL query INSERT not working, and I'm going to go crazy!


Recommended Posts

====================

MY CODE.  I'm trying to insert a record into a table in my database.  It doesn't really get any simpler than this!  But for some reason it's not working no matter what I try, I get the error message below.

====================

 

$sql = "INSERT INTO phonebook (name) VALUES ('Dave')";

 

if (mysql_query($sql)) {

echo("Your record has been added.");

} else {

echo("Error adding submitted record: " . mysql_error());

}

 

 

====================

ERROR MESSAGE (below).  I feel like I've checked the syntax a hundred times.  Can somebody help me?  Thanks!

====================

 

Error adding submitted record: 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 'character (name) VALUES ('Dave')' at line 1

 

 

Link to comment
Share on other sites

Try this:

$sql = "INSERT INTO phonebook (name) VALUES ('Dave')";
$result=mysql_query($sql);
if ($result) {
echo("Your record has been added.");
} else {
echo("Error adding submitted record: " . mysql_error());
}

Link to comment
Share on other sites

Try this:

$sql = "INSERT INTO phonebook (name) VALUES ('Dave')";
$result=mysql_query($sql);
if ($result) {
echo("Your record has been added.");
} else {
echo("Error adding submitted record: " . mysql_error());
}

 

That is exactly the same.

Link to comment
Share on other sites

I'm close to figuring it out!!!  Okay, it DOES have something to do with my database table.  I created a new empty table, didn't do anything fancy to it, and the insert function worked fine!  (I can't believe I spent HOURS on this, dang).

 

So I think the problem with my existing table is in the "auto increment" feature.  In the table, for the "id" row, I have set the following:

 

Type = int

Attributes = UNSIGNED

Extra = auto_increment

 

What else do I need to make this auto increment feature work correctly?  Thanks in advance....

 

 

Link to comment
Share on other sites

normally INSERT queries are like "INSERT INTO table VALUES('','bob','smith')"

 

double single quotes would be the ID you want to increment and bob would be first name field, smith last name field. Thats old school though you might be using a newer method.

Link to comment
Share on other sites

Yeah, I had the same problem with it inserting and getting the same errors.  best thing to do is do the "insert into table (id,name) VALUES ('', '$name')"

 

If that doesn't work, do a manual insert through phpMyAdmin and check the code that is used and just apply it to your script.

Link to comment
Share on other sites

in auto_increment ed fields you doesn't need to enter any values it will get incremented automatically.

Try this

$result=mysql_query($sql) or die(mysql_error());

And make sure is the auto_incremented field Your PRIMARY KEY ??? ??? ??

Link to comment
Share on other sites

It is set as my primary key (I am using phpMyAdmin to set up the database).  But still no luck.  It's strange, I can insert into to the new table (which has no primary key yet) but I still cannot insert into the existing table.

 

Should I just delete the "id" field and add a new one to the table?  Should I make another field my primary?

 

 

Link to comment
Share on other sites

I am going to close this issue because I ended up deleting the table and all its records (like I said, it was driving me crazy).  So I recreated a new table (more carefully this time) and tested it along the way, and it works perfectly!  No idea why the first table caused me so much pain.  Now I've just gotta enter in my old records one by one, which isn't too big of a deal.

 

Thanks for all your help, you guys helped me flesh out my query structure and that's what counts!

 

-S

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.