Jump to content

problem searching for email address in table


phill2000star

Recommended Posts

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 connection
include '../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!!
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.