我已经尝试了一个多小时的事情,以便将其插入我的数据库中。我无法想象我的生活,即使查询完全像是工作得很好。
页面上没有抛出任何错误,但它没有将数据插入数据库。
PHP代码(变量正确发布到页面):
<?php
error_reporting(E_ALL);
//start session
session_start();
//include database connection
include('../db_connect.php');
//import info
$client = $_POST['client'];
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$vehicle = $_POST['vehicle'];
$msrp = $_POST['msrp'];
$saleprice = $_POST['saleprice'];
$date = date("Y-m-d H:i:s");
$status = "Pending";
$notes = "Test notes are great!";
$sql = "INSERT INTO sales (salePrice, saleDate, saleStatus, saleNotes, saleName, saleCompany, salePhone, saleEmail, saleAddress1, saleAddress2, saleAddress3, saleCity, saleState, saleZipcode, saleCountry, clientFK) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if ($stmt = $mysqli->prepare($sql)) {
/* Bind our params */
$stmt->bind_param('dssssssssssssssi', $saleprice , $date , $status , $notes , $name , $company , $phone , $email , $address1 , $address2 , $address3 , $city , $state , $zip , $country , $client);
/* Execute the prepared Statement */
$stmt->execute();
/* Echo results */
echo "Inserted user information into database.\n";
/* Close the statement */
$stmt->close();
}else{
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
?>
以下是我的数据库中此表的布局:
CREATE TABLE IF NOT EXISTS `sales` (
`idsale` int(11) NOT NULL AUTO_INCREMENT,
`salePrice` double NOT NULL,
`saleDate` datetime NOT NULL,
`saleStatus` varchar(50) NOT NULL,
`saleNotes` varchar(100) DEFAULT NULL,
`saleName` varchar(70) DEFAULT NULL,
`saleCompany` varchar(50) DEFAULT NULL,
`salePhone` varchar(20) DEFAULT NULL,
`saleEmail` varchar(255) DEFAULT NULL,
`saleAddress1` varchar(35) DEFAULT NULL,
`saleAddress2` varchar(35) DEFAULT NULL,
`saleAddress3` varchar(35) DEFAULT NULL,
`saleCity` varchar(20) DEFAULT NULL,
`saleState` varchar(50) DEFAULT NULL,
`saleZipcode` varchar(15) DEFAULT NULL,
`saleCountry` varchar(50) DEFAULT NULL,
`createdBy` varchar(30) DEFAULT NULL,
`createdDate` datetime DEFAULT NULL,
`modifiedBy` varchar(30) DEFAULT NULL,
`modifiedDate` datetime DEFAULT NULL,
`clientFK` int(11) NOT NULL,
PRIMARY KEY (`idsale`),
KEY `clientFK_INDEX` (`clientFK`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
任何帮助将不胜感激!提前感谢您的帮助。
答案 0 :(得分:1)
您的查询未正确绑定变量,因为您在查询中传递了16个占位符并在bind函数中传递了17个值。
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) //16 values...
$stmt->bind_param('dssssssssssssssi', $saleprice , $date , $status , $notes , $name , $company , $phone , $email , $address1 , $address2 , $address3 , $city , $state , $zip , $country , $client); //17 variables
因此,您的查询会抛出错误。在php.ini中启用错误以查看错误消息。
答案 1 :(得分:0)
现在这看起来真的很愚蠢,但它指出:
//include database connection
include('../db_connect.php');
直到我将其更改为:
之后才能正常工作//include database connection
include('db_connect.php');
最奇怪的是../可以带来所有的不同。感谢大家的帮助。