PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hello All I am trying to make a code that will look at the database and compare with what has been entered. IF the entered value already equals what is int he database then I would like it to do nothing, but if it is not there, then I would like it to be entered. Also, I would like it NOT to be case sensitive. This is my code so far, which is not working, I believe it is the $sql portion.
That's why I said you need to change up the values to suit your needs, that was just an example. I will try to write out an actual code that will work. but I need to get out of the office right now, so I'll do it either later tonight or have it for you in the morning.
For the life of me I cannot figure this issue out. I keep getting different errors at the 34th row. I think it is simply a missplaced semi-colon, but I can't figure it out. Thanks for your help!
Sorry forgot that part in my last post, had it right in the first post though right? just wasn't supposed to have the quotes around the 0? so it should be ...
$sql2 = "SELECT * FROM Breweries WHERE brewery=$brewery";
$sql3 = if(mysql_num_rows($sql2) == 0) {
mysql_query("INSERT INTO Breweries (brewery) VALUES('$brewery')");
}
#1 - if() statements do NOT have a return. This is a fatal error in PHP.
#2 - You are passing raw SQL to mysql_num_rows(), pass the SQL to mysql_query() and pass the result of that function to mysql_num_rows()
#3 - You are not escaping your input values -- big mysql injection potential
$brewery = mysql_real_escape_string($_POST['brewery']);
//adds breweries to brewery database, but only allows new one.
$query = "SELECT * FROM Breweries WHERE brewery='$brewery'";
$result = mysql_query($query,$con) or die("Error: ".mysql_error()." with query: $query");
if (mysql_num_rows($result) == 0) {
// insert the brewery
$insquery = "INSERT INTO Breweries (brewery) VALUES ('$brewery')";
$insresult = mysql_query($insquery, $con) or die("Error: ".mysql_error()." with query: $insquery");
}