PHP - 将下拉框保存到我的mysql数据库中

时间:2015-08-27 21:54:22

标签: php mysql

我正在制作一个表格来创建运动员的姓氏和姓氏。这样可以正常工作,填充数据库的相应部分。

我现在来添加一个下拉框,他们将在其中选择运动员的国家。不幸的是,我无法将其显示在数据库的athletecountry字段中。这与forename和surname在同一个表中。

我非常感谢任何帮助。

<?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);

3 个答案:

答案 0 :(得分:1)

您似乎没有给出正确的名称来选择即$ athletecountry

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

更改为

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

答案 1 :(得分:0)

您在选择的名称attr中输入错误:

变化:

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

要:

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

答案 2 :(得分:0)

根据@Chandu和@taxicala的答案,一旦代码显示为“athletecountry”,应该没问题。

检查Athlete()类是否期望数组中的所有元素,也许国家字符串被删除,因为它出于某种原因出乎意料?