gpt4 book ai didi

php - 使用 PDO 语句未将数据插入 mysql

转载 作者:行者123 更新时间:2023-11-30 00:03:33 25 4
gpt4 key购买 nike

我在使用 PDO 语句插入时遇到问题。我正在从 mysql 转向 PDO 语句。所以欢迎您提出意见。这根本不是一个重复的问题。我检查了其他问题,但无法得到结果。因此,请不要将其标记为重复问题。

<?php
class Stock{
const DB_HOST = 'localhost';
const DB_NAME = 'stock';
const DB_USER = 'root';
const DB_PASSWORD = '';

private $conn = null;



/**
* Open the database connection
*/
public function __construct(){
// open database connection
$connectionString = sprintf("mysql:host=%s;dbname=%s",
Stock::DB_HOST,
Stock::DB_NAME);
try{
$this->conn = new PDO($connectionString,
Stock::DB_USER,
Stock::DB_PASSWORD);
}
catch(PDOException $e){
die($e->getMessage());
}
}//end of constructor

public function insert_stock($stock_symbol,$stock_name,$no_of_trans,$max_price,$min_price,$closing_price,$total_shares,$amount,$previous_close,$difference){
$data = array(
':stock_symbol' => $stock_symbol,
':stock_name' => $stock_name,
':no_of_trans' => $no_of_trans,
':max_price' => $max_price,
':min_price' => $min_price,
':closing_price' => $closing_price,
':total_shares' => $total_shares,
':amount' => $amount,
':previous_close' => $previous_close,
':difference' => $difference
);
$sql = 'INSERT INTO tbl_stock(stock_symbol,stock_name,no_of_trans,max_price,min_price.closing_price,total_shares,amount,previous_close,difference)
VALUES(:stock_symbol,:stock_name,:no_of_trans,:max_price,:min_price,:closing_price,:total_shares,:amount,:previous_close,:difference)';

$q = $this->conn->prepare($sql);

return $q->execute($data);

}
}//end of class

$obj = new Stock;
if($obj->insert_stock('TES','Testing',5,500,600,200,2000,200000,600,10)!= false){
echo 'One row added sucessfully';
}
else{
echo 'Error adding the task';
}
?>

我的tbl_stock中也有这个id表有 auto_increment就在其中。

最佳答案

您的连接字符串有拼写错误:

    $connectionString = sprintf("mysql:host=%s;dbaname=%s",
Stock::DB_HOST, ^
Stock::DB_NAME);

替换为:

    $connectionString = sprintf("mysql:host=%s;dbname=%s",
Stock::DB_HOST,
Stock::DB_NAME);

关于php - 使用 PDO 语句未将数据插入 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24800918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com