2013-05-07 161 views
-3

我想寫一些代碼,只有在沒有驗證錯誤的情況下,才能將輸入的數據從表單寫入數據庫的表中。寫入數據庫?

我目前的代碼不允許我寫入數據到數據庫中,因爲它要求填寫表單中的每個字段都能寫,但我只需要表單中的一些字段是強制性的,不是所有的人都需要。

我實際上需要強制的字段是客戶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"/>&nbsp;<input type="reset" value="Clear Form" /> 
</tr> 

</form> 

</body> 

任何一種對解決方案的幫助,將不勝感激!

+0

太多的代碼和格式不正確。我懷疑任何人都會花時間閱讀所有內容。嘗試做一些調試,然後根據您的發現發佈問題。 – 2013-05-07 11:24:35

+0

你的第一個問題是讓所有的代碼都在這樣一個大的blob中。將每個驗證代碼寫入一個單獨的函數,然後從主程序中依次調用它們。每個人都應該返回一個真/假的值,它會告訴你它是否正常。整理這樣的代碼將使事情變得更容易處理和理解。 – Spudley 2013-05-07 11:26:02

+0

如果我分開代碼,我將如何去調用它們在表單頭中,會有點類似於在表頭中調用JavaScript? – user2273149 2013-05-07 11:30:26

回答

2

的基本方法應該是所有的驗證時間喜歡這個。在頂部,設置一個變量:

$valid = true; 

然後,當執行每個驗證檢查,如果失敗設置:

$valid = false; 

除了打印錯誤消息給用戶。

最後,所有的驗證完成後,你這樣做:

if ($valid) { 
    // Code to add to database 
} 

要允許可選字段,你可以這樣做:

if (isset($_POST['postcode'])) { 
    $post = $_POST['postcode']; 
    // validation of field 
} else { 
    $post = ''; 
} 

添加else條款爲所有其他可選字段,將該變量設置爲默認值。

+0

這似乎像它應該很好地工作謝謝你,我現在只是遇到的問題在哪裏能夠寫什麼到數據庫它要求每一個領域有一個值,但我只需要幾個字段是強制性的不是所有的人,所以它應該能夠寫入空值到數據庫中,如果該字段在表單中留空,有什麼建議? – user2273149 2013-05-07 11:42:27

+0

請參閱答案中的其他代碼。 – Barmar 2013-05-07 11:49:17

+0

非常感謝您的幫助。 – user2273149 2013-05-07 11:53:06

1

雖然你的實現幾乎是好的,你的問題是你每次檢查

if(isset($_POST['submit'])) 

這是錯誤的,只是檢查裏面一個

if(isset($_POST['submit'])) { 

    //your id validation 
    //your first name validation 
    //your last name validation 

    if (youhavenoerror) { 
     //perform sql operation 
    } 

} 
+0

好吧,我已經採取了你的意見,並把所有的編碼之間的一個單一的if(isset($ _ POST ['submit'])),雖然我仍然有同樣的問題。 – user2273149 2013-05-07 11:28:27

+0

這是很好的風格,但它與他所問的問題無關。 – Barmar 2013-05-07 11:33:24

0

這段代碼可能會有所改進。

但現在對您的問題的簡短回答是在您的代碼頂部設置一個變量標誌爲true。然後在驗證失敗時進行驗證,將該標誌設置爲false。稍後在插入到db時,如果它是真的,請檢查此標誌否則不插入。

+0

謝謝你的信息,對這個問題的建議在哪裏可以寫任何東西到數據庫它要求每一個領域有一個值,但我只需要幾個字段是強制性的不是所有的人,所以它應該能夠將空值寫入數據庫如果該字段在表單中留空? – user2273149 2013-05-07 11:45:38

0

你必須一步一步做。在逐行分析和學習之前複製代碼。

試試這個。但這不是編碼的最佳實踐。我只是編輯你的代碼以便你理解。

<body> 
<?php 
/* CUSTOMER ID VALIDATION */ 
if (isset($_POST["submit"])) 
{ 
    $flag = false; 

    $number = $_POST["customerid"]; 
    $msg = ""; 

    if (empty($number)) 
    { 
     $msg = '<span class="error"> Please enter a Customer ID</span>'; 
     $flag = true; 
    } else 
     if (!is_numeric($number)) 
     { 
      $msg = '<span class="error"> Data entered was not numeric</span>'; 
      $flag = true; 
     } else 
      if (strlen($number) != 6) 
      { 
       $msg = '<span class="error"> Customer ID must be 6 digits in length</span>'; 
       $flag = true; 
      } else 
      { 
       /* Success */ 
      } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /* FIRST NAME VALIDATION */ 
    $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*/ 
    $badcharl = ""; 
    $stringl = $_POST["customerlname"]; 
    $stringl = trim($stringl); 
    $lengthl = strlen($stringl); 
    $strmsgl = ""; 

    if ($lengthl == 0) 
    { 
     $strmsgl = '<span class="error"> Please enter your last name</span>'; 
     $flag = true; 
    } else 
     if ($lengthl > 30) 
     { 
      $strmsgl = '<span class="error"> Can not enter more than 30 characters</span>'; 
      $flag = true; 
     } else 
     { 
      for ($il = 0; $il < $lengthl; $il++) 
      { 
       $cl = strtolower(substr($stringl, $il, 1)); 
       if (strpos("abcdefghijklmnopqrstuvwxyz-", $cl) === false) 
       { 
        $badcharl .= $cl; 
        $flag = true; 
       } 
      } 
      if ($flag) 
      { 
       $strmsgl = '<span class="error"> The field contained the following invalid characters: ' . $badcharl . 
        '</span>'; 
      } 
     } 
     if (!$flag) 
     { 
      $strmsgl = '<span class="error"> Correct!</span>'; 
     } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /* ADDRESS VALIDATION */ 
    $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."; 
      $flag = true; 
     } 
    } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /* POSTCODE VALIDATION */ 

    $post = $_POST["postcode"]; 
    $msgp = ""; 

    if (!empty($post)) 
    { 
     if (!is_numeric($post)) 
     { 
      $msgp = '<span class="error"> Data entered was not numeric</span>'; 
      $flag = true; 
     } else 
      if (strlen($post) != 4) 
      { 
       $msgp = '<span class="error"> Postcode must be 4 digits in length</span>'; 
       $flag = true; 
      } else 
      { 
       $msgp = '<span class="error> right</span>'; 
      } 

    } 

    if (!$flag) 
    { 
     $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"/>&nbsp;<input type="reset" value="Clear Form" /> 
</tr> 

</form> 

</body>