doufan6886
2014-05-26 19:04
浏览 253
已采纳

将select标签中的选定值插入数据库

I have a database table with the names and locations of schools. Looks like this

id      name                           location
1   Federal University of tech        California
2.  Massachusetts Inst of tech            Boston

I'm populating a select tag with the names of these schools using this table. And it works fine. Each user is supposed to select his/her school when creating his/her profile. However when the form is submitted, only the first word in the school name gets inserted. e.g for Federal University of tech; only Federal is inserted into the database and for Massachusetts Inst of tech only Massachusetts is inserted - and so on - as opposed to the full name of the school. How do i get the full names of the selected school inserted? The code(page.php):

<?php
//Getting names of schools to populate select tag

$stmt = $db->query("SELECT name from schools order by name ASC");

if($_SERVER['REQUEST_METHOD'] == "POST"){
$major = $_POST['major'];
$university = $_POST['university'];
$email = $_POST['email'];

$stmts = $db->prepare("INSERT INTO profile (university, major email)") VALUES (:university, :major, :email);
$stmts->execute(array(':university' => $university, ':major' => $major, ':email' => $email));
}

?>

//THE HTML FORM
<form action = "page.php" method = "post">

<label>University:

<?php
echo '<select title = "Select one" name= "university"></select>';
echo '<option value="">--please select--</option>';

while ($rows = $stmt->fetch[PDO::FETCH_ASSOC])
echo '<option value = '.$rows['name'].'>'.$rows['name'].'</option>';

echo '</select>';

?>
//other form elements
</label>
</form>

图片转代码服务由CSDN问答提供 功能建议

我有一个数据库表,其中包含学校的名称和位置。 看起来像这样</ p>

  id name location 
1 Federal University of tech California 
2。 麻省理工学院波士顿
 </ code> </ pre> 
 
 

我正在使用此表填充带有这些学校名称的选择标签。 它工作正常。 每个用户都应该在创建他/她的个人资料时选择他/她的学校。 但是,提交表单时,只会插入学校名称中的第一个单词。 例如联邦科技大学; 只有联邦被插入到数据库中,对于麻省理工学院,只插入马萨诸塞州 - 依此类推 - 而不是学校的全名。 如何插入所选学校的全名? 代码(page.php):</ p>

 &lt;?php 
 //获取学校名称以填充选择标记
 
 $ stmt = $ db-&gt; 查询(“从学校按名称ASC选择姓名”); 
 
if($ _ SERVER ['REQUEST_METHOD'] ==“POST”){
 $ major = $ _POST ['major']; 
 $ university  = $ _POST ['university']; 
 $ email = $ _POST ['email']; 
 
 $ stmts = $ db-&gt; prepare(“INSERT INTO profile(university,major email)”)VALUES(  :university,:major,:email); 
 $ stmts-&gt; execute(array(':university'=&gt; $ university,':major'=&gt; $ major,':email'=&gt; $ email  )); 
} 
 
?&gt; 
 
 // HTML格式
&lt; form action =“page.php”method =“post”&gt; 
 
&lt; label&gt;大学:\  n 
&lt;?php 
echo'&lt; select title =“选择一个”name =“university”&gt;&lt; / select&gt;'; 
echo'&lt; option value =“”&gt;  - 请选择 -  &lt; / option&gt;'; 
 
while($ rows = $ stmt-&gt; fetch [PDO :: FETCH_ASSOC])
echo'&lt; option value ='。$ rows ['name']。'&gt;'  。$ rows ['name']。'&lt; / option&gt;'; 
 
echo'&lt; / select&gt;'; 
 
?&gt; 
 //其他表单元素
&lt; / label&g  t; 
&lt; / form&gt; 
 </ code> </ pre> 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题