Jump to content

MySQL error *help*


Onloac

Recommended Posts

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. :(

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

$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?

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.