php mysql如何检查数据退出然后更新

时间:2016-04-20 17:16:05

标签: php mysql

我有mysql的字段:

enter image description here 用户提交时。我执行测试数据字段顺序是否存在。如果存在,执行at update。如何执行命令更新已经存在一行。谢谢 这是代码HTML:

<form id="students" action="insert" method="post">
                  <div class="row">
                       <input name="order[]" value="1" type="text" class="a1" >
                      <input id="aa" name="a[]" value="Smith" type="text" class="a1" >
                      <input id="bb" name="b[]" value="Alen" type="text" class="b1" >
                      <input id="cc" name="c[]" value="luis" type="text" class="a1" >
                      <input id="dd" name="d[]" value="Kell" type="text" class="a1"  >
                      <input id="ee" name="e[]" value="Keith" type="text" class="b1" >
                  </div>
                  <div class="row">
                      <input name="order[]" value="2" type="text" class="a1" >
                      <input id="aa" name="a[]" value="" type="text" class="a1"  >
                      <input id="bb" name="b[]" value="" type="text" class="b1" >
                      <input id="cc" name="c[]" value="luis" type="text" class="a1" >
                      <input id="dd" name="d[]" value="Kell" type="text" class="a1"  >
                      <input id="ee" name="e[]" value="Keith" type="text" class="b1" >
                  </div>
                  <div class="row">
                      <input name="order[]" value="3" type="text" class="a1" >
                      <input id="aa" name="a[]" value="Bill" type="text" class="a1"   >
                      <input id="bb" name="b[]" value="Mark" type="text" class="b1" >
                      <input id="cc" name="c[]" value="luis" type="text" class="a1" >
                      <input id="dd" name="d[]" value="Kell" type="text" class="a1"  >
                      <input id="ee" name="e[]" value="Keith" type="text" class="b1" >
                  </div>
                  <div class="row">
                      <input name="order[]" value="1" type="text" class="a1" >
                      <input id="aa" name="a[]" value="" type="text" class="a1"  >
                      <input id="bb" name="b[]" value="" type="text" class="b1" >
                      <input id="cc" name="c[]" value="luis" type="text" class="a1" >
                      <input id="dd" name="d[]" value="Kell" type="text" class="a1"  >
                      <input id="ee" name="e[]" value="Keith" type="text" class="b1" >
                  </div>
                  <div class="row">
                      <input name="order[]" value="2" type="text" class="a1" >
                      <input id="aa" name="a[]" value="Kell" type="text" class="a1"  >
                      <input id="bb" name="b[]" value="Keith" type="text" class="b1" >
                      <input id="cc" name="c[]" value="luis" type="text" class="a1" >
                      <input id="dd" name="d[]" value="Kell" type="text" class="a1"  >
                      <input id="ee" name="e[]" value="Keith" type="text" class="b1" >

                  </div>
          <input type="button" value="submit" id="submitbutton" class="insert"  />
     </form>

这是Insert.php

$a=$_POST['a'];
$b=$_POST['b'];
$c=$_POST['c'];
$d=$_POST['d'];
$e=$_POST['e'];
$order=$_POST['order'];
$index=0;

foreach($a as $s){

$check="Select * from tblName where order='".$order[$index]."'";
$result1 = mysql_query($check); 
if (mysql_num_rows($result1) > 0)
{
    //echo "update";
    $sql="Update tblName set col_a='$s',col_b='".$b[$index]."',col_c='".$c[$index]."',col_d='".$d[$index]."',col_e='".$e[$index]."' where id=? ";   //I don't know what is condition where here
    $result = mysql_query($sql);

}
$index++;

2 个答案:

答案 0 :(得分:0)

这不是正确的语法。这只是供您理解。他们俩都这样做。它假设订单栏是唯一的。对于以下示例,订单为1.

INSERT ON DUPLICATE KEY UPDATE

insert into tblName (col_a, col_b, col_c, col_d, col_e, `order`) 
values ('[col-a-value]', '[col-b-value]', '[col-c-value]', '[col-d-value]', '[col-e-value]', 1) 
on duplicate key update col_a='[col-a-value]', col_b='[col-b-value]', col_c='[col-c-value]', col_d='[col-d-value]', col_e='[col-e-value]'

SELECT,INSERT AND UPDATE(这与你的代码类似,有一些修改)

myid = select id from tblName where `order`= 1
if(myid is null - which means new entry) {
    insert into tblName (col_a, col_b, col_c, col_d, col_e, `order`) 
    values ('[col-a-value]', '[col-b-value]', '[col-c-value]', '[col-d-value]', '[col-e-value]', 1) 
} else {
    update tblName 
    set col_a='[col-a-value]', col_b='[col-b-value]', col_c='[col-c-value]', col_d='[col-d-value]', col_e='[col-e-value]'
    where id = myid
}

答案 1 :(得分:-1)

首先,不需要使用FOR循环。你可以简单地使用Select查询,当它发现数据存在时,使用if进行更新,如果不存在则可以执行Insert。