Onloac Posted September 16, 2009 Share Posted September 16, 2009 I've been trying to figure this out and I'm still somewhat new to this. I have alot of information submitted to my site and I need away to stop duplicates. The information I start with is article title, body, timestamp, source, and link. Now before I add it to the database I wanna check to see if it already exists. This is the code I use. $query = $db->query("SELECT * FROM myarticles WHERE link=".$link."") or die("Could not execute link lookup."); $check_link = mysql_num_rows($query); if($check_link > 0){ echo "Exists - $title<br>"; } else { $db->query("INSERT INTO myarticles (id, title, description, date, source, link) VALUES('', '$title', '$description', '$date', '$source', '$link')") or die(mysql_error()); echo "Added - $title<br>"; } I wanted to check to see if the title exists but I couldn't figure out away around the quotes within the string and it always caused errrors. If someone could help me do that I would appreciate it cause I've checked google and tutorial sites with no luck. Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/ Share on other sites More sharing options...
Onloac Posted September 16, 2009 Author Share Posted September 16, 2009 By the way with the above code I'm not looking for the title like I originally wanted, instead I'm looking for the article link. At the moment with the above code I get this error. SQL Error: 1064 - 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 '://www.mysite.com/article.php?id=24' at line 1 Query: SELECT * FROM myarticles WHERE link=http://www.mysite.com/article.php?id=24 What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/#findComment-919762 Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 There are no quotes around the 'link'; $query = $db->query("SELECT * FROM myarticles WHERE link='".$link."'") or die("Could not execute link lookup."); Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/#findComment-919766 Share on other sites More sharing options...
Maq Posted September 16, 2009 Share Posted September 16, 2009 You also insert '' for id every time. If that field is auto-increment it will throw an error. Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/#findComment-919767 Share on other sites More sharing options...
Onloac Posted September 16, 2009 Author Share Posted September 16, 2009 Alright that seemed to fixed the errors. But for some reason it still doesn't detect an article is already in the database. What am I doing wrong? Shouldn't that code check to see if the url is already there? Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/#findComment-919769 Share on other sites More sharing options...
Onloac Posted September 17, 2009 Author Share Posted September 17, 2009 Does anyone have any idea what I'm doing wrong? Regardless if the article URL exists in the database its still being added. I'm trying to use the above code to check and see if the URL of the submitted article already exists in the database.... if it doesn't exist I want it added, if it does I want it to say it already exists. I've searched this forum, google, and a couple other sites with no luck. Whats wrong with my method? Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/#findComment-919932 Share on other sites More sharing options...
gevans Posted September 17, 2009 Share Posted September 17, 2009 What do you get on screen when the article exists? Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/#findComment-919959 Share on other sites More sharing options...
Onloac Posted September 17, 2009 Author Share Posted September 17, 2009 All I get is "Added". When I attempt to add an article it says "Added" even if it exists. It seems like no matter what I do it always says "Added" and never seems to make a match and notify me that it exists. Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/#findComment-919967 Share on other sites More sharing options...
gevans Posted September 17, 2009 Share Posted September 17, 2009 $db->query("INSERT INTO myarticles (id, title, description, date, source, link) VALUES('', '$title', '$description', '$date', '$source', '$link')"); As mentioned by Maq; You also insert '' for id every time. If that field is auto-increment it will throw an error. try doing; $db->query("INSERT INTO myarticles (title, description, date, source, link) VALUES('$title', '$description', '$date', '$source', '$link')"); Also the or die() doesn't work here as you're using a method not a mysql resource/function. What does the method return on success/failure? Quote Link to comment https://forums.phpfreaks.com/topic/174509-mysql-error-help/#findComment-919978 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.