简体   繁体   English

PHP bind_param和Decimal值不起作用

[英]PHP bind_param and Decimal values not working

I am trying to create a PHP script to store some values in MySql table. 我试图创建一个PHP脚本来在MySql表中存储一些值。

The table looks like this: 该表如下所示:

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 ;

My PHP scripts are like this: 我的PHP脚本是这样的:

Config.php: config.php文件:

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

DbConnect.php: 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: 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;
        }
    }

}

Lastly, createentry.php: 最后,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);

I am not sure if the part where i am doing: 我不确定我在做什么:

$stmt->bind_param("idididididid",

is correct or not. 是否正确。 Because, when I run this on postman, i get an error saying: 因为,当我在邮递员上运行此程序时,出现错误消息:

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

I would suggest that you write the output of this SQL query in a variable and log it. 我建议您将此SQL查询的输出写入变量并记录下来。 Then you can execute it on your favorite MySQL client see what the error is. 然后,您可以在自己喜欢的MySQL客户端上执行它,看看错误是什么。 in your DbOperation.php, add this : 在您的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");

Run your script and then the log will contain the request you can run manually. 运行您的脚本,然后日志将包含您可以手动运行的请求。 That way you will be able to see what the db is complaining about. 这样,您将能够查看数据库在抱怨什么。

解决:我意识到我是从邮递员发送参数的,而我需要通过“正文”发送它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM