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 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)