如果第一行不为空,如何将值插入第二行? 数据库

I should insert value to parnerCode2 if parnerCodeOwner1 already taken value;

Below code only inserts to parnerCodeOwner1 if new values comes it rewrites first one, It shouldn't rewritten, value should inserted to parnerCode2

<?php
if (isset($_POST['partnerCode'])) {
    // receive all input values from the form
    $partnerCode= mysqli_real_escape_string($db, $_POST['partnerCode']);
    if (empty($partnerCode)) { 
        array_push($errors, "CODE required"); 
    }

    $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 = "update refer set partnerCodeOwner1= '" . $_SESSION['username'] . "'  where randomfield='$partnerCode'  ";
        mysqli_query($db, $query);
    }   
}
?>

ID       randomfield   parnerCode2  parnerCodeOwner1
+-------+-------------+-------------+----------------+
|   1   | uOxerUg4    |             | BuBI9vk        |
+-------+-------------+-------------+----------------+
|   2   | cZzuJlcs    |             | uOxerUg4       |
+-------+-------------+-------------+----------------+
|   3   | BuBI9vke    |             | cZzuJlcs       |
+-------+-------------+-------------+----------------+
dragon071111
dragon071111 比赛条件是一个很好的起点,你是对的。但是,只要我读取给定的代码和表结构,定义列是否在PHP代码中使用的partnerCode或者表转储中给出的parnerCode2是另一个应该首先解决的问题
一年多之前 回复
dqzve68846
dqzve68846 那么@NicoHaase竞争条件可能呢?理想情况下,如果可能,topicstarter应将这两个查询合并为一个。
一年多之前 回复
dongqiaolong9034
dongqiaolong9034 调整代码首先检查然后运行UPDATE或INSERT查询有什么问题?
一年多之前 回复

1个回答



您使用以下代码开始了正确的轨道:</ p>

  $ user_check_query =“  SELECT * FROM引用WHERE partnerCode ='$ partnerCode'LIMIT 1“; 
$ result = mysqli_query($ db,$ user_check_query);
$ user = mysqli_fetch_assoc($ result);
</ code> </ pre>

您真正想要的是获取属于用户的行,而不是合作伙伴。 所以,你的查询应该是:</ p>

  $ user_check_query =“SELECT partnerCode1 FROM refer WHERE randomField ='”。$ _ SESSION ['username']。“'LIMIT 1”; \  n </ code> </ pre> 

获得$ user行... </ p>

  if(empty($ user ['partnerCode1'  ]))
</ code> </ pre>

如果是,请将partnerCode插入partnerCode1。 否则,将其插入partnerCode2。</ p>

此外,如果出现错误,则无需获取行进行检查。 因此,所有代码都应该在检查错误的if语句之后。</ p>
</ div>

展开原文

原文

You started on the right track with this code:

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

What you actually want is to grab the row that belongs to the user, not the partner. So, your query should be:

$user_check_query = "SELECT partnerCode1 FROM refer WHERE randomField='".$_SESSION['username']."' LIMIT 1";

Once you get the $user row...

if(empty($user['partnerCode1']))

If that is true, insert the partnerCode into partnerCode1. Else, insert it into partnerCode2.

Also, there is no need to fetch the row to check it if there is an error. So, all of that code should come after the if statement that checks for errors.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问