我想寫一些代碼,只有在沒有驗證錯誤的情況下,才能將輸入的數據從表單寫入數據庫的表中。寫入數據庫?
我目前的代碼不允許我寫入數據到數據庫中,因爲它要求填寫表單中的每個字段都能寫,但我只需要表單中的一些字段是強制性的,不是所有的人都需要。
我實際上需要強制的字段是客戶ID,名字和姓氏,我已經爲他們編寫了以前的工作驗證碼。
這裏是我的代碼:
<body>
<?php
/* CUSTOMER ID VALIDATION */
if (isset($_POST["submit"])) {
$number = $_POST["customerid"];
$msg = "";
if(empty($number)) {
$msg = '<span class="error"> Please enter a Customer ID</span>';
} else if(!is_numeric($number)) {
$msg = '<span class="error"> Data entered was not numeric</span>';
} else if(strlen($number) != 6) {
$msg = '<span class="error"> Customer ID must be 6 digits in length</span>';
} else {
/* Success */
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* FIRST NAME VALIDATION */
if (isset($_POST["submit"])) {
$flag = false;
$badchar = "";
$string = $_POST["customerfname"];
$string = trim($string);
$length = strlen($string);
$strmsg = "";
if ($length == 0) {
$strmsg = '<span class="error"> Please enter your first name</span>';
$flag = true;}
else if ($length > 30) {
$strmsg = '<span class="error"> Can not enter more than 30 characters</span>';
$flag = true;}
else {
for ($i=0; $i<$length;$i++){
$c = strtolower(substr($string, $i, 1));
if (strpos("abcdefghijklmnopqrstuvwxyz-", $c) === false){
$badchar .=$c;
$flag = true;
}
}
if ($flag) {
$strmsg = '<span class="error"> The field contained the following invalid characters: '.$badchar.'</span>';}
}
if (!$flag) {
$strmsg = '<span class="error"> Correct!</span>';}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* LAST NAME VALIDATION*/
if (isset($_POST["submit"])) {
$flagl = false;
$badcharl = "";
$stringl = $_POST["customerlname"];
$stringl = trim($stringl);
$lengthl = strlen($stringl);
$strmsgl = "";
if ($lengthl == 0) {
$strmsgl = '<span class="error"> Please enter your last name</span>';
$flagl = true;}
else if ($lengthl > 30) {
$strmsgl = '<span class="error"> Can not enter more than 30 characters</span>';
$flagl = true;}
else {
for ($il=0; $il<$lengthl;$il++){
$cl = strtolower(substr($stringl, $il, 1));
if (strpos("abcdefghijklmnopqrstuvwxyz-", $cl) === false){
$badcharl .=$cl;
$flagl = true;
}
}
if ($flagl) {
$strmsgl = '<span class="error"> The field contained the following invalid characters: '.$badcharl.'</span>';}
}
if (!$flagl) {
$strmsgl = '<span class="error"> Correct!</span>';}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* ADDRESS VALIDATION */
if (isset($_POST["submit"])) {
$valid_states = array('ACT', 'NSW', 'NT', 'QLD', 'SA', 'TAS', 'VIC', 'WA');
if (isset($_POST[ 'customeraddress' ]) && ! empty($_POST[ 'customeraddress' ])) {
if (! isset($_POST[ 'state' ]) || ! in_array($_POST[ 'state' ], $valid_states)) {
echo "There was an error: the address is set but the state is not.";
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* POSTCODE VALIDATION */
if (isset($_POST["submit"])) {
$post = $_POST["postcode"];
$msgp = "";
if (!empty($post)) {
if(!is_numeric($post)) {
$msgp = '<span class="error"> Data entered was not numeric</span>';
} else if(strlen($post) != 4) {
$msgp = '<span class="error"> Postcode must be 4 digits in length</span>';
} else {
$msgp = '<span class="error> right</span>';
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* WRITE TO DATABASE */
if (isset($_POST["submit"])) {
$conn = mysqli_connect("localhost", "twa312", "dam6av9a");
mysqli_select_db("warehouse312", $conn)
or die ('Database not found ' . mysqli_error());
$sql = "INSERT INTO customer (customerID, firstName, lastName, address, suburb, state, postcode)
VALUES
('$_POST[customerid]','$_POST[customerfname]','$_POST[customerlname]','$_POST[customeraddress]','$_POST[suburb]','$_POST[state]','$_POST[postcode]')";
$rs = mysqli_query($sql, $conn)
or die ('Problem with query' . mysqli_error());
if (!mysqli_query($conn, $sql))
{
die('Error: ' . mysqli_error($conn));
}
echo "1 record added";
mysqli_close($conn);
}
?>
<h1>Customer Information Collection <br /></h1>
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo" >
<table>
<tr>
<td><label for="customerid">Customer ID (integer value): </label></td>
<td><input type="text" id="customerid" name="customerid" value="<?php echo $temp ?>" size=11 /><?php echo $msg; ?></td>
</tr>
<tr>
<td><label for="customerfname">Customer First Name: </label></td>
<td><input type="text" id="customerfname" name="customerfname" size=50/><?php echo $strmsg; ?></td>
</tr>
<tr>
<td><label for="customerlname">Customer Last Name: </label></td>
<td><input type="text" id="customerlname" name="customerlname" size=50/><?php echo $strmsgl; ?></td>
</tr>
<tr>
<td><label for="customeraddress">Customer Address: </label></td>
<td><input type="text" id="customeraddress" name="customeraddress" size=65/></td>
<td><label for="suburb"> Suburb: </label></td>
<td><input type="text" id="suburb" name="suburb"/></td>
</tr>
<tr>
<td>
State:<select name="state" id="state">
<option value="select">--</option>
<option value="ACT">ACT</option>
<option value="NSW">NSW</option>
<option value="NT">NT</option>
<option value="QLD">QLD</option>
<option value="SA">SA</option>
<option value="TAS">TAS</option>
<option value="VIC">VIC</option>
<option value="WA">WA</option>
</select>
</td>
<td><label for="postcode"> Post Code: </label><input type="text" id="postcode" name="postcode" size=4/><?php echo $msgp; ?></td>
</tr>
</table>
<p><input type="submit" name="submit" value="Save Data"/> <input type="reset" value="Clear Form" />
</tr>
</form>
</body>
任何一種對解決方案的幫助,將不勝感激!
太多的代碼和格式不正確。我懷疑任何人都會花時間閱讀所有內容。嘗試做一些調試,然後根據您的發現發佈問題。 – 2013-05-07 11:24:35
你的第一個問題是讓所有的代碼都在這樣一個大的blob中。將每個驗證代碼寫入一個單獨的函數,然後從主程序中依次調用它們。每個人都應該返回一個真/假的值,它會告訴你它是否正常。整理這樣的代碼將使事情變得更容易處理和理解。 – Spudley 2013-05-07 11:26:02
如果我分開代碼,我將如何去調用它們在表單頭中,會有點類似於在表頭中調用JavaScript? – user2273149 2013-05-07 11:30:26