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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能