<?php
/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script. Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.inc.php");
if(!empty($_POST))
{
echo "Posted data not empty";
} else {
$query = "INSERT INTO userrequests ( userName, contactNumber, userAddress, storeList, requestBody ) VALUES ( :name, :contact, :address, :store, :request ) ";
$query_params = array(
':name' => $_POST['userName'],
':contact' => $_POST['contactNumber'],
':address' => $_POST['userAddress'],
':store' => $_POST['storeList'],
':request' => $_POST['requestBody']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error2. Please Try Again!";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Request successfully sent! One of our Rockets will contact you in a while!";
echo json_encode($response);
?>
<h1>Register</h1>
<form action="register.php" method="POST">
Name:<br />
<input type="text" name="userName" value="" />
<br /><br />
Contact Number:<br />
<input type="text" name="contactNumber" value="" />
<br /><br />
Address:<br />
<input type="text" name="userAddress" value="" />
<br /><br />
Store:<br />
<input type="text" name="storeList" value="" />
<br /><br />
Request:<br />
<input type="text" name="requestBody" value="" />
<br /><br />
<input type="submit" value="Call A Rocket" />
</form>
<?php
}
?>
注意:未定义的索引:第20行的C:\ xampp \ htdocs \ callarocket \ register.php中的userName
注意:未定义的索引:第21行的C:\ xampp \ htdocs \ callarocket \ register.php中的contactNumber
注意:未定义的索引:第22行的C:\ xampp \ htdocs \ callarocket \ register.php中的userAddress
注意:未定义的索引:第23行的C:\ xampp \ htdocs \ callarocket \ register.php中的storeList
注意:未定义的索引:第24行的C:\ xampp \ htdocs \ callarocket \ register.php中的requestBody {“success”:0,“message”:“数据库错误2.请再试一次!”}
我可以知道这段代码有什么问题吗?
答案 0 :(得分:1)
因此,您正在检查$_POST
不是否为空,如果是,则使用不存在的变量来创建查询。你应该把你的sql代码放在$_POST
不为空的部分。
但是,您应检查所有密钥是否存在。这样,您确定不会发生任何错误。
<?php
require("config.inc.php");
if(!empty($_POST))
{
echo "Posted data not empty";
$query = "INSERT INTO userrequests ( userName, contactNumber, userAddress, storeList, requestBody ) VALUES ( :name, :contact, :address, :store, :request ) ";
$query_params = array(
':name' => $_POST['userName'],
':contact' => $_POST['contactNumber'],
':address' => $_POST['userAddress'],
':store' => $_POST['storeList'],
':request' => $_POST['requestBody']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error2. Please Try Again!";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Request successfully sent! One of our Rockets will contact you in a while!";
echo json_encode($response);
}else{
?>
<h1>Register</h1>
<form action="register.php" method="POST">
Name:<br />
<input type="text" name="userName" value="" />
<br /><br />
Contact Number:<br />
<input type="text" name="contactNumber" value="" />
<br /><br />
Address:<br />
<input type="text" name="userAddress" value="" />
<br /><br />
Store:<br />
<input type="text" name="storeList" value="" />
<br /><br />
Request:<br />
<input type="text" name="requestBody" value="" />
<br /><br />
<input type="submit" value="Call A Rocket" />
</form>
<?php
}
?>