Hello. I have the following script that adds data to a mysql table :
<?php
#connects to the MySQL server
$username = "my_username";
$password = "my_password";
$host = "localhost";
$database = "my_database";
$table = "my_table";
mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$id=$_GET['id'];
$category=escapeshellcmd($_POST['category']);
$query = mysql_query("SELECT category FROM $table WHERE category=$category");
$check = mysql_fetch_array($query);
if(!$_POST) {
echo "<form method='post' action='add.php'>";
echo "Category :<br><input type=='text' size='64' name='category'<br>";
echo "<input type='submit' value='Add'>";
echo "<input type='button' onclick='history.go(-1)' value='Cancel'>";
echo "</form>";
}
elseif ($query==$category) {
echo "<a href='#' onclick='history.go(-1)'>Duplicate entry not allowed!</a><br>";
}
else {
$insert = "INSERT INTO $table SET category='$category'";
$query = mysql_query($insert) or die(mysql_error());
echo "Post added<br>";
echo "<br><a href='admin.php'>To mainpage</a>";
}
?>
I'm trying to get my script to check wether the category name already exist or not in the table. If the name already exists I want the script to choose another name because duplicate entry is not allowed. But I can't get it to work. Any suggestions are greatly apreciated ?