我需要自动在php中增加数据库中的id

时间:2018-01-24 09:18:44

标签: php html increment

添加相册页面代码

这里我有3个名为name,sensor和orderid的字段,这里我需要填写所有字段。填写后我保存了这些详细数据库。

在数据库中添加数据后,它将转到cameralist.php文件并使用编辑和删除按钮显示名称。

我的问题是我想通过cameralist.php页面中的订单ID订购数据库。考虑第一个记录名是sony,传感器名称是sony,orderid变为1.现在我要插入新的记录名称是canon而传感器是canon而id是1.如果我输入相同的id,sony的id应该增加到2 ,

这是否可行,请提出建议

<?php
include('db.php');
if(isset($_POST['submit']))
{
     //echo json_encode($_POST);



    foreach($_POST as $key=>$value)
    {
        if($key!="submit"){

        $names[]=$key;
        $val[]= "'".$value."'";
        }
        //$names=mysqli_real_escape_string($db,$value);
    }
    $output=implode(",",(array)($names));
    $v=implode(",",(array)($val));


$query="INSERT INTO `camera details` ($output)
                                        VALUES($v)";

$result=mysqli_query($db,$query) or die("OrderID Already Exists Add A new One");
    if($result)
    {

    header('location:cameralist.php');

    }
}
else {
?>



<div class="form">
<form method="POST" action="">
<?php

include('db.php');

$col="SHOW COLUMNS FROM `camera details`";
$output=mysqli_query($db,$col);

$kal=array();
while($row=mysqli_fetch_array($output))
{
    if($row['Field']!='id')
    {
        ?><div class="values"><?php echo $row['Field'];
        echo "<br>";?></div><br><br><?php
        array_push($kal, $row['Field']);
    }
}
?>
<div class="input">
<?php

foreach($kal as $key=>$value){
?>

<input type="text" name="<?php echo $value;?>" required /><br><br>
    <?php  } ?>




<br><input type="submit" name="submit" value="SUBMIT">

<?php 

?>
<br>

</div>
<br><br>
</form>
<?php } ?>

cameralist.php文件代码

<?php

include('db.php');
$sql="SHOW COLUMNS FROM `camera details`";
$n=mysqli_query($db,$sql);
$kalai=array();

while($k=mysqli_fetch_array($n))
{
    if($k['Field']!='id')
    {
        array_push($kalai,$k['Field']);
    }
}

include('db.php');
$query="SELECT * FROM `camera details` ORDER BY OrderNo ";

$result=mysqli_query($db,$query);

while($row=mysqli_fetch_assoc($result))
{?>
<tr><td><div class="values"><?php echo $row["$kalai[0]"]; ?></td></div>
<td><div class="edit"><a href="editcameradetails.php?id=<?php echo $row["id"] ?>">MODIFY</a></td></div>
<td><div class="delete"><a href="deletecameradetails.php?id=<?php echo $row['id'] ?>"> DELETE</a></td></div></tr>
<br><br><br>
<?php }  ?>

1 个答案:

答案 0 :(得分:0)

Simply use DESC so that new records appear in the top of the list you are displaying

 $query="SELECT * FROM `camera details` ORDER BY OrderNo DESC ";

by default its ASC