0
mysql_query("INSERT INTO data(timestamp,type,lift,sets,reps,weight) VALUES(".$time.",".$_POST[type][$i].",".$_POST[lift][$i].",".$_POST[sets][$i].",".$_POST[reps][$i].",".$_POST[weight][$i].")")or die(mysql_error());

My code is as above. All the variables are set.

The error I am getting is

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 'Standing DB Hammer Curls,1,2,3)' at line 1

There is seemingly no reason for this.

Could someone clarifty on the problem, and ideally explain why multi dimensional arrays are so particular as far as what they need to be enclosed between etc.

Cheers

1 Answer 1

1

Are you adding ' single quotes around the variables your inserting?

This will fail

$t = "Test";
mysql_query("INSERT INTO data(mystring) VALUES (" . $t . ")");

This will work

$t = "Test";
mysql_query("INSERT INTO data(mystring) VALUES ('" . $t . "')");

Remember to defend against MySql Injection

Edit: Thanks Eric

Yes you should use something like mysql_real_escape_string() to escape the single quotes that might be in your POSTd variables.

Edit: About Injection

Briefly....because you need to wrap strings in ' (single quotes) if a ' is typed into a form and then submitted then it can break the insert or update code.

If the following is entered into a form "O'Brien" then the MySql code would be

INSERT INTO data(mystring) VALUES ('O'Brien')

Which is of course incorrect, such things can be exploited and used to gain entry to a web site. This was exploited world wide recently

4
  • 1
    To add to Jake's reply, to defend against MySQL Injection, you'll want to escape any quotes in the data you're inserting. Commented Apr 9, 2011 at 20:37
  • Many Thanks. Would greatly appreciate it if you could clarify as to why exactly such is needed? Cheers Commented Apr 9, 2011 at 20:47
  • Thomas, I have edited with some more info, Google it though, there is a lot out there. Commented Apr 9, 2011 at 20:59
  • My pleasure. Good answer to the OP. Commented Apr 9, 2011 at 23:12

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.