PHP Insert Into没有被执行

时间:2017-01-27 20:27:31

标签: php mysql

即使连接成功并且手动使用数据库上的查询工作,也不会执行插入INTO。 数据库服务器是MySql数据库服务器。

这是我调用函数的地方

    $username = $_SESSION['username'];
if (isset($_POST["btn_addevent"])) {
    $username = $_SESSION['username'];
    $eventname = htmlentities(addslashes($_POST["txt_eventname"]));
    $eventdate = htmlentities(addslashes($_POST["dt_date"]));
    $eventtime = htmlentities(addslashes($_POST["tm_time"]));
    $eventgame = htmlentities(addslashes($_POST["txt_game"]));
    fcn_execSql("INSERT INTO db_Projekt.tbleventstest (dtUserName, dtEventName, dtEventTime, dtEventdate,dtGame)VALUES('$username','$eventname','$eventtime','$eventdate','$eventgame')");
}

这是执行SQL的函数

function fcn_execSql($thissql, $return = "default") {
    //defining the values needed to establish a connection
    if ($_SESSION["isdefined"] == false) {
        define('DB_SERVER', '31.220.59.149');
        define('DB_USERNAME', 'USERNAME');
        define('DB_PASSWORD', 'PASSWORD');
        define('DB_DATABASE', 'db_Projekt');
        $_SESSION["isdefined"] = true;
    }
    //connecting using the values defined before
    $conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    //trying to connect
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    //executing a query and writing its result into a variable
    $result = $conn->query($thissql);
    if ($result == false) {
        $conn->close();
        die;
    }
    if ($return == "JSON") {                                          //query
        $array = mysqli_fetch_row($result);                          //fetch result
        $returnedjson = json_encode($array);
        echo $returnedjson;
    }
    if (strpos($thissql, 'SELECT') !== false) {
        $array = array();
        $i = 0;
        $rowcount = $result->num_rows;
        if ($rowcount > 0) {
            //closing the SQL Connection again
            $conn->close();
            //returning query results
            if ($rowcount > 1) {
                while ($row = mysqli_fetch_assoc($result)) {
                    $array[] = $row;
                }
            } else {
                $row = mysqli_fetch_assoc($result);
                return $row;
            }
            return $array;
        } //returning false in case of a failure and closing the connection
        else {
            $conn->close();
            return false;
        }
    } else {
        $conn->close();
        return true;
    }
}

此功能正在成功使用

if (isset($_POST["btn_register"]) && strlen($_POST["txt_username"]) > 3 && strlen($_POST["txt_password"]) > 3) {
    $username = $_POST["txt_username"];
    $password = md5($_POST["txt_password"]);
    $email = $_POST['txt_email'];
    $activationcode = rand(10000, 99999);
    fcn_execSql("INSERT INTO db_Projekt.tblUsers (idUsername,dtPassword,dtEmail,dtActivationcode)VALUES('$username','$password','$email','$activationcode')");
    sendmail($email, $activationcode, $username);
}

Database structure

1 个答案:

答案 0 :(得分:0)

对我来说没问题。你确定所有这些$ _POST变量都已正确设置吗?

另外,我们可以看到您的数据库结构吗?

修改

周围缺少引号
$eventgame

查询结束时的值。这可能会有所不同。