phill2000star Posted December 2, 2006 Share Posted December 2, 2006 Hiya all,I am writing a script at the moment that basically checks an email address isn't already stored in a MySQL DB before adding it. I am getting stuck though and can't see why. Can anyone help me??Here's the code.=================================[code]$userExists = false;// open DB connectioninclude '../db/config.php';include '../db/dbOpen.php';$sql = "SELECT id, email FROM tbl_mailing_list WHERE email=$email";$result = mysql_query($sql);if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit;}if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit;}[/code]=================================Now when I submit an email address e.g. user@somedomain.net to the script and an email address already exists, I get the following error.[b]Could not successfully run query (SELECT id, email FROM tbl_mailing_list WHERE email=user@somedomain.net) from DB: 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 '@somedomain.net' at line 1[/b]Now if I was to remove the section of SQL "WHERE email=$email" and replace it with "WHERE email='user@somedomain.net'" then it works perfectly!!Im pulling my last strand of hair out as I type!! Quote Link to comment Share on other sites More sharing options...
artacus Posted December 2, 2006 Share Posted December 2, 2006 quote your strings.[code] "SELECT ... WHERE email='$email'";[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.