我想在MySQL中插入数据,但有些id不在数据库中

时间:2016-06-02 12:55:31

标签: php mysqli

private_messages

enter image description here

message_group

enter image description here

<?php
include("include/connection.php");

include("templates/header.php");
include("templates/navbar.php");

?>
<div class="s_send">

<?php include("templates/contentLeft.php"); ?>

    <div id="send">

        <h1>Private Message System</h1>

        <?php
        include("message_title_bar.php");
        ?>


        <br/>
        <?php



        if(isset($_GET['user']) && !empty($_GET['user'])){
        ?>
        <form method="post">
        <?php




            if(isset($_POST['message']) && !empty($_POST['message'])){
                $user = $_GET['user'];
                $random_number = rand();
                $message = $_POST['message'];
                $my_id = $_SESSION['UserName'];

                $check_con = "SELECT hash FROM message_group WHERE (user_one = '$my_id' AND user_two = '$user') OR (user_one = '$user' AND user_two = '$my_id')";
                $check_con_query = mysqli_query($con, $check_con);
                if(mysqli_num_rows($check_con_query) == 1){

                    echo "<p>Conversation Already Started!</p>";
                } else{

                    $ccc = "INSERT INTO message_group(user_one, user_two, hash) VALUES('$my_id','$user','$random_number')";
                    mysqli_query($con, $ccc);

                    $ddd = "INSERT INTO private_messages(group_hash, from_id, message) VALUE('$random_number', '$my_id', '$message')";
                    mysqli_query($con, $ddd);
                    echo "<p>Conversation Started Now!</p>";
                }
            } 

            ?>

            Enter Message: <br/>
            <textarea name="message" rows="7" cols="60"></textarea>
            <br/>
            <input type="submit" name="" value="send Message" />
            </form> 
        <?php
        }
        else{

            echo "<b>Select User<b>";
            $user_list = "SELECT user_id, u_name FROM registration";
            $user_list_query = mysqli_query($con, $user_list);
            while($run_user = mysqli_fetch_array($user_list_query)){
                $user = $run_user['user_id'];
                $username = $run_user['u_name'];
                echo "<p><a href='send.php?user=$user'>$username</a></p>";
            } 
        }



        ?>

    </div>

<?php include("templates/contentRight.php"); ?>
</div>

<?php include("templates/footer.php"); ?>





<?php
include("include/connection.php");

include("templates/header.php");
include("templates/navbar.php");

?>


<div class="c_con">

<?php include("templates/contentLeft.php"); ?>

    <div id="con">

        <h1>Private Message System</h1>

        <?php include("message_title_bar.php");

              $my_id = $_SESSION['UserName'];
        ?>
        <br/>


        <?php

        if(isset($_GET['hash']) && !empty($_GET['hash'])){

            $hash = $_GET['hash'];
            $message_query = "SELECT from_id, message FROM private_messages WHERE group_hash ='$hash'";
            $message_query_start = mysqli_query($con, $message_query);
            while($run_message = mysqli_fetch_array($message_query_start)){
                $from_id = $run_message['from_id'];
                $message = $run_message['message'];

                    $user_query = "SELECT u_name FROM registration WHERE user_id = '$from_id'";
                    $user_query_start = mysqli_query($con, $user_query);
                    $run_user = mysqli_fetch_array($user_query_start);
                    $from_username = $run_user['u_name'];


            echo "<p><b>$from_username</b><br/>$message</p>";
            }
        ?>
            <br/>
            <form method="post">
            <?php

            if(isset($_POST['message']) && !empty($_POST['message'])){
                $new_message = $_POST['message'];
                $fff = "INSERT INTO private_messages(group_hash, from_id, message) VALUES('$hash', '$my_id', '$new_message')";
                mysqli_query($con, $fff);
                header('location: conversations.php?hash='.$hash);
            }

            ?>
                Enter Message: <br/>
                <input type="text" name="message" style="width:200px; height:100px;" />
                <br/>
                <input type="submit" value="send Message" />
            </form>


        <?php


        } 
        else{
            echo "<b>select conversation: </b>";
            $get_con = "SELECT hash, user_one, user_two FROM message_group WHERE user_one = '$my_id' OR user_two = '$my_id'";
            $get_con_query = mysqli_query($con, $get_con);
            while($run_con = mysqli_fetch_array($get_con_query)){
                $hash = $run_con['hash'];
                $user_one = $run_con['user_one'];
                $user_two = $run_con['user_two'];

                    if($user_one == $my_id){
                        $select_id = $user_two;
                    }
                    else{
                        $select_id = $user_one;
                    }
                $user_get = "SELECT u_name FROM registration WHERE user_id = '$select_id'";
                $user_get_query = mysqli_query($con, $user_get);
                $run_user = mysqli_fetch_array($user_get_query);
                $select_username = $run_user['u_name'];

                echo "<p><a href='conversations.php?hash=$hash'>$select_username</a></p>";
            }
        }

        ?>
    </div>

    <?php include("templates/contentRight.php"); ?>
</div>

2 个答案:

答案 0 :(得分:1)

我不确定这是否有帮助,但您可以使用$my_id打印die($my_id);的值。检查你是否得到任何东西。我注意到的第二件事是:$my_id = $_SESSION['UserName'];假设变量$_SESSION['UserName']包含字符串(因为UserName),你试图将字符串添加到int列(也假设列为0值是int类型)?可能就是这种情况,因为没有数字的字符串等于0。

答案 1 :(得分:0)

问题是

$my_id = $_SESSION['UserName'];

您需要更改SESSION名称

$my_id = $_SESSION['id'];

见图:

enter image description here