选择auto_increment字段在php中返回空白

时间:2014-04-03 15:25:19

标签: php mysql

我正在开发一个php脚本,它将消息ID(Msg_ID,Ref_ID)存储在相应的用户帐户表中。

我所说的是,Msg_ID已正确写入,但Ref_ID始终为空白。 然而,当我单独运行查询时,它可以工作,但由于某些奇怪的原因,它在脚本中不起作用。

以下是代码:

    $qry = "SELECT Ref_ID FROM Chat WHERE Msg_ID = " .$MsgID. ")";
    $resp = mysqli_query($con, $qry);

    $xx = mysqli_fetch_array($resp);
    $ref_id = $xx['Ref_ID'];

    foreach ($Array as $user){
        $query = "Insert into ".$user."(POST_ID, REF_ID) values ('". $MsgID . "', '" .$ref_id. "')";
        mysqli_query($con, $query);
    }

$ ref_id始终为空,因此空白值将写入相应的数据库。 一些错误的帮助会有所帮助。

以下是完整代码:

<?php
function PostMainThread($Heading, $Message, $Author, $MarkedList){
    $con=mysqli_connect("mysql.serversfree.com", "u521497173_root", "123456", "u521497123_mydb");
    $Array = explode(',', $MarkedList);
    if (mysqli_connect_errno()){
        $response["success"] = 0;
        $response["message"] = "Connection Failed.";
        echo json_encode($response);
    }else{
        here:$MsgID = rand(1, 9999999);
        $query = "Insert into Chat(Msg_ID, Header, MsgBody, Author) values (". $MsgID . "," . "'" . $Heading . "' ," .
            "'" . $Message . "', '". $Author . "')";
        $result=mysqli_query($con, $query);
        if (!$result){
            goto here;
        }else{
            //Put the MsgID in the respective user tables.
            $qry = "SELECT Ref_ID FROM Chat WHERE Msg_ID = " .$MsgID. ")";
            $resp = mysqli_query($con, $qry);

            $xx = mysqli_fetch_array($resp);
            $ref_id = $xx['Ref_ID'];

            foreach ($Array as $user){
                $query = "Insert into ".$user."(POST_ID, REF_ID) values ('". $MsgID . "', '" .$ref_id. "')";
                mysqli_query($con, $query);
            }

            $response["success"] = 1;
            $response["message"] = "Submission successful.";
            mysqli_close($con);
            echo json_encode($response);
        }

    }
}

function PostReplyToThread($PostID, $Author, $Reply){
    $con=mysqli_connect("mysql.serversfree.com", "u521497123_root", "123456", "u521497123_mydb");
    if (mysqli_connect_errno()){
        echo 2;
    }else{
        $query = "Insert into Chat(Msg_ID, Header, MsgBody, Author) values (". $PostID . "," . "'" . " " . "' ," .
            "'" . $Reply . "', '". $Author . "')";
        $result=mysqli_query($con, $query);
        if ($result){
            echo 3;
        }else{
            echo 4;
        }
        mysqli_close($con);
    }
}

if (isset($_POST['what_to_do'])){
    if ($_POST['what_to_do'] == 0){
        if ((isset($_POST['Title'])) &&(isset($_POST['Body']))&&(isset($_POST['Marked']))&&(isset($_POST['_Author']))){
            PostMainThread($_POST['Title'], $_POST['Body'], $_POST['_Author'], $_POST['Marked']);
        }
    }else if ($_POST['what_to_do'] == 1){
        if ((isset($_POST['Thread_ID'])) &&(isset($_POST['Answer']))&&(isset($_POST['_Author']))){
            PostReplyToThread($_POST['Thread_ID'], $_POST['_Author'], $_POST['Answer']);
        }
    }
}else{
    $response["success"] = 0;
    $response["message"] = "Unspecified action";
    echo json_encode($response);
}

聊天表的定义:

Create table Chat(Ref_ID INT Auto_Increment, Msg_ID INT, Header varchar(50), MsgBody varchar(500
), Author varchar(30), Primary Key(Ref_ID, Msg_ID));

1 个答案:

答案 0 :(得分:1)

$xx = mysqli_fetch_array($resp);

只返回数字索引数组,如$ xx [0],$ xx [1]。

要使用列名,您需要使用:

$xx = mysqli_fetch_array($resp, MYSQLI_ASSOC);

或更短的版本:

$xx = mysqli_fetch_assoc($resp);

作为旁注,不要忘记安全性,当插入来自函数外部的数据并且可能有引号或SQL时,它需要被转义!

$Heading = mysqli_real_escape_string($con, $Heading);

否则它会回来咬你。