2015-08-27 43 views
0

我正在創建一個運動員姓和名的表單。這可以像它應該那樣工作,填充數據庫的適當部分。PHP - 保存一個下拉框到我的MySQL數據庫

我現在來添加一個下拉框,在其中他們將選擇運動員的國家。不幸的是,我無法將它顯示在數據庫的運動國家領域。這與姓氏和姓氏在同一個表格中。

我非常感謝任何幫助。

<?php echo ($error != "") ? $error : ""; ?> 
<form action="createathlete.php" method="post"> 
<br> 
<br> 
Athlete Forename: <input type="text" value="<?php echo $athleteforename; ?>" name="athleteforename" /><br/> 
Athlete Surname: <input type="text" value="<?php echo $athletesurname; ?>" name="athletesurname" /><br/> 
Representing: Country:  <select name=$athletecountry tabindex="1"> 
       <optgroup label="Continent"> 
        <option value="Country 1">Country 1</option> 
        <option value="Country 2">Country 2</option> 
        <option value="Country 3">Country 3</option> 
       </optgroup> 
      </select> 
<input type="submit" value="Register" name="submit-form" /> 
    </form> 

在頁面的前面我也有這個代碼,我從一些其他教程拼湊在一起。

//initialize php variables used in the form 
$athleteforename = ""; 
$athletesurname = ""; 
$userID = ""; 
$athletecountry = ""; 

//check to see that the form has been submitted 
if(isset($_POST['submit-form'])) { 

//retrieve the $_POST variables 
$athleteforename = $_POST['athleteforename']; 
$athletesurname = $_POST['athletesurname']; 
$athletecountry = $_POST['athletecountry']; 

//initialize variables for form validation 
$success = true; 
$userTools = new UserTools(); 

//prep the data for saving in a new user object 
    $data['athleteforename'] = $athleteforename; 
    $data['athletesurname'] = $athletesurname; 
    $data['athletecountry'] = $athletecountry; 
    $data['userID'] = $user->id; 


    //create the new user object 
    $newAthlete = new Athlete($data); 

    //save the new user to the database 
    $newAthlete->save(true); 
+0

這可能是一個壞主意,不要把整個頁面代碼放在這裏。 http://pastebin.com/TvbxFAhK – Geoff

回答

0

作爲每@Chandu和@taxicala答案,一旦代碼被表示爲「athletecountry」應該被罰款。

檢查Athlete()類是否在期待數組中的所有元素,或許國家字符串因爲某些原因意外而被丟棄?

+0

就是這樣 - 我忘記告訴運動員班級處理數據。相反,我希望它以某種方式神奇地進入數據庫。謝謝,互聯網陌生人! – Geoff

0

你有選擇的name屬性一個錯字:

變化:

<select name=$athletecountry tabindex="1"> 

要:

<select name="athletecountry" tabindex="1"> 
+0

謝謝你的幫助。不幸的是,這並沒有解決問題。運動國家領域仍未在數據庫中更新。 – Geoff

1

看來你是不給予適當的名稱select ie $ athletecountry

<select name=$athletecountry tabindex="1"> 

變化到

<select name="athletecountry" tabindex="1"> 
+0

謝謝你的幫助。不幸的是,這並沒有解決問題。運動國家領域仍未在數據庫中更新。 – Geoff

相關問題