dongxianghuan3587 2018-02-07 13:08
浏览 27
已采纳

检查数组是否在DB中有ID更新,否则执行insert

I send input values with same names in array(ID inside) by post as it shown bellow

<form action="" method="POST" role="form" name="formetn" enctype="multipart/form-data">
  <input type="text" name="videolinks[<?php echo $videoid; ?>]" value="<?php echo $videolink; ?>" class="form-control">
</form

Here I am getting array and parse it

if(isset($_POST["hidden_input_name"]) && $_POST["hidden_input_name"]!="") 
{
  $ppp = $_POST['videolinks'];

  $arraylength = count($ppp);     //here i got count of sent items

  $ids = implode(",",array_keys($ppp));         //here I parse keys
  $escaped_values = array_map('mysql_real_escape_string', array_values($ppp));
  $values  = implode(",", $escaped_values);      //here I parse values
}

Now I need to check if i have same ID in my table--> do update of this row
If it is new ID --> do Insert this row

for($i = 0; $i < $arraylength; $i++)
{
  while($row = mysqli_fetch_array($selectvidelink))
  {
    $videoid  = $row["ID"];
  if ($videoid == key($ppp)) 
  {
   //do update of this row
  }
  else
  {
   //do insert of this row
  }
 }
}

I know that I do wrong smth, but do not know what exactly I should change to check array.

  • 写回答

2条回答 默认 最新

  • duangan6636 2018-02-09 22:49
    关注

    <?php
    
    if(isset($_POST["videolinksecret"]) && $_POST["videolinksecret"]!="" && ($_POST["videolinksecret"]) === "baisdbsdvSuvhs7634bHv73bnjonasecret")
    {
    $conn = connect_to_bd();
    mysqli_set_charset($conn,"utf8");
    
    //var_dump($_POST['videolinks']);
    
    $fullarray = $_POST['videolinks'];
    
        foreach($fullarray as $key => $value) 
        {
            $query = "select * from videolink where id=$key";
            $pp = mysqli_query($conn,$query);
            if(mysqli_num_rows($pp)){
                    $dd = "update videolink set `videolink`='$value' where id=$key";
            }
            else{
                    $dd = "insert into videolink(`videolink`) values('$value')";
            }
            $videolinkupdate = mysqli_query($conn,$dd);
            //echo "$key is at $value";
        }
        if($videolinkupdate)
        {
            echo $alertx='<div class="alert alert-success alert-dismissible" id = "successupdate" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Kapat"><span aria-hidden="true">&times;</span></button>
                    <strong>Success!</strong>
                </div>';
        }
            else
        {
            echo $alertx='<div class="alert alert-danger alert-dismissible" id = "successupdate" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Kapat"><span aria-hidden="true">&times;</span></button>
                        <strong>ERROR!</strong> ERROR 03! 
                    </div>';
        }
        
    }

    <form action="" method="POST" role="form" name="formetn" enctype="multipart/form-data">
      <div class="col-sm-9">
        <input type="text" name="videolinks[<?php echo $videoid; ?>]" value="<?php echo $videolink; ?>" class="form-control">
      </div>
      <button type="submit" id="btnformetn" class="btn btn-md btn-success pull-right">Update</button>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?