2015-12-05 79 views
0

你好,我有麻煩讓我的表單驗證正確,以及獲取插入語句發佈確認的消息/張貼到數據庫中的表。插入語句和狀態下拉不起作用

下面是代碼

<?php require_once('dbuser.php');?> 
<?php 
// if the submit button has been selected 
if(isset($_POST['submit_registration'])) { 
    // assign variable to each for control to capture the values 
    $first = $_POST['first_name']; 
    $last = $_POST['last_name']; 
    $email = $_POST['email']; 
    $address1 = $_POST['address1']; 
    $address2 = $_POST['address2']; 
    $city = $_POST['city']; 
    $state = $_POST['state']; 
    $postal = $_POST['postal_code']; 
    $phone = $_POST['phone']; 
    $distance = $_POST['dist']; 
    //assign null values for use with the isset function to identify null fields that are missing a value 
    $nofirst = null; 
    $nolast = null; 
    $noemail = null; 
    $noaddress1 = null; 
    $noaddress2 = ''; 
    $nocity = null; 
    $nostate= null; 
    $nopostal = null; 
    $nophone = null; 
    $nodistance = null; 
    // if value of variable is nothing assign something other than null 
    if($first=="") { 
     $nofirst = ''; 
     $novalue = ''; 
    } 
    if($last==""){ 
     $nolast = ''; 
     $novalue =''; 
    } 
    if($email=="") { 
     $noemail = ''; 
     $novalue = ''; 
    } 
    if($address1=="") { 
     $noaddress1 = ''; 
     $novalue = ''; 
    } 
    if($address2=="") { 
     $noaddress2 = ''; 
     $novalue = ''; 
    } 
    if($city=="") { 
     $nocity = ''; 
     $novalue = ''; 
    } 
    if($state=="") { 
     $nostate = ''; 
     $novalue = ''; 
    } 
    if($postal=="") { 
     $nopostal = ''; 
     $novalue = ''; 
    } 
    if($phone=="") { 
     $nophone = ''; 
     $novalue = ''; 
    } 
    if($distance=="") { 
     $nodistance = ''; 
     $novalue = ''; 
    } 
    if(isset($novalue)) {$confim = 'Please enter all values';} 
     else { 
     $insertsql = "INSERT INTO `runner`(`fname`, `lname`,`email`,`address1`,`address2`,`city`,`state`,`postalcode`, `phone`, `distance`) VALUES ('$first', '$last', '$email', '$address1','$address2','$city','$state','$postal','$phone','$distance')"; 
     mysqli_query($lrconnect, $insertsql) or die ("Insert failed: ".mysql_error($lrconnect)); 
     $confirm = 'Thank you for registering!'; 
     } 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="UTF-8"> 
    <title>website</title> 
    <link rel="stylesheet" type="text/css" href="css/style.css" /> 
    </head> 
    <body> 
     <header><img src="images/eastersealsclevelogo.png" alt="Easter Seals Logo" width="445" height="300"</img> </header> 
     <nav> 
     <ul> 
      <li><a href="index.html" class="current">Home</a></li> 
      <li><a href="signup.php" class="current">Run Sign-Up</a></li> 
      <li><a href="refer.php" class="current">Refer-a-Friend</a></li> 
     </ul> 
     </nav> 
    <h1>Sign up for a event!</h1> 
    <p>If you would like to join in any of the runs, then feel free to sign up using the form provided below.</p><br> 
     <?php {echo $confirm;} ?> 
     <form method="post" action="signup.php"> 
     <form name="signup" title="signup" id="signup"> 
     <table> 
     <tr> 
     <td><label for="first_name">First Name:</label></td> 
     <td><input type="text" name="first_name" id="first_name" title="first name" ></td> 
     <?php if(isset($nofirst)){echo"<span class='errormsg'> First name is required<br></span>";}?> 
     </tr> 

     <tr> 
     <td><label for="last_name">Last Name:</label></td> 
     <td><input type="text" name="last_name" id="last_name" title="last name" ></td> 
     <?php if(isset($nolast)){echo"<span class='errormsg'> Last Name is required<br></span>";}?> 
     </tr> 

     <tr> 
     <td><label for="email">Email:</label></td> 
     <td><input type="email" name="email" id="email" title="email" ></td> 
     <?php if(isset($noemail)){echo"<span class='errormsg'> Your Email is required<br></span>";}?> 
     </tr> 

     <tr> 
     <td><label for="address1">Address 1:</label></td> 
     <td><input type="text" name="address1" id="address1" title="address1" ></td> 
     <?php if(isset($noaddress1)) {echo"<span class='errormsg'> Your Address is required<br></span>";}?> 
     </tr> 

     <tr> 
     <td><label for="address2">Address 2:</label></td> 
     <td><input type="text" name="address2" id="address2" title="address2"></td> 
     </tr> 

     <tr> 
     <td><label for="city">City:</label></td> 
     <td><input type="text" name="city" id="city" title="city" value="Cleveland" ></td> 
     <?php if(isset($nocity)){echo"<span class='errormsg'> Your City is required<br> </span>";}?> 
     </tr> 

     <tr> 
     <td><label for="state">State:</label></td> 
     <td> 
    <select > 
    <option value="AL" name="AL" title="alabama">Alabama</option> 
    <option value="AK" name="AK" title="alaska">Alaska</option> 
    <option value="AZ" name="AZ" title="arizona">Arizona</option> 
    <option value="AR" name="AR" title="arkansas">Arkansas</option> 
    <option value="CA" name="CA" title="california">California</option> 
    <option value="CO" name="CO" title="colorado">Colorado</option> 
    <option value="CT" name="CT" title="connecticut">Connecticut</option> 
    <option value="DE" name="DE" title="delware">Delaware</option> 
    <option value="DC" name="DC" title="district of columbia">District Of Columbia</option> 
    <option value="FL" name="FL" title="florida">Florida</option> 
    <option value="GA" name="GA" title="georgia">Georgia</option> 
    <option value="HI" name="HI" title="hawaii">Hawaii</option> 
    <option value="ID" name="ID" title="idaho">Idaho</option> 
    <option value="IL" name="IL" title="illinois">Illinois</option> 
    <option value="IN" name="IN" title="indiana">Indiana</option> 
    <option value="IA" name="IA" title="iowa">Iowa</option> 
    <option value="KS" name="KA" title="kansas">Kansas</option> 
    <option value="KY" name="KY" title="kentucky">Kentucky</option> 
    <option value="LA" name="LA" title="louisiana">Louisiana</option> 
    <option value="ME" name="ME" title="maine">Maine</option> 
    <option value="MD" name="MD" title="maryland">Maryland</option> 
    <option value="MA" name="MA" title="massachusetts">Massachusetts</option> 
    <option value="MI" name="MI" title="michigan">Michigan</option> 
    <option value="MN" name="MN" title="minnesota">Minnesota</option> 
    <option value="MS" name="MS" title="mississippi">Mississippi</option> 
    <option value="MO" name="MO" title="missouri">Missouri</option> 
    <option value="MT" name="MT" title="montana">Montana</option> 
    <option value="NE" name="NE" title="nebraska">Nebraska</option> 
    <option value="NV" name="NV" title="nevada">Nevada</option> 
    <option value="NH" name="NH" title="new hampshire">New Hampshire</option> 
    <option value="NJ" name="NJ" title="new jersey">New Jersey</option> 
    <option value="NM" name="NM" title="new mexico">New Mexico</option> 
    <option value="NY" name="NY" title="new york">New York</option> 
    <option value="NC" name="NC" title="north carolina">North Carolina</option> 
    <option value="ND" name="ND" title="north dakota">North Dakota</option> 
    <option value="OH" name="OH" title="ohio" selected>Ohio</option> 
    <option value="OK" name="OK" title="oklahoma">Oklahoma</option> 
    <option value="OR" name="OR" title="oregon">Oregon</option> 
    <option value="PA" name="PA" title="pennsylvania">Pennsylvania</option> 
    <option value="RI" name="RI" title="rhode island">Rhode Island</option> 
    <option value="SC" name="SC" title="south carolina">South Carolina</option> 
    <option value="SD" name="SD" title="south dakota">South Dakota</option> 
    <option value="TN" name="TN" title="tennessee">Tennessee</option> 
    <option value="TX" name="TX" title="texas">Texas</option> 
    <option value="UT" name="UT" title="utah">Utah</option> 
    <option value="VT" name="VT" title="vermont">Vermont</option> 
    <option value="VA" name="VA" title="virginia">Virginia</option> 
    <option value="WA" name="WA" title="washington">Washington</option> 
    <option value="WV" name="WV" title="west virginia">West Virginia</option> 
    <option value="WI" name="WI" title="wisconsin">Wisconsin</option> 
    <option value="WY" name="WY" title="wyoming">Wyoming</option> 
</select></td> 
    <?php if(isset($nostate)) {echo "<span class='errormsg'>Your state is required<br></span>";}?> 
</tr> 

    <tr> 
    <td><label for="postal_code">Zip Code:</label></td> 
    <td><input type="text" name="postal_code" id="postal_code" title="postal_code" pattern="[\d]{5}(-[\d]{4})" ></td> 
    <?php if(isset($nopostal)) {echo "<span class='errormsg'> Your postal code is required<br></span>";}?> 
    </tr> 

    <tr> 
    <td><label for="phone">Phone:</label></td> 
    <td><input type="tel" name="phone" id="phone" title="phone" ></td> 
    <?php if(isset($nophone)) {echo "<span class='errormsg'> Your Phone number is required<br></span>";}?> 
    </tr> 

    <tr> 
    <td><label for="distance">Distance:</label></td><br> 
    <td><input type="radio" name="dist" value="1mile" id="dist" title="1mile" checked ><label>1 Mile</label><br> 
    <input type="radio" name="dist" value="5k" id="dist" title="5k"><label>5K</label><br> 
    <input type="radio" name="dist" value="10k" id="dist" title="10k"><label>10K</label></td> 
    <?php if(isset($nodist)) {echo "<span class='errormsg'> You must choose a distance</span>";}?> 
    </tr> 

    <tr> 
    <td> <input type="submit" name="submit_registration" id="submit_registration" title="submit" value="Submit Form"></td> 
    <td> <input type="button" name="cancel" id="cancel" title="cancel" value="Cancel" onClick="location.href='index.html'"></td> 
     </tr> 
     </table> 
    </form> 

    </body> 

</html> 

任何幫助,不勝感激!

回答

1

您不需要像這樣在所有選項標籤中分配名稱。 <option value="AL" name="AL" title="alabama">Alabama</option><select name="dropdownname">只需指定一個名稱,並把它在你的要求$city = $_POST['dropdownname'];

0

我剛剛編輯你的PHP代碼,因爲當我在這拍了一下我看到了很多的if語句。這並不能解決你的問題,但它使你的代碼更具可讀性。

保存時應該避開變量。

您是否轉過錯誤報告?

希望我的代碼能幫助你一點點。對於輸入字段,可以使用下例所示的相同變量。在你輸入字段的值,你可以使用代碼:

<input name="first_name" value="<?php echo isset($first_name) ? $first_name : '' ?>"> 

這是因爲如果別人短。如果設置了first_name,那麼echo $ first_name或者echo'';

<?php require_once('dbuser.php');?> 
<?php 
error_reporting(E_ALL); 
ini_set("display_errors", 1); 


// if the submit button has been selected 
if(isset($_POST['submit_registration'])) { 
    //Those are the predefined fields, set in a array 
    $fieldsArray = ['first_name', 'last_name', 'email', 'address1', 'address2', 'city', 'state', 'postal_code', 'phone', 'dist']; 

    //The fields are always valid unless we get something else while looping 
    $valid = true; 
    //Loop through the fieldsArray 
    foreach($fieldsArray as $key => $value){ 
     if(isset($_POST[$value])){ 
      //If $_POST with the value from the fieldsArray is SET, then create a variable called $first_name or $last_name, depends on the keys from 
      // the fields array 
      ${$value} = $_POST[$value]; 
     } else { 
      //If the value is not set, then the validation is false 
      $valid = false; 
     } 
    } 

    //If false, then dont run the query 
    if($valid == false) 
    { 
     $confim = 'Please enter all values'; 
    } else { 
     $insertsql = "INSERT INTO `runner`(`fname`, `lname`,`email`,`address1`,`address2`,`city`,`state`,`postalcode`, `phone`, `distance`) VALUES ('".$first."', '".$last_name."', '$email', '$address1','$address2','$city','$state','$postal','$phone','$distance')"; 
     mysqli_query($lrconnect, $insertsql) or die ("Insert failed: ".mysql_error($lrconnect)); 
     $confirm = 'Thank you for registering!'; 
    } 
}