i have a list of card names and a php file inserting card. When i inserted one card and deleted it in the database then inserted again. The primary key increase after the card delete. For example:
Card number 1
Card number 2
Card number 3 --> if i delete this value and inserte again the primary key is 4 not 3 how to fix that problem ?
Here is my code
<?php
// this file show card name and picture
include("connect.inc");
$connect=mysqli_connect($host,$username,$password,$dbname) or die ("can't connect to server");
$query="SELECT * FROM dragon ";
$result=mysqli_query($connect,$query) or die("can't execute query");
echo $_SESSION['count'];
echo "<hr/>";
while($row=mysqli_fetch_assoc($result))
{
extract($row);
echo $type."<br/>";
echo $CardName."/";
echo $Description;
echo "<br/>";
echo "<a href='../dragon/{$picture}' border='0'> <img src='../dragon/{$picture}' border='0' width='300' height='300'/></a>";
echo "<hr/>";
}
?>
this file shows the insert form
<?php
$labels=array("type"=>"type",
"CardName"=>"Card Name",
"Description"=>"Description",
"atk"=>"Attack",
"def"=>"Defend",
"picture"=>"picture");
echo "<form action='InsertCard.php' method='POST'>";
echo "<h2>Insert new card </h2>";
foreach($labels as $keys =>$values)
{
echo "$values <input type='text' name='$keys'/><br/>";
}
echo "<input type='submit' value='insert new cards'/>";
echo "<input type='submit' name='return' value='return'/>";
echo "</form>";
?>
this file handle the inserted file
<?php
$labels=array("type"=>"type",
"CardName"=>"Card Name",
"Description"=>"Description",
"atk"=>"Attack",
"def"=>"Defend",
"picture"=>"picture");
if(@isset($_POST['return']))
{
header("Location:ShowCatalog.php");
}
include("connect.inc");
$connect=mysqli_connect($host,$username,$password,$dbname) or die("can't connect to server");
foreach($_POST as $keys =>$values)
{
if(empty($values))
{
if($keys=='type' or $keys=='CardName' or $keys=='Description' or $keys=='picture')
{
$empty_values[]=$keys;
}
}
else
{
if($keys=='type')
{
if(!preg_match("/^[A-Za-z -]{4,15}$/",$values))
{
$invalid_data[]=$keys;
}
}
elseif($keys=='CardName')
{
if(!preg_match("/^[A-Za-z -]{4,30}$/",$values))
{
$invalid_data[]=$keys;
}
}
elseif($keys=='Description')
{
if(!preg_match("/^[A-Za-z., -]{4,255}$/",$values))
{
$invalid_data[]=$keys;
}
}
elseif($keys=="atk" or $keys=="def")
{
if(!preg_match("/^[0-9]{3,5}$/",$values))
{
$invalid_data[]=$keys;
}
}
elseif($keys=='picture')
{
if(!preg_match("/^[A-Za-z -]{4,30}(.jpg)$/",$values))
{
$invalid_data[]=$keys;
}
}
/*else
{
$clean_data[$keys]=trim(strip_tags($values));
}*/
}
}
if(@sizeof($empty_values)>0 or @sizeof($invalid_data)>0)
{
if(@sizeof($empty_values)>0)
{
$join=join(", ",$empty_values);
$msg="You forgot to input: $join<br/>";
echo $msg;
}
if(@sizeof($invalid_data)>0)
{
$join=join(", ",$invalid_data);
$msg="Invalid data: $join";
echo $msg;
}
echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
echo "<h2>Insert new card </h2>";
foreach($labels as $keys =>$values)
{
echo "$values <input type='text' name='$keys'/><br/>";
}
echo "<input type='submit' value='insert new cards'/>";
echo "<input type='submit' name='return' value='return'/>";
echo "</form>";
exit();
}
foreach($_POST as $keys =>$values)
{
$queried_data[$keys]=mysqli_real_escape_string($connect,trim(strip_tags($values)));
}
$check_existence="SELECT CardName FROM dragon WHERE CardName=";
foreach($queried_data as $keys =>$values)
{
if($keys=="CardName")
{
$check_existence.="'".$values."'";
}
}
$checking_result=mysqli_query($connect,$check_existence)or die("can't execute query ".mysqli_error($connect));
if(mysqli_affected_rows($connect)>0)
{
echo "card is already existed !";
include("ShowForm.php");
exit();
}
else
{
$query="INSERT INTO dragon(";
foreach($queried_data as $keys =>$values)
{
$query.=$keys.",";
}
$query.=")";
$query=preg_replace("/\,\)/",")",$query);
$query.="VALUES(";
foreach($queried_data as $keys =>$values)
{
if($keys=="type" or $keys=="CardName" or $keys=="Description")
{
$values=ucfirst($values);
}
if($keys=="atk" or $keys=="def")
{
if(empty($values))
{
$values='n/a';
}
}
$query.="'".$values."',";
}
$query.=")";
$query=preg_replace("/\,\)/",")",$query);
$result=mysqli_query($connect,$query);
echo "card is inserted !";
}
?>