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";
}
?>

$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";
}
?>

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.