检查具有多个选项的列

时间:2015-11-27 13:38:41

标签: php html mysqli operators

我有这个基本脚本检查以确保数据库中尚未进行对话,如果有,它应该获得该对话ID号并将用户发送到该对话,否则处理该信息。我已经过了一半而且完全陷入困境,因为它似乎没有正确检查,因为它只是不断添加相同的对话......

已编辑以包含更多信息:

在脚本启动时,它将起作用并将对话添加到我拥有的两个数据库中:

ap_conversations:
conversation_id | user_one | user_two | time | delete

ap_messages: 
message_id | message | sender_id | time_sent | time_read | conversation_id 

如果在与两个用户的ap_conversations中已经存在对话,我想将用户发送到该对话而不是创建一个全新的对话。虽然脚本不会这样做,只是创建多个对话。

/////////// CREATE NEW CONVERSATION //////////////////////
if(isset($_POST['newmessage'])){
    $to = $_POST['to'];
    $text = $_POST['message'];
    $message = str_replace("'","\\'",$text); 
    $userid = $_SESSION['userid'];

    /// GET OTHER USER ID
$c_id = rand();
$getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT user_id FROM ap_users 
WHERE '$to' LIKE CONCAT(first_name, '%', last_name)"));
$touserID = $getID['user_id'];
if(isset($touserID)){


   /// CHECK CONVO DOESNT EXIST

    $sql = "SELECT * from ap_conversations WHERE user_one = '$userid' AND user_two = '$touserid' OR user_two = '$userid' AND user_one = '$touserid' LIMIT 1";
    $result = $conn->query($sql);
    if($result->num_rows != 1){ 


        mysqli_query($conn,"INSERT INTO `ap_conversations` (`conversation_id`, `user_one`, `user_two`, `time`, `delete`) 
VALUES ('$c_id', '$userid', '$touserID', NOW(), '0');");    

        mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id) 
VALUES ('','$message','$userid', NOW(), '', '$c_id')"); 

        header('Location: messages.php?convoid='.$c_id.'');


      } else {
          $getconid = mysqli_fetch_assoc(mysqli_query($conn, "SELECT conversation_id FROM ap_conversations 
            WHERE user_one = '$userid' AND user_two = '$touserid' OR user_two = '$userid' AND user_one = '$touserid' LIMIT 1"));
          $conid = $getconid['conversation_id'];
        header('Location: messages.php?convo='.$conid.'');
   }
} else {
    header('Location: messages.php?error=2');
   }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

// your code

if($result->num_rows != 1){
    // ...
}else{
    // ...
}

// your code

<强>编辑:

使用以下代码段运行程序,看看你得到了什么。

/////////// CREATE NEW CONVERSATION //////////////////////
if(isset($_POST['newmessage'])){
    $to = $_POST['to'];
    $text = $_POST['message'];
    $message = str_replace("'","\\'",$text); 
    $userid = $_SESSION['userid'];

    /// GET OTHER USER ID
    $c_id = rand();
    $result = $conn->query("SELECT user_id FROM ap_users WHERE '$to' LIKE CONCAT(first_name, '%', last_name)");
    $getID = $result->fetch_assoc();
    $touserID = $getID['user_id'];
    if(isset($touserID) && !empty($touserID)){

        $sql = "SELECT * from ap_conversations WHERE (user_one = '$userid' AND user_two = '$touserid') OR (user_two = '$userid' AND user_one = '$touserid') LIMIT 1";
        $result = $conn->query($sql);
        if ($result->num_rows) {
            // conversation exists
            $result = $conn->query("SELECT conversation_id FROM ap_conversations WHERE (user_one = '$userid' AND user_two = '$touserid') OR (user_two = '$userid' AND user_one = '$touserid') LIMIT 1");
            $getconid = $result->fetch_assoc();
            $conid = $getconid['conversation_id'];
            header('Location: messages.php?convoid=' . $conid);
            exit();
        }else{
            // conversation does not exist
            $conn->query("INSERT INTO `ap_conversations` (`conversation_id`, `user_one`, `user_two`, `time`, `delete`) VALUES ('$c_id', '$userid', '$touserID', NOW(), '0');");
            $conn->query("INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)  VALUES ('','$message','$userid', NOW(), '', '$c_id')");

            header('Location: messages.php?convoid=' . $c_id);
            exit();
        }

    }else{
        header('Location: messages.php?error=2');
        exit();
    }
}

旁注:您混合了mysqli的程序和面向对象样式。

相关问题