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

将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>
  • 写回答

1条回答 默认 最新

  • du90093662774150 2014-05-26 19:27
    关注

    I think the problem lies in the the way you have written value attribute for the option tag. The suggestion is to put values-with-spaces in double quotes.

    Like:

    echo '<option value = "Massachusetts Inst of tech">Massachusetts Inst of tech</option>';
    


    But what is getting passed is this because double quotes have been neglected:

    echo '<option value = "Massachusetts">Massachusetts Inst of tech</option>';
    

    You might want to check out this line:

    echo '<option value = '.$rows['name'].'>'.$rows['name'].'</option>';
    

    and correct with this one:

    echo '<option value = "'.$rows['name'].'">'.$rows['name'].'</option>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值