如何插入…值…其中Value =等于用户给定的值?复杂查询

时间:2019-03-26 17:34:18

标签: php html mysql

我应该插入partnerCode单元格,用户输入的值等于randomfield,并且应该准确插入相反单元格,其中randomfield=partnerCode < / p>

例如:输入的值是 66666 ,它存在于randomfield中并插入到相反的单元格中

   ID      randomfield  partnerCode    
+-------+-------------+-------------+
|   1   |  555555     |    null     |
+-------+-------------+-------------+
|   2   |  666666     |   666666    | 
+-------+-------------+-------------+
|   3   |  777777     |    null     |
+-------+-------------+-------------+

HTML表单代码:

     <form name="Partner" action="../../pages/examples/profile.php" method="POST" >

  <input class="random-but"  name="partnerCode" value="<?php echo $partnerCode; ?>"> 
   <input class="reload-but" type="submit" value=" >>>" name="partnerCode">  



                </form>   

此处为插入代码,但无法正常工作

    if (isset($_POST['partnerCode'])) {
      // receive all input values from the form
       $partnerCode= mysqli_real_escape_string($db, $_POST['partnerCode']);

      $user_check_query = "SELECT * FROM refer WHERE partnerCode='$partnerCode' LIMIT 1";
      $result = mysqli_query($db, $user_check_query);
      $user = mysqli_fetch_assoc($result);



    if (count($errors) == 0) {

     $query = "INSERT INTO refer (randomfield, partnerCode)  
VALUES('$randomfield', '$partnerCode') where 
randomfield='$partnerCode' ";
            mysqli_query($db, $query);  
    }   

1 个答案:

答案 0 :(得分:0)

您应该使用UPDATE

代替插入
    update refer 
    set partnerCode = '$partnerCode' 
    where randomfield='$partnerCode' 

无论如何,您要代替sql和mysqli_real_escape_string中的php var来查看数据库驱动程序,以准备语句和绑定参数