Jump to content

[SOLVED] Preventing the addition of duplicate items to a database


Richzilla

Recommended Posts

Hi All. I'm trying to stop my file upload system adding the same files to my database. The script below is a much simplified version of what I'm using. It's getting frustrating as I can't get this to work.

 

I have a page previosu to this that posts up the details form a previous form. The form and everything works fine apart from the duplicate check.

 

 

<?
$event = @$_POST['event'];
$date = @$_POST['date'];
$flyer = @$_POST['uploaded'];
$user="xxxxx";
$pass="xxxxxx
$host = "xxxxx";
$dbase="xxxxx";
$table = "events";
mysql_connect($host,$user,$pass);
@mysql_select_db($dbase) or die("Unable to select database");
$query = "SELECT FROM events WHERE event ='$event'";
$result = mysql_query($query);

if ($result !== FALSE){
echo "Error : This event is already in the database";

} else {
echo "This is a new event";
}
?>

Link to comment
Share on other sites

<?php

$query = "SELECT FROM events WHERE event ='$event'";
if ($result = mysql_query($query)) {
  if (mysql_num_rows($result)) {
    echo "Error : This event is already in the database";
  } else {
    // not found
}

?>

Link to comment
Share on other sites

$result will always be true regardless of the rows returned. It will only be false if the query fails.

 

You want to use mysql_num_rows()

 

<?
if (mysql_num_rows($result) > 0)
{
   echo "Error: This event is already in the database";
}
else
{
   echo "This is a new event";
}
?>

Link to comment
Share on other sites

thanks for the quick responses, sadly the first response has a syntax error where there's a missing } closing the first statement.

 

The second code causes this error - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

Link to comment
Share on other sites

That type of crap happens when the query is wrong usually.

 

<?php

$query = "SELECT * FROM events WHERE event ='$event'";

if ($result = mysql_query($query) or die(mysql_error()) > 0) {

  if (mysql_num_rows($result)) {

    echo "Error : This event is already in the database";

  } else {

    echo "Not Found";

}

}

?>

 

Fixed the thinkgs u needed try that.

Link to comment
Share on other sites

That type of crap happens when the query is wrong usually.

 

<?php

$query = "SELECT * FROM events WHERE event ='$event'";

if ($result = mysql_query($query) or die(mysql_error()) > 0) {

  if (mysql_num_rows($result)) {

    echo "Error : This event is already in the database";

  } else {

    echo "Not Found";

}

}

?>

 

Fixed the thinkgs u needed try that.

 

 

Top stuff works a treat.

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.