PHP bind_param和Decimal值无效

时间:2017-09-07 04:27:43

标签: php mysql

我正在尝试创建一个PHP脚本来在MySql表中存储一些值。

表格如下:

CREATE TABLE IF NOT EXISTS `KeyPad` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `phone1` bigint(11) NOT NULL,
 `concept1` double(11,4) NOT NULL,
 `phone2` bigint(11) NOT NULL,
 `concept2` double(11,4) NOT NULL,
 `phone3` bigint(11) NOT NULL,
 `concept3` double(11,4) NOT NULL,
 `phone4` bigint(11) NOT NULL,
 `concept4` decimal(11,4) NOT NULL,
 `phone5` bigint(11) NOT NULL,
 `concept5` double(11,4) NOT NULL,
 `phone6` bigint(11) NOT NULL,
 `concept6` double(11,4) NOT NULL,
 UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

我的PHP脚本是这样的:

CONFIG.PHP:

<?php
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'hostname');
define('DB_NAME', 'DB_name');

DbConnect.php:

<?php

class DbConnect
{
    private $conn;

    function __construct()
    {
    }
    function connect()
    {
        require_once 'config.php';
        $this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

        // Check for database connection error
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        // returing connection resource
        return $this->conn;
    }

DbOperation.php:

<?php

class DbOperation
{
    private $conn;

    //Constructor
    function __construct()
    {
        require_once dirname(__FILE__) . '/Config.php';
        require_once dirname(__FILE__) . '/DbConnect.php';
        // opening db connection
        $db = new DbConnect();
        $this->conn = $db->connect();
    }

    //Function to create a new user
    public function createentry($P1, $C1, $P2, $C2, $P3, $C3, $P4, $C4, $P5, $C5, $P6, $C6)
    {
        $stmt = $this->conn->prepare("INSERT INTO KeyPad(phone1, concept1, phone2, concept2, phone3, concept3, phone4, concept4, phone5, concept5, phone6, concept6) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("idididididid", $P1, $C1, $P2, $C2, $P3, $C3, $P4, $C4, $P5, $C5, $P6, $C6);
        $result = $stmt->execute();
        $stmt->close();
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

}

最后,createentry.php:

<?php

//creating response array
$response = array();

if($_SERVER['REQUEST_METHOD']=='POST'){

    //getting values
    $ph1 = $_POST['phone1'];
    $co1 = $_POST['concept1'];
    $ph2 = $_POST['phone2'];
    $co2 = $_POST['concept2'];
    $ph3 = $_POST['phone3'];
    $co3 = $_POST['concept3'];
    $ph4 = $_POST['phone4'];
    $co4 = $_POST['concept4'];
    $ph5 = $_POST['phone5'];
    $co5 = $_POST['concept5'];
    $ph6 = $_POST['phone6'];
    $co6 = $_POST['concept6'];

    //including the db operation file
    require_once '../includes/DbOperation.php';

    $db = new DbOperation();

    //inserting values
    if($db->createentry($ph1, $co1, $ph2, $co2, $ph3, $co3, $ph4, $co4, $ph5, $co5, $ph6, $co6)){
        $response['error']=false;
        $response['message']='Team added successfully';
    }else{

        $response['error']=true;
        $response['message']='Could not add team';
    }

}else{
    $response['error']=true;
    $response['message']='You are not authorized';
}
echo json_encode($response);

我不确定我正在做的部分:

$stmt->bind_param("idididididid",

是否正确。因为,当我在邮递员上运行时,我收到一个错误说:

"error":true,"message":"Could not add team"

2 个答案:

答案 0 :(得分:0)

我建议您在变量中编写此SQL查询的输出并进行记录。 然后你可以在你最喜欢的MySQL客户端上执行它,看看错误是什么。 在你的DbOperation.php中,添加:

$log = "INSERT INTO KeyPad(phone1, concept1, phone2, concept2, phone3, concept3, phone4, concept4, phone5, concept5, phone6, concept6) values($P1, $C1, $P2, $C2, $P3, $C3, $P4, $C4, $P5, $C5, $P6, $C6);";
error_log($log, 3, "/path/to/your/error.log");

运行您的脚本,然后日志将包含您可以手动运行的请求。 通过这种方式,您将能够看到数据库抱怨的内容。

答案 1 :(得分:0)

解决: 我意识到我是从Postman发送参数而我需要通过&#34; body&#34;发送它们。