使用PHP更新mySQL条目无法正常工作

时间:2015-11-24 07:24:32

标签: php mysqli

我正在尝试使用以下命令更新mysql数据库中的字段:

mysqli_query($con,"UPDATE secretSanta SET giverAssigned = '$valueReciever[idsecretSanta]' WHERE idsecretSanta = '$valueGiver[idsecretSanta]';");

我的代码有问题吗? 打印时,这些变量给出正确的数字

更新:添加了代码

include_once("config.php");
session_start();
        error_reporting( E_ALL );
        function passList($con){
        $giverLot = mysqli_query($con,"SELECT idsecretSanta FROM secretSanta  WHERE giftReq = 1 AND giverAssigned= -1 ORDER BY RAND()");
        $recieverLot = mysqli_query($con,"SELECT idsecretSanta, giverAssigned FROM secretSanta  WHERE giftReq = 1");

        if(mysqli_num_rows($giverLot) <=1){
            print("Not enough people are ready or available, no pairing possible. Could be result of a deadlock or everyone is already paired<br>");
        }

        $giver[] =NULL;
        $reciever[] = NULL;

        while($valueReciever = mysqli_fetch_assoc($recieverLot)){
            while($valueGiver= mysqli_fetch_assoc($giverLot)){
                if(($valueReciever[idsecretSanta] != $valueGiver[idsecretSanta]) && ($valueGiver[giverAssigned]<0)){
                    mysqli_query($con,"UPDATE secretSanta SET giverAssigned = '{$valueReciever['idsecretSanta']}' WHERE idsecretSanta = '{$valueGiver['idsecretSanta']}';");
                    print($valueReciever[idsecretSanta] . "was given to " . $valueGiver[idsecretSanta] . "<br>");
                    break;
                }
                else{
                    print("The value is equal <br>");
                    print($valueReciever[idsecretSanta] . " Reciever was equal to Giver " . $valueGiver[idsecretSanta] . "<br>");
                }
            }
            $giverLot = mysqli_query($con,"SELECT idsecretSanta FROM secretSanta  WHERE giftReq = 1 AND giverAssigned = -1 ORDER BY RAND();");
        }

4 个答案:

答案 0 :(得分:0)

query中,您需要使用与数组串联。请尝试以下查询。

mysqli_query($con,"UPDATE secretSanta SET giverAssigned = '".$valueReciever['idsecretSanta']."' WHERE idsecretSanta = '".$valueReciever['idsecretSanta']."'") or die(mysqli_error());

即使此查询也会返回错误。

答案 1 :(得分:0)

mysqli_query($con,"UPDATE secretSanta SET giverAssigned = '$valueReciever[idsecretSanta]' WHERE idsecretSanta = '$valueGiver[idsecretSanta]';");

删除分号并尝试

mysqli_query($con,"UPDATE secretSanta SET giverAssigned = '$valueReciever[idsecretSanta]' WHERE idsecretSanta = '$valueGiver[idsecretSanta]'");

看看你是否真的使用echo()和die()

进入if条件
if(($valueReciever[idsecretSanta] != $valueGiver[idsecretSanta]) && ($valueGiver[giverAssigned]<0)){
    echo "inside if condition";
    die();
                mysqli_query($con,"UPDATE secretSanta SET giverAssigned = '{$valueReciever['idsecretSanta']}' WHERE idsecretSanta = '{$valueGiver['idsecretSanta']}';");
                print($valueReciever[idsecretSanta] . "was given to " . $valueGiver[idsecretSanta] . "<br>");
                break;
}

答案 2 :(得分:0)

您没有正确地使用引号来访问数组元素,并且字符串连接也不正确。试试这个:

 mysqli_query($con,"UPDATE secretSanta SET giverAssigned = '{$valueReciever['idsecretSanta']}' WHERE idsecretSanta = '{$valueGiver['idsecretSanta']}';");

答案 3 :(得分:0)

尝试将变量封装在花括号中,然后在执行之前测试sql看起来没问题。数组中的字段名称也需要引用,因为它们是字符串...

<?php
    error_reporting( E_ALL );
    $sql="UPDATE secretSanta SET giverAssigned = '{$valueReciever['idsecretSanta']}' 
          WHERE idsecretSanta = '{$valueGiver['idsecretSanta']}';"

    echo $sql;/* does it look ok?*/
    #mysqli_query($con,$sql);
?>