Hi I want to check if a record already exist in MySQL database. If it does it shuld echo "It does exist" and if it dont it shuld do nothing.. Now I figured out the following code but it will not work or give me a error message : if(mysql_num_rows(mysql_query("SELECT * FROM engines WHERE keyword = '$GKeyword'"))){ echo "Its there"; } PHP: Can anyone help me?
$exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword'") or die(mysql_error()); if(mysql_num_rows($exists) == 1) { // 1 or 0 and change the echo's around echo "It does exist"; } else { //echo "It doesn\'t exist"; } PHP:
Nice thank you How can I make it stop if it finds a match? I tried exit; but it wouldnt work .. What I am trying to do is to make it not add any new table if it finds a copy of "keyword"
Without seeing your code I have no idea what your doing.... but if its a mysql thing you could add a LIMIT $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword' LIMIT 0, 10") or die(mysql_error()); PHP: Should only get the first 10 results
No I am adding records to the table.. And if the value already exist I dont want it to add anything.. Here is the code: <?php ini_set('display_errors', 1); error_reporting(E_ALL); include("../password_protect.php"); include ("../../connect.php"); $GName = $_POST['name']; $GUrl = $_POST['baseurl']; $GSearchurl = $_POST['searchurl']; $GLanguages = $_POST['languages']; $GKeyword = $_POST['keyword']; $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword'") or die(mysql_error()); if(mysql_num_rows($exists) == 1) { // 1 or 0 and change the echo's around echo "It does exist"; } else { //echo "It doesn\'t exist"; } $write = mysql_query("INSERT INTO engines VALUES ('', '$GName', '$GUrl', '$GSearchurl', '$GLanguages', '$GKeyword')"); echo "<br><br>The following data has been added to the database:<br><br><b>Name:</b> $GName<br><b>URL:</b> $GUrl<br><b>Search URL:</b> $GSearchurl<br><b>Language Code:</b> $GLanguages<br><b>Keyword:</b> $GKeyword<br><br><a href='http://www.gedoo.com/admin/addtable'>Go back</a>"; PHP:
I just moved your $write code under if it does exist so if it doesn't then your code will input it to the database, is that what you wanted ? <?php ini_set('display_errors', 1); error_reporting(E_ALL); include("../password_protect.php"); include ("../../connect.php"); $GName = $_POST['name']; $GUrl = $_POST['baseurl']; $GSearchurl = $_POST['searchurl']; $GLanguages = $_POST['languages']; $GKeyword = $_POST['keyword']; $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword'") or die(mysql_error()); if(mysql_num_rows($exists) == 1) { echo "It does exist"; } else { $write = mysql_query("INSERT INTO engines VALUES ('', '$GName', '$GUrl', '$GSearchurl', '$GLanguages', '$GKeyword')"); echo "<br><br>The following data has been added to the database:<br><br><b>Name:</b> $GName<br><b>URL:</b> $GUrl<br><b>Search URL:</b> $GSearchurl<br><b>Language Code:</b> $GLanguages<br><b>Keyword:</b> $GKeyword<br><br><a href='http://www.gedoo.com/admin/addtable'>Go back</a>"; } PHP:
Make it $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword' LIMIT 1") or die(mysql_error()); because you check if it is = to 1 and if they are more than one results, it will say that it doesn't exists.
Personally, I wouldn't do this with PHP. Let MySQL handle it. Create a unique index on the keyword field, then use 'insert ignore'. If you try to insert a row that has the same keyword, MySQL will quietly discard it .
@human in that case do it this way... <?php ini_set('display_errors', 1); error_reporting(E_ALL); include("../password_protect.php"); include ("../../connect.php"); $GName = $_POST['name']; $GUrl = $_POST['baseurl']; $GSearchurl = $_POST['searchurl']; $GLanguages = $_POST['languages']; $GKeyword = $_POST['keyword']; $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword'") or die(mysql_error()); if(mysql_num_rows($exists) == 0) { $write = mysql_query("INSERT INTO engines VALUES ('', '$GName', '$GUrl', '$GSearchurl', '$GLanguages', '$GKeyword')"); echo "<br><br>The following data has been added to the database:<br><br><b>Name:</b> $GName<br><b>URL:</b> $GUrl<br><b>Search URL:</b> $GSearchurl<br><b>Language Code:</b> $GLanguages<br><b>Keyword:</b> $GKeyword<br><br><a href='http://www.gedoo.com/admin/addtable'>Go back</a>"; } else { echo "The Keyword already exist"; } PHP: