我有一個表格,在成功完成后,它應該被插入到資料庫中,但是,這不是發生的事情,發生的是我在資料庫表中看到“id”增加了 1,但是表中的其他列都是空的,所以與資料庫的連接不是問題,可以使用一些幫助,謝謝大家。下面是我的表單的 php 代碼,以及插入代碼。
<?php
// define variables and set to empty values
$staffErr = $emailErr = $subjectErr = $problemErr = $descriptionErr= "";
$staffname = $email = $subject = $problem_type = $description = "";
// staff name validation:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["staff_name"])) {
$staffErr = "Staff Name is required";
} else {
$staffname = test_input($_POST["staff_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z-' ]*$/",$staffname)) {
$staffErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Please enter a valid email.";
}
}
// subject validation:
if (empty($_POST["subject"])) {
$subjectErr = "Subject is required";
} else {
$subject = test_input($_POST["subject"]);
// check if subject only contains letters and whitespace
if (!preg_match("/^[a-zA-Z-' ]*$/",$subject)) {
$subjectErr = "Only letters and white space allowed";
}
}
// problem type validation:
if (empty($_POST["problem_type"])) {
$problemErr = "Problem type is required";
} else {
$problem_type = test_input($_POST["problem_type"]);
}
// description validation:
if (empty($_POST["description"])) {
$descriptionErr = "A Description is required";
} else {
$description = test_input($_POST["description"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(isset($_POST['submit'])) {
if ($staffErr == "" && $emailErr == "" &&$subjectErr == "" &&$problemErr == "" &&$descriptionErr == "") {
header("Location:insert_logs.php");
exit();
}
else {
header("Location:log-it-reports.php");
exit();
}
}
?>
<div class= "content">
<div class="form1">
<form method="POST" onsubmit=" return formSubmit() " >
<div class="error1" id= "errorMsg">* Required Fields</div>
<div class="error" id= "errorMsg1">*<?php echo $staffErr; ?></div>
<div>
<label for="staff_name"><b>Staff Name:</b></label>
<input class="field" id="staff_name" name="staff_name" onclick=" return staffValidation()" onchange=" return staffValidation()" id="subject" type="text" placeholder="Staff Name" >
</div><br>
<div class="error" id= "errorMsg2">*<?php echo $emailErr;?></div>
<div>
<label for="email"><b>Email:</b></label>
<input class="field" id="email1" name="email" onclick=" return emailValidation()" onchange=" return emailValidation()" type="email" placeholder="[email protected]">
</div><br>
<div class="error" id= "errorMsg3">*<?php echo $subjectErr;?></div>
<div>
<label for="subject"><b>Subject:</b></label>
<input class="field" name="subject" id="subject1" onclick=" return subjectValidation()" onchange=" return subjectValidation()" type="text" placeholder="Subject Title" >
</div><br>
<div class="error" id= "errorMsg4">*<?php echo $problemErr;?></div>
<div>
<select onclick=" return problemValidation()" onchange=" return problemValidation()" class="field4" name="problem_type" id="problemtypes">
<option value="">Problem Type</option>
<option value="Hardware">Hardware</option>
<option value="Software">Software</option>
<option value="Software&Hardware">Software & Hardware</option>
<option value="Other">Other</option>
</select>
</div><br>
<div class="error" id= "errorMsg5">*<?php echo $descriptionErr;?></div>
<div>
<textarea class="field2" id="description1" name="description" onclick=" return descriptionValidation()" onchange=" return descriptionValidation()" placeholder="Description goes here" name="descript" rows="15" cols="90"></textarea>
</div>
<div>
<button class="field3" name="submit" type="submit" class="btn">Submit</button>
<input type="checkbox" id="notify" name="notify" value="">
<label for="notify">Inform me by email when issue is resolved.</label>
</div>
</form>
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inserting IT reports into the database</title>
<link rel="stylesheet" href="update.css">
</head>
<body>
<?php
//inserting inputs to the data base
//database connection variables for the database on your web server
$servername = "localhost";
$username = "*******";
$password = "*******";
$database = "attack_titan";
//saving all of the user's POST values from the the form submission to local variables
$staffname = $_POST['staffname'];
$email = $_POST['email'];
$subject = $_POST['subjects'];
$problem_type = $_POST['problem_type'];
$description = $_POST['description'];
//we start a try and catch block to attempt to connect to our database and run the query. If it fails, we see the error/exception generated by the catch
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password); //building a new PDO connection object
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to exception
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO it_reports (staff_name, email, subjects, problem_type, descriptions)
VALUES ('$staffname', '$email', '$subject', '$problem_type', '$description')");
$stmt->bindParam(':staffname', $staffname);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':subject', $subject);
$stmt->bindParam(':problem_type', $problem_Type);
$stmt->bindParam(':description', $description);
// use execute() to run the query
$stmt->execute();
echo "<p class = 'inserted' >Your report has been submitted successfully</p><br>" , '<a class = "button" href="log-it-reports.php">Click to go back to the previous page</a>';
// If successful we will see this
// you could then check PHPMyAdmin to see if the record was inserted
}
catch(PDOException $e)
{
echo "Error" . $e->getMessage(); //If we are not successful we will see an error
}
?>
</body>
</html>
uj5u.com熱心網友回復:
您的插入陳述句中有錯誤。
嘗試改變:
$stmt = $conn->prepare("INSERT INTO it_reports (staff_name, email, subjects, problem_type, descriptions) VALUES ('$staffname', '$email', '$subject', '$problem_type', '$description')");
和
$stmt = $conn->prepare("INSERT INTO it_reports (staff_name, email, subjects, problem_type, descriptions) VALUES (:staffname, :email, :subject, :problem_type, :description)");
所以,bindValue 陳述句可以作業
uj5u.com熱心網友回復:
我對 MySQL 不太熟悉,但我知道有不同的存盤引擎可用。InnoDB 支持事務,而 MyISAM 沒有這樣的功能。使用 InnoDB,您可能需要根據配置顯式提交更改。
有關此主題的更多資訊:https ://dev.mysql.com/doc/refman/8.0/en/innodb-autocommit-commit-rollback.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/439558.html
上一篇:PHPxpath查找元素
下一篇:在熊貓資料框中洗掉重復項
