如何在选择下拉框中设置值?

时间:2016-11-27 05:23:08

标签: php mysql

我是php的初学者。这是我选择下拉框的代码。我的问题是如何使用来自我在mysql中创建的数据库的php输入数据,并检查所选数据是否已成功插入。

<select name = "txtroundcake">
                <option value="" >SELECT ONE</option>
                <option value=">Boiled Icing 6" without filling</option>
                <option>Boiled Icing 6" with filling</option>
                <option>Boiled Icing 9" without filling</option>
                <option>Boiled Icing 9" with filling</option>
                <option>Boiled Icing 12" without filling</option>
                <option>Boiled Icing 12" with filling</option>
                <option>Boiled Icing 14" without filling</option>
                <option>Boiled Icing 14" with filling</option>
                <option>Fondant 2 1/2" without filling</option>
                <option>Fondant 4" without filling</option>
                <option>Fondant 9" without filling</option>
                <option>Fondant 9" with filling</option>
                <option>Fondant 12" without filling</option>
                <option>Fondant 12" with filling</option>
                <option>Fondant 14" without filling</option>

我的数据库的架构是 productinfo(的productid,名称,价格,类别ID,描述,图像)

1 个答案:

答案 0 :(得分:2)

好的,所以我认为这段代码应该做你想要的。 下面的代码将采用他们选择的任何选择,并将其放在名为productinfo

的表的Name列中
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = mysqli_connect($servername, $username, $password);
echo "
<form action='' method='post'>
<table>
<b>Select One:</b>
<select name='name = "txtroundcake"'>
<option value='Choice1'>label here</option>
<option value='Choice2'>label here</option>
<option value='Choice3'>label here</option>
<option value='Choice4'>label here</option>
<option value='Choice5'>label here</option>
<option value='Choice6'>label here</option>
<option value='Choice7'>label here</option>
<option value='Choice8'>label here</option>
</select>
</table>
<input type='submit' name='Submit' value='Send'></form>
<b>Message Here</b>
";


$txtroundcake = SecurePost($_POST['txtroundcake']);
if ($Submit) {
if(!$txtroundcake) {
echo"Please include your name";
}
else {

mysql_query("INSERT INTO productinfo (name) VALUES ('$txtroundcake')");  
echo "<font color='green'><b>Submitted!</b></font>";
}
}
?>

我留下的评论没有正确格式化代码,所以这里是

从产品信息中获取价格,并显示它,你会做这样的事情

$getItems = mysql_query("SELECT * FROM productinfo WHERE name LIKE '%$q%'
ORDER BY productid DESC"); 
$gI = mysql_fetch_object($getItems); 

<b>".$gI->price." Price</b> 
<b>".$gI->name." Name</b> 
i havent tested it, but i think to put this in a dropdown box you would do
<option value='Choice1'>Name:".$gI->name."Price:".$gI->price."</option>
or if you wanted to display a price and the name
<option value='Choice1'>Name:".$gI->name."Price:".$gI->price."</option>

但我不是积极的,善良的