Jump to content

Recommended Posts

The code below is generating a number of new entries in my tries table, inserting 4 values. When $val contained an integer it worked fine, but I changed it to contain text, and suddenly I'm getting an error. Despite the error, it still inserts the correct amount of new entries in the table. What's going on?

 

Error is:

INSERT into tries (tryOpponent, tryTeam, trySeason, playerID) VALUES ('Wildcats', 'Red', '2006', 'Justin Conway'), ('Wildcats', 'Red', '2006', 'Justin Conway'), ('Wildcats', 'Red', '2006', 'Hamish McGill'),You have an error in your SQL syntax near 'Conway)' at line 1

 

Code is:

$qry = "INSERT into tries (tryOpponent, tryTeam, trySeason, playerID) VALUES ";

$counter =1;

foreach ($_POST['try'] as $key => $val){

$qry .= "('" . $_SESSION['creatematch']['opposition'] . "',

              '" . $_SESSION['creatematch']['ottersteam'] . "',

              '" . $_SESSION['creatematch']['season'] . "',

              '" . $val."'), ";

}

Link to comment
https://forums.phpfreaks.com/topic/70596-mysql-error-but-query-still-working/
Share on other sites

Yes, I changed it to varchar.

 

I've been playing around with the offending code, and now that I've gone back to the original it's stopped working at all! Can anyone see what is going wrong? It's not even inserting new records anymore.

 

$qry = "INSERT into tries (tryOpponent, tryTeam, trySeason, playerID) VALUES ";

$counter =1;

foreach ($_POST['try'] as $key => $val){

$qry .= "('" . $_SESSION['creatematch']['opposition'] . "',

              '" . $_SESSION['creatematch']['ottersteam'] . "',

              '" . $_SESSION['creatematch']['season'] . "',

              '" . $val . "'), ";

}

 

$qry = substr($qry,0,-1);

echo $qry;

$qry = mysql_query($qry);

Here's one I just tried:

INSERT into tries (tryOpponent, tryTeam, trySeason, playerID) VALUES ('Rhinos', 'Red', '2006', 'Phil Adams'), ('Rhinos', 'Red', '2006', 'Phil Adams'), ('Rhinos', 'Red', '2006', 'Raechel Adams'), ('Rhinos', 'Red', '2006', 'Nick Adams'), ('Rhinos', 'Red', '2006', 'Suzie OSullivan'),

 

It ends in a comma - is that a problem? However, the error message says the error is near 'Adams' so I assume that's after the first Phil Adams entry.

 

There are 5 fields in the tries table, the first one is tryID - an auto-incrementing primary key field.

I just changed the substr function so that it doesn't include the final comma and the query is now correctly inserting records. However, I'm still getting the error appearing.

 

New code:

$qry = substr($qry,0,-2);

 

Query and error:

INSERT into tries (tryOpponent, tryTeam, trySeason, playerID) VALUES ('Rhinos', 'Red', '2006', 'Phil Adams'), ('Rhinos', 'Red', '2006', 'Phil Adams'), ('Rhinos', 'Red', '2006', 'Raechel Adams'), ('Rhinos', 'Red', '2006', 'Raechel Adams'), ('Rhinos', 'Red', '2006', 'Nick Adams')

You have an error in your SQL syntax near 'Adams)' at line 1

 

When I copy and paste the query into phpmyadmin it works fine too.

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.