在PHP中插入查询失败

时间:2017-01-23 18:59:59

标签: php

我是PHP的新手,我目前正在使用一个设置操作的模块,其中我将数据从模态插入到数据库中。在获取数据方面没有错误,但是当我点击按钮时,“查询失败:INSERT INTO tbl_mobilvis(位置,时间开始,时间,动员)VALUES('vbv','12:09','17:09' ,'4')“始终显示。

这是db类的代码..     

var $hostname = "REDACTED";
var $username = "REDACTED";
var $password = "REDACTED";
var $database = "REDACTED";

function select_db() {
    $result = mysqli_connect($this->hostname,$this->username,$this->password); 
    if (!mysqli_select_db( $result, $this->database)) {
        echo 'Selection of database: '.$this->database.' failed.';
        return false;
    }
}

function query($query) {
    $result1 = mysqli_connect($this->hostname,$this->username,$this->password); 
    $result = mysqli_query($result1,$query) or die("Query failed: $query<br><br>" . mysql_error());
    return $result;
    mysql_free_result($result);
}

function fetch_array($result) {
    return mysqli_fetch_array($result);
}

function num_rows($result) {
    return mysqli_num_rows($result);
}

function last_insert_id() {
    return mysqli_insert_id();
}

function kill() {
    mysqli_close();
}

} 
?>

这是插入数据的代码。

<?php
if (isset($_POST['btnSubmit'])) {
$db = new mysqldb();
$db->select_db();
$result1 = mysqli_connect("REDACTED","REDACTED","REDACTED");
$sql_mobilvis = sprintf("INSERT INTO tbl_mobilvis (location, timestart, timeend, mobilnum) VALUES ('%s','%s','%s','%s')",
                    mysqli_real_escape_string($result1,$_POST['location']),
                    mysqli_real_escape_string($result1,$_POST['starttime']),
                    mysqli_real_escape_string($result1,$_POST['endtime']),
                    mysqli_real_escape_string($result1,$_POST['mobilnum']));  
$result_user = $db->query($sql_mobilvis);

if ($_POST['fields']){


    $inserted_mobilvis_id = $db->last_insert_id();


    foreach ( $_POST['fields'] as $key=>$value ) {


        $sql_enforcer = sprintf("INSERT INTO tbl_mobilenforcer (enforcername) VALUES ('%s')",
                               mysqli_real_escape_string($result1, $value) );  
        $result_website = $db->query($sql_enforcer);
        $inserted_enforcer_id = $db->last_insert_id();


        $sql_mobil_enforcer = sprintf("INSERT INTO tbl_mobil (mobilid, mobilenforcerid) VALUES ('%s','%s')",
                               mysqli_real_escape_string($result1,$inserted_mobilvis_id),
                               mysqli_real_escape_string($result1,$inserted_enforcer_id) );  
        $result_mobil_enforcer= $db->query($sql_mobil_enforcer);

    }

} else {

    echo "<script>alert('no added enforcers')</script>";

}
    echo "<h1>User Added, <strong>" . count($_POST['fields']) . "</strong> enforcers are added!</h1>";

$db->kill();
}
?>

为什么我收到此查询失败?

0 个答案:

没有答案