我正在使用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>
答案 0 :(得分:0)
你需要用撇号包装日期变量。
$sql = "INSERT INTO frs_Payment (payid, amount, paydatetime, empnin, custid, pstatusid, ptid) VALUES ($payid, $amount, '$paydatetime', $employeeid, $customerid, $pstatusid, $ptid)";
确保格式正确:尝试$ paydatetime = date(&#34; Y-m -d H:i:s&#34;);