Note that there are some explanatory texts on larger screens.

plurals
  1. POprimary key increase not orderly
    text
    copied!<p>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:</p> <pre><code>Card number 1 Card number 2 Card number 3 --&gt; if i delete this value and inserte again the primary key is 4 not 3 how to fix that problem ? </code></pre> <p>Here is my code </p> <pre><code> &lt;?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 "&lt;hr/&gt;"; while($row=mysqli_fetch_assoc($result)) { extract($row); echo $type."&lt;br/&gt;"; echo $CardName."/"; echo $Description; echo "&lt;br/&gt;"; echo "&lt;a href='../dragon/{$picture}' border='0'&gt; &lt;img src='../dragon/{$picture}' border='0' width='300' height='300'/&gt;&lt;/a&gt;"; echo "&lt;hr/&gt;"; } ?&gt; </code></pre> <p>this file shows the insert form</p> <pre><code>&lt;?php $labels=array("type"=&gt;"type", "CardName"=&gt;"Card Name", "Description"=&gt;"Description", "atk"=&gt;"Attack", "def"=&gt;"Defend", "picture"=&gt;"picture"); echo "&lt;form action='InsertCard.php' method='POST'&gt;"; echo "&lt;h2&gt;Insert new card &lt;/h2&gt;"; foreach($labels as $keys =&gt;$values) { echo "$values &lt;input type='text' name='$keys'/&gt;&lt;br/&gt;"; } echo "&lt;input type='submit' value='insert new cards'/&gt;"; echo "&lt;input type='submit' name='return' value='return'/&gt;"; echo "&lt;/form&gt;"; ?&gt; </code></pre> <p>this file handle the inserted file</p> <pre><code>&lt;?php $labels=array("type"=&gt;"type", "CardName"=&gt;"Card Name", "Description"=&gt;"Description", "atk"=&gt;"Attack", "def"=&gt;"Defend", "picture"=&gt;"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 =&gt;$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)&gt;0 or @sizeof($invalid_data)&gt;0) { if(@sizeof($empty_values)&gt;0) { $join=join(", ",$empty_values); $msg="You forgot to input: $join&lt;br/&gt;"; echo $msg; } if(@sizeof($invalid_data)&gt;0) { $join=join(", ",$invalid_data); $msg="Invalid data: $join"; echo $msg; } echo "&lt;form action='$_SERVER[PHP_SELF]' method='POST'&gt;"; echo "&lt;h2&gt;Insert new card &lt;/h2&gt;"; foreach($labels as $keys =&gt;$values) { echo "$values &lt;input type='text' name='$keys'/&gt;&lt;br/&gt;"; } echo "&lt;input type='submit' value='insert new cards'/&gt;"; echo "&lt;input type='submit' name='return' value='return'/&gt;"; echo "&lt;/form&gt;"; exit(); } foreach($_POST as $keys =&gt;$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 =&gt;$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)&gt;0) { echo "card is already existed !"; include("ShowForm.php"); exit(); } else { $query="INSERT INTO dragon("; foreach($queried_data as $keys =&gt;$values) { $query.=$keys.","; } $query.=")"; $query=preg_replace("/\,\)/",")",$query); $query.="VALUES("; foreach($queried_data as $keys =&gt;$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 !"; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload