【发布时间】:2017-04-12 13:51:53
【问题描述】:
我正在使用 php 进行一个项目,并试图将一些数据插入 MySQL 数据库。当我在线运行代码并尝试将数据插入数据库时,我遇到了一个致命错误。从错误消息中,我认为这可能与数据库使用时间戳有关。我曾尝试使用 NOW() 来插入当前日期和时间,但它没有奏效。如果有人能指出我正确的方向,将不胜感激。
这是错误信息
Fatal error: Uncaught exception 'Exception' with message 'Database Error [1064] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '12:41:28, NIN012, 12, 1, 1)' at line 1' in E:\xampp\htdocs\CIT2318\Relational database and web integration\models\DAO.php:23 Stack trace: #0 E:\xampp\htdocs\CIT2318\Relational database and web integration\models\LoanModel.php(15): DAO->query('INSERT INTO frs...') #1 E:\xampp\htdocs\CIT2318\Relational database and web integration\controllers\LoanController.php(24): LoanModel->insertRental('104', '10.50', '2015-05-22 12:4...', 'NIN012', '12', '1', '1') #2 {main} thrown in E:\xampp\htdocs\CIT2318\Relational database and web integration\models\DAO.php on line 23
更新,新的错误信息
INSERT INTO frs_Payment (payid, amount, paydatetime, empnin, custid, pstatusid, ptid) VALUES (104, 10.50, '2015-05-22 12:41:28', 'NIN012', 1, 1, 1)
Fatal error: Call to a member function fetch_assoc() on boolean in E:\xampp\htdocs\CIT2318\Relational database and web integration\controllers\LoanController.php on line 25
我现在可以将数据插入数据库,但现在出现了这个新的错误消息。关于如何解决这个问题的任何想法。
这是贷款控制人
<?php
session_start();
require_once("../models/LoanModel.php");
require_once("../views/LoanView.php");
$error = "";
if (isset($_POST["Submit"])) {
$payid=($_POST["payid"]);
$amount=($_POST["amount"]);
$paydatetime=($_POST["paydatetime"]);
$empnin=($_POST["empnin"]);
$custid=($_POST["custid"]);
$pstatusid=($_POST["pstatusid"]);
$ptid=($_POST["ptid"]);
if (empty($payid)) {
//header('Location: ../views/LoanView.php?error=1');
$error = "Payment ID is required";
} else {
$lgi = new LoanModel;
$result = $lgi->insertRental($payid, $amount, $paydatetime, $empnin, $custid, $pstatusid, $ptid);
$row = $result->fetch_assoc();
$error = "";
$_SESSION['payid'] = $row["payid"];
$_SESSION['amount'] = $row["amount"];
$_SESSION['paydate'] = $row["paydatetime"];
$_SESSION['employeeid'] = $row["empnin"];
$_SESSION['customerid'] = $row["custid"];
$_SESSION['pstatus'] = $row["pstatusid"];
$_SESSION['ptype'] = $row["ptid"];
header('Location: ../views/MenuView.php');
}
echo "<hr>" . $error;
}
?>
这是贷款模型 更新
<?php
require_once('DAO.php');
class LoanModel extends DAO{
protected $target = "frs_Payment";
public function __construct(){
parent::__construct();
}
public function insertRental($payid, $amount, $paydatetime, $employeeid, $customerid, $pstatusid, $ptid){
$sql = "INSERT INTO frs_Payment (payid, amount, paydatetime, empnin, custid, pstatusid, ptid) VALUES ($payid, $amount, '$paydatetime', '$employeeid', $customerid, $pstatusid, $ptid)";
echo $sql;
return parent::query($sql);
}
}
?>
这是贷款视图
<html>
<head>
</head>
<body>
<h1>Add a new rental</h1>
<form action="../controllers/LoanController.php" method="POST">
<label for="payid">Payment ID</label>
<input type="text" id="payid" name="payid">
<p> </p>
<label for="amount">Amount</label>
<input type="text" id="amount" name="amount">
<p> </p>
<label for="paydatetime">Payment date/time</label>
<input type="text" id="paydatetime" name="paydatetime">
<p> </p>
<label for="empnin">Empnin</label>
<input type="text" id="empnin" name="empnin">
<p> </p>
<label for="custid">Customer ID</label>
<input type="text" id="custid" name="custid">
<p> </p>
<label for="pstatusid">Payment Status</label>
<input type="text" id="pstatusid" name="pstatusid">
<p> </p>
<label for="ptid">Payment Type</label>
<input type="text" id="ptid" name="ptid">
<p> </p>
<input type="submit" name="Submit" value="Add Rental">
<br></br>
<a href="../views/MenuView.php">Menu Page</a>
<br></br>
</form>
【问题讨论】:
-
我经常要做的一件事是验证我的查询是对
$lgi->insertRental中的实际SQL 和变量执行var_dump或echo,然后手动尝试在phpMyAdmin 中执行它。如果是 SQL 中的语法错误或参数计数不匹配等,这将帮助您缩小范围。 -
您也有可能尝试将
datetime插入timestamp字段。日期时间的格式为YYYY-MM-DD HH:ii:ss,而时间戳为整数(自 unix 纪元以来的秒数)。请参阅stackoverflow.com/questions/5362874/… 了解更多信息。 -
@WOUNDEDStevenJones 所说的。回显发送到数据库的实际 SQL,语法错误应该很明显。
-
您的代码也容易受到 SQL 注入的攻击。请参阅stackoverflow.com/questions/60174/… 了解更多信息。
-
在我的 DAO 中,我使用的是 mysqli_real_escape_string。在模型中,我调用“require_once('DAO.php');”。使用 mysqli_real_escape_string,这是防止 SQL 注入还是这种不好的做法。