1

I have this query:

$FullName = mysql_real_escape_string($_REQUEST['name']);
$EmailAdd = mysql_real_escape_string($_REQUEST['email_address']);
$City = mysql_real_escape_string($_REQUEST['city']);
$State = mysql_real_escape_string($_REQUEST['state']);

$SqlEInsert= "INSERT INTO `td_email` VALUES ((SELECT ownerid FROM 'td_events' where event_id = '$EvID'),'$EmailAdd','$FullName', '$City'  ,'$State')";

$RsEmail = mysql_query($SqlEInsert) or die('Error :' . mysql_error());

but I'm getting the following error when I run the application

Error :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 ''td_events' where event_id = '394'),'[email protected]','Full Name', 'Atl' at line 1

3
  • Why not select the values first, then insert them? You're trying too much with that query. Commented Jul 1, 2012 at 16:04
  • Change 'td_events' to `td_events` Commented Jul 1, 2012 at 16:08
  • 1
    Please use MySQLi or PDO; The MySQL extension is deprecated and should no longer be used. Commented Jul 1, 2012 at 16:08

3 Answers 3

1

You don't need ' for the table name when you want to use quotes then you have to use `

$SqlEInsert= "INSERT INTO td_email VALUES ((SELECT ownerid FROM td_events WHERE event_id = '$EvID'),'$EmailAdd','$FullName', '$City'  ,'$State')";

And please take a look at SQL Injections and Security

$SqlEInsert= "INSERT INTO td_email VALUES ((SELECT ownerid FROM td_events WHERE event_id = '".(int)$EvID."'),'".mysql_real_escape_string($EmailAdd)."','".mysql_real_escape_string($FullName)."', '".mysql_real_escape_string($City)."'  ,'".mysql_real_escape_string($State)."')";
Sign up to request clarification or add additional context in comments.

Comments

1

The td_event is a field name rather than a value. Escape it with an apostrophe.

$SqlEInsert= "INSERT INTO `td_email` VALUES ((SELECT ownerid FROM `td_events` where event_id = '$EvID'),'$EmailAdd','$FullName', '$City'  ,'$State')";

Comments

0

Make sure your values are escaped. You can run them through: mysql_real_escape_string() to do so.

Comments

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.