0
我的表單輸入記錄,如果我根據我所建立的正則表達式格式化表格字段,然後當不echo'ed表單驗證錯誤Echo的被插入到數據庫中的記錄成功。如果我犯了任何錯誤,它只是刪除字段,並不會迴應錯誤。我對PHP相當陌生,我正在爲一個課程項目做這個工作,作爲在線課程的一部分。我不確定我的錯誤在哪裏。任何幫助將不勝感激。試圖插入新記錄
<!DOCTYPE HTML>
<html lang = "en">
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Tech Order Department.html</title>
<meta charset = "UTF-8" />
<style>
div {
text-align: justify;
}
.section {
margin-left: auto;
margin-right: auto;
width: 70%;
}
</style>
</head>
<body>
<h2>Tech Orders</h2>
<br>
<title>Page Title</title>
<h2>New Project</h2>
<p class="first"><span class="error">* required field.</span></p>
<form action="http://www.oldgamer60.com/Project/try.php" method="post">
<div class="fieldset">
<fieldset>
Project: <input type="text" name="Project" value="<?php if(isset($Project)){ echo $Project; } ?>">
<span class="error">* <?php if(isset($ProjectErr)){ echo $ProjectErr; } ?></span>
<br><br>
Client: <input type="text" name="Client" value="<?php if(isset($Client)){ echo $Client; } ?>">
<span class="error">* <?php if(isset($ClientErr)){ echo $ClientErr; } ?></span>
<br><br>
LastName: <input type="text" name="LastName" value="<?php if(isset($LastName)){ echo $LastName; } ?>">
<span class="error">* <?php if(isset($LastNameErr)){ echo $LastNameErr; } ?></span>
<br><br>
DateReceived: <input type="text" name="DateReceived" value="<?php if(isset($DateReceived)){ echo $DateReceived; } ?>">
<span class="error">* <?php if(isset($DateReceivedErr)){ echo $DateReceivedErr; } ?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</fieldset>
</div>
</form>
<br>
<?php
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// create connection
$connection = new mysqli($servername, $username, $password, $dbname);
if(isset($_POST['submit']) && !$connection->connect_error){
// to track errors
$error = false;
// now validate input fields
if (empty($_POST['Project']) || !isset($_POST['Project'])){
$ProjectErr = "Project name is required";
$error = true;
}elseif(!preg_match("/^[A-Za-z0-9.-]+$/",$_POST['Project'])){
// check if project only contains number, letters, comma's periods and whitespace
$ProjectErr = "Only letters, numbers, comma's, periods and white space allowed";
$error = true;
}else{
$Project = test_input($_POST['Project']);
}
if (empty($_POST['Client']) || !isset($_POST['Client'])){
$ClientErr = "Client name is required";
$error = true;
}elseif(!preg_match("/^[A-Za-z0-9.-]+$/",$_POST['Client'])){
// check if project only contains number, letters, comma's periods and whitespace
$ClientErr = "Only letters, numbers, comma's, periods and white space allowed";
$error = true;
}else{
$Client = test_input($_POST['Client']);
}
if (empty($_POST['LastName']) || !isset($_POST['LastName'])){
$LastNameErr = "Last name is required";
$error = true;
}elseif(!preg_match("/^[A-Za-z0-9-]+$/",$_POST['LastName'])){
// check if last name only contains letters and whitespace
$LastNameErr = "Only letters and white space allowed";
$error = true;
}else{
$LastName = test_input($_POST['LastName']);
}
if (empty($_POST['DateReceived']) || !isset($_POST['DateReceived'])){
$DateReceivedErr = "Data received field is required";
$error = true;
}elseif(!preg_match("/^\d{4}-\d{2}-\d{2}$/",$_POST['DateReceived'])){
// check if data received only contains letters and whitespace
$DateReceivedErr = "Date must be entered as YYYY/MM/DD";
$error = true;
}else{
$DateReceived = test_input($_POST['DateReceived']);
}
if(!$error){
$query = "INSERT INTO Projects (Project, Client, LastName, DateReceived) VALUES ('$Project', '$Client', '$LastName', '$DateReceived')";
if($connection->query($query)){
echo "record is successfully inserted!";
}else{
echo "error: record could not be inserted";
}
}
}
?>
<?php
$connection->close();
?>
</div>
</div>
</body>
</html>
我搬到了PHP下的形式,但我覺得我一直是在頁面底部的密切聯繫中。我運行了你的編輯,但我現在收到兩個警告。 #1警告:mysqli :: mysqli()[mysqli.mysqli]:(28000/1045):在/ home/oldga740/public_html/Project/try中拒絕用戶'xxx'@'localhost'(使用密碼:YES) .php 51行 #2警告:mysqli :: close()[mysqli.close]:無法在第113行的/home/oldga740/public_html/Project/try.php中獲取mysqli。這些是我創建的連接,關閉連接線 – Tony
如果我再次輸入登錄信息,可能會有幫助..... – Tony
感謝您的幫助。這一旦我把我的數據庫登錄信息。 – Tony