我收到致命错误“未捕获异常'PDOException',消息'SQLSTATE [23000]:完整性约束违规:1048列'名称'不能为空'在C:\ Users \ Aushkia \ Desktop \ PHP Class \ new.php:20堆栈跟踪:#0 C:\ Users \ Aushkia \ Desktop \ PHP Class \ new.php(20):PDOStatement->执行(数组)#1 {main}抛出C:\ Users \ Aushkia \ Desktop \ PHP Class \ new.php on line 20“
并且它也说我的第5-7行的索引是Undefined Index。
这是我这个课程的最后一个项目,我似乎无法找到如何解决这个问题,我查找的所有内容都是针对其他python或java的。为了我的宝宝出生,我必须在一个半小时内去医院!大声笑。无论如何,这是我的代码,任何帮助都会很棒,提前谢谢!!
<?php
session_start();
/* Data input */
$Name = $_POST['Name'];
$Description = $_POST['Description'];
$Price = $_POST['Price'];
/* login section */
require("auth.php");
/* sql input */
$db = new PDO( "mysql:host=localhost;dbname=php", "admin", "123" );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmnt = $db->prepare( "insert into products (name, description, price)values(:name, :description, :price)" );
$stmnt->execute( array( 'name' => $Name, 'description' => $Description, 'price' => $Price ));
$results = $db->query( 'select * from products' );
$products = $results->fetchAll();
?>
<!DOCTYPE HTML>
<!-- UPDATED TO AUTHENTIACTION -->
<html>
<head>
<meta charset="utf-8">
<title>new item</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="stl.css" type="text/css" media="screen" charset="utf-8">
</head>
<p>
<a href="show.php" class="button">Home</a>
<a href="new.php" class="button">Add New</a>
<a href="login.view.php" class="button">Login</a>
<a href="logout.php" class="button">Logout</a>
</P>
<body>
<div align="Center">
<h1>item info</h1><hr/>
<form action="new.php" method="post">
<label for="name">Name:</label><br />
<input name="name" type="text" placeholder="Name" value="" id="Name">
<br />
<label for="desciption">Description :</label><br />
<input name="desciption" type="text" placeholder="Description" value="" id="Description">
<br />
<label for="price">Price :</label><br />
<input name="price" type="text" placeholder="Price" value="" id="Price">
<br />
<p><input type="submit" value="save"></p>
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
你必须学会调试自己的代码,你得到的错误和通知是自我解释的,正如他们在评论中所说的那样。
首先是未定义的索引通知
在表单中未定义的索引表示在数组中找不到该条目。在 你的情况,$ _POST ['姓名'],$ _POST ['描述']; $ _ POST [ '价格'] 在$ _POST数组中不存在。
,您没有具有上述名称属性的输入类型。注意:名称和名称,价格和价格,描述和描述之间存在细微差别。
您在pdo上遇到致命错误,因为您在必须为非null的列上插入空值。你的错误的基础来自上面的解释,
PDO使用带有占位符的预准备语句,如:
:placeholdername,:placeholdername2
执行它们时,您可以按原样编写占位符
execute(array(':placeholdername'=>$identifiers,':placeholdername2'=>identifier2)
在您的情况下,这些$stmnt->execute( array( 'name' => $Name, 'description' => $Description, 'price' => $Price ));
不存在。
或者您使用“?”作为占位符:
你的代码应该是
<?php
session_start();
/* login section */
require("auth.php");
/* sql input */
$db = new PDO("mysql:host=localhost;dbname=php", "admin", "123");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$errors = "";
if (isset($_POST['btnName'])) {
//check if inputs are not empty
if (empty($_POST['name'])) {
echo "enter name";
$errors++;
} else {
$Name = $_POST['name'];
}
if (empty($_POST['desciption'])) {
echo "enter desciption";
$errors++;
} else {
$Description = $_POST['desciption'];
}
if (empty($_POST['price'])) {
echo "enter price";
$errors++;
} else {
$Price = $_POST['price'];
}
if ($errors <= 0) {
//No errors insert
try {
$stmnt = $db->prepare("insert into products (name, description, price)values(:name, :description, :price)");
$stmnt->execute(array(
':name' => $Name,
':description' => $Description,
':price' => $Price
));
echo "records added";
} catch (PDOException $e) {
echo "error : ".$e->getMessage();
}
}
}
?>
<!DOCTYPE HTML>
<!-- UPDATED TO AUTHENTIACTION -->
<html>
<head>
<meta charset="utf-8">
<title>new item</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="stl.css" type="text/css" media="screen" charset="utf-8">
</head>
<p>
<a href="show.php" class="button">Home</a>
<a href="new.php" class="button">Add New</a>
<a href="login.view.php" class="button">Login</a>
<a href="logout.php" class="button">Logout</a>
</P>
<body>
<div align="Center">
<h1>item info</h1><hr/>
<form action="new.php" method="post">
<label for="name">Name:</label><br />
<input name="name" type="text" placeholder="Name" value="" id="Name">
<br />
<label for="desciption">Description :</label><br />
<input name="desciption" type="text" placeholder="Description" value="" id="Description">
<br />
<label for="price">Price :</label><br />
<input name="price" type="text" placeholder="Price" value="" id="Price">
<br />
<p><input type="submit" value="save" name="btnName"></p>
</form>
</div>
</body>
</html>
或“?”占位符强>
<?php
session_start();
/* login section */
require("auth.php");
/* sql input */
$db = new PDO("mysql:host=localhost;dbname=php", "admin", "123");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$errors = "";
if (isset($_POST['btnName'])) {
//check if inputs are not empty
if (empty($_POST['name'])) {
echo "enter name";
$errors++;
} else {
$Name = $_POST['name'];
}
if (empty($_POST['desciption'])) {
echo "enter desciption";
$errors++;
} else {
$Description = $_POST['desciption'];
}
if (empty($_POST['price'])) {
echo "enter price";
$errors++;
} else {
$Price = $_POST['price'];
}
if ($errors <= 0) {
//No errors insert
try {
$stmnt = $db->prepare("insert into products (name, description, price)values(?,?,?)");
$stmnt->execute(array($Name,$Description,$Price));
echo "records added";
} catch (PDOException $e) {
echo "error : ".$e->getMessage();
}
}
}
?>
注意:为了您自己的利益,最好过滤和清理来自的输入 用户之前你可以保存它