【问题标题】:Checking column with multiple options检查具有多个选项的列
【发布时间】:2015-11-27 13:38:40
【问题描述】:

我有这个基本的脚本检查以确保数据库中没有发生对话,如果发生了,它应该获取该对话 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');
   }
}

【问题讨论】:

  • 首先,您使用的是非常不安全的 SQL 查询。请阅读此问题并回答stackoverflow.com/questions/60174/…
  • @ksimka 这不适用于公共站点,因此不必担心太多,尽管一旦我让它工作,我会稍微清理一下。我会阅读那篇文章,谢谢!
  • 1) 请显示完整代码; 2)“不断添加相同的对话......”是什么意思?我的意思是,有什么问题?你能提供一个实际的和预期的结果来显示差异吗?
  • @Snappysites 首先调试您的代码。 if(isset($touserID) && !empty($touserID)){ // echo all your data here },看看有没有遗漏。
  • 你能手动执行你的查询“SELECT * from ap_conversations WHERE user_one = ? AND user_two = ?”已知用户 ID 仍能获得新对话?那里有什么?

标签: php html mysqli operators


【解决方案1】:

试试这个:

// your code

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

// your code

已编辑:

用下面的代码 sn-p 运行你的程序,看看你得到了什么。

/////////// 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的过程和面向对象风格。

【讨论】:

  • @Snappysites 请参阅我的回答的已编辑部分。
  • 我已经使用您的编辑更新了我的代码,但仍然没有运气?我真的不明白为什么这不能正常工作?
  • 是否有其他路线可以让我得到我正在寻找的结果?
  • @Snappysites 哦,那一定是运算符优先级问题。我已经更新了代码。
  • 啊,这似乎工作得很好!太棒了,谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多