I'm making a virtual pets script.
Example of problem:
User 1 creates a pet named "PET".
User 2 creates a pet named "PET".

Should be:
User 1 creates a pet named "PET".
User 2 creates a pet named "PET" but gets an error saying that the name is already taken.

Everything gets inserted into fusion_uberpets_pets.pet_name.

I use $_POST[petname_cancer] as the variable.

If you do not want the duplication then ,
first check in the table if that name already exists

if petname already exists
then tell user to choose someother name

else
insert into database.

it would be great if you can post the relevant code.

Here are the relevant files:
cancer_create_pet.php (the form action)

<?php
require_once "../../../maincore.php";
include "core.php";
if (!preg_match("/^\w+$/", $_POST[petname_cancer])){
pagecreate("Error", "You may only use A-Z and 0-9!");
}
if (empty($_POST[petspecies_cancer]) || empty($_POST[petname_cancer]) || empty($_POST[petcolor_cancer])) { die("All fields must be filled in. Please go back and try again");
}
dbquery("INSERT INTO ".$db_prefix."uberpets_pets (uid, name, color, species)
 VALUES ('".$userdata['user_id']."','".$_POST[petname_cancer]."','".$_POST[petcolor_cancer]."','".$_POST[petspecies_cancer]."')");

redirect("../create_pet.php?step=3");
?>

What I want is if the field "name" already contains the value of $_POST[petname_cancer], it would return an error message.

Ok try this then,

<?php
require_once "../../../maincore.php";
include "core.php";
if (!preg_match("/^\w+$/", $_POST[petname_cancer])){
pagecreate("Error", "You may only use A-Z and 0-9!");
}
if (empty($_POST[petspecies_cancer]) || empty($_POST[petname_cancer]) || empty($_POST[petcolor_cancer])) { die("All fields must be filled in. Please go back and try again");
}

// changes are from here to line 22.


$petnamecheck = $_POST['petname_cancer'];
$check = mysql_query("SELECT name FROM ".$db_prefix."uberpets_pets WHERE name = '$petnamecheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the petname exists it gives an error
if ($check2 != 0) {
die('Sorry, that Petname '.$_POST['petname_cancer'].' is already in use. Choose some other name');
}
else
{



dbquery("INSERT INTO ".$db_prefix."uberpets_pets (uid, name, color, species)
 VALUES ('".$userdata['user_id']."','".$_POST[petname_cancer]."','".$_POST[petcolor_cancer]."','".$_POST[petspecies_cancer]."')");

redirect("../create_pet.php?step=3");
}
?>

Let me know if you have any issues with this.

Thanks! Perfect solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.