Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正在尝试将一些数据插入MySQL数据库_Php_Mysql - Fatal编程技术网

Php 正在尝试将一些数据插入MySQL数据库

Php 正在尝试将一些数据插入MySQL数据库,php,mysql,Php,Mysql,我正在使用php进行一个项目,并试图将一些数据插入MySQL数据库。当我在线运行代码并尝试将数据插入数据库时,我得到了一个致命错误。从错误消息中,我认为这可能与数据库使用时间戳有关。我尝试使用NOW()获取当前日期和时间,但没有成功。如果有人能给我指出正确的方向,我将不胜感激 这是错误消息 Fatal error: Uncaught exception 'Exception' with message 'Database Error [1064] You have an error in yo

我正在使用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
更新,新错误消息

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

您需要用撇号包装日期变量

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

并确保格式正确:try$paydatetime=date(“Y-m-dh:i:s”)

我经常要做的一件事是验证我的查询,即对
$lgi->insertrent
中的实际SQL和变量执行
var\u dump
echo
,然后在phpMyAdmin中手动执行它。如果是SQL中的语法错误或参数计数不匹配等,这将帮助您缩小范围。此外,您还可能试图在
时间戳
字段中插入
日期时间
。Datetime的格式为
YYYY-MM-DD HH:ii:ss
,而时间戳为整数(自unix纪元起的秒数)。更多信息请参见@WOUNDEDStevenJones所说的。回显发送到数据库的实际SQL,语法错误应该很明显。此外,您的代码容易受到SQL注入的攻击。有关更多信息,请参阅。在我的DAO中,我使用的是mysqli\u real\u escape\u字符串。在这个模型中,我调用“require_once('DAO.php');”。使用mysqli_real_escape_字符串,这会阻止SQL注入还是这是一种糟糕的做法。