gpt4 book ai didi

php - 尝试将一些数据插入 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 19:07:28 25 4
gpt4 key购买 nike

我正在使用 php 开发一个项目,并尝试将一些数据插入 MySQL 数据库。当我在线运行代码并尝试将数据插入数据库时​​,出现 fatal error 。从错误消息来看,我认为这可能与数据库使用时间戳有关。我尝试使用 NOW() 插入当前日期和时间,但没有成功。如果有人能指出我正确的方向,我将不胜感激。

The image is of the structure of the table (frs_Payment). I am trying to insert the data into this table

这是错误消息

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

我现在可以将数据插入数据库,但现在出现了这个新的错误消息。关于如何解决这个问题的任何想法。

这是贷款 Controller

<?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);
}
}
?>

这是贷款 View

<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>

最佳答案

您需要用撇号将日期变量括起来。

$sql = "INSERT INTO frs_Payment (payid, amount, paydatetime, empnin, custid, pstatusid, ptid) VALUES ($payid, $amount, '$paydatetime', $employeeid, $customerid, $pstatusid, $ptid)";

并确保格式正确:尝试 $paydatetime = date("Y-m-d H:i:s");

关于php - 尝试将一些数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43371536/

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