我正在創建一個運動員姓和名的表單。這可以像它應該那樣工作,填充數據庫的適當部分。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);
這可能是一個壞主意,不要把整個頁面代碼放在這裏。 http://pastebin.com/TvbxFAhK – Geoff