Jump to content

[SOLVED] if statement and mysql queries


Recommended Posts

I am trying to have a database searched for an entry that matches an email address typed in a textbox, and if no entries matching the entered email is found to then add it to the database

 

<?php

mysql_connect("localhost","root","");

mysql_select_db("test");
$q=mysql_query("select * from emails where Email like '$_POST[email]';");
$num=mysql_num_rows($q);

if ($num=0)
{
mysql_query ("INSERT INTO emails values('','$_POST[email]')");
echo "<script language=JavaScript>window.location='add.php'</script>";
}
else
{
echo "Entry already exists";
}

mysql_close();
?>

Link to comment
https://forums.phpfreaks.com/topic/104626-solved-if-statement-and-mysql-queries/
Share on other sites

<?php

mysql_connect("localhost","root","");
mysql_select_db("test");

$query = mysql_query("select * from emails where Email like '$_POST[email]';");
$num = mysql_num_rows($q);

# Here's the bit. = should be == (see below)..
if ($num == 0)
{
mysql_query ("INSERT INTO emails values('','$_POST[email]')");
echo "<script language=JavaScript>window.location='add.php'</script>";
}
else
{
echo "Entry already exists";
}

mysql_close();
?>

 

changed = to ==.

 

= sets a value, == checks a value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.