将从下拉列表中选择的值插入PhP中的数据库

时间:2016-11-15 04:15:17

标签: php html mysql combobox

这是包含Combobox的HTML代码。

<form action="resourcemain.php" method="post">
                        Resource Name: <input type="text" name="resname" id="res"/><br>
                        Price per item: <input type="text" name="ppi" id="price"/><br>
                        Select Vendor: <select name="resadd">
                        <?php
                        $con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
                        $query = mysqli_query($con,"select venname from vendoradd");
                        while ($row=$query->fetch_assoc()){
                        echo "<option value=".$row['venname'].">".$row['venname']."</option>";
                        }
                        ?>
                        </select>
                        <center><input type="submit" value="ADD" id="ares">
                        <input type="reset" value="ADD MORE RESOURCE" id="more"></center>
                    </form>

这是用于插入数据库的PhP文件。

<?php
$con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");

$resname = $_POST['resname'];
$ppi = $_POST['ppi'];
$venname = $_POST['resadd'];
//$vendor = mysql_real_escape_string($venname);

$query = mysql_query("insert into mainresource(resname,price,venname)values('$resname','$ppi','$venname'");
if($query)
{
echo"<script>alert('Resource inserted! If you want to add more resource     please click on add more resource button');
window.location='http://localhost/project/Miniproject_Final/public_html/php/resadd.php';
</script>";
}
else
{
echo"<script>alert('Resource insertion failed! Please contact the web admin for more details.');
window.location='http://localhost/project/Miniproject_Final/public_html/php/resadd.php';
</script>";
}
?>

让我解释一下我要做的事情。在HTML页面中我将数据检索到组合框中,一旦检索到数据,我将添加资源名称,价格并从组合框中选择一个选项,然后单击添加按钮,该按钮必须调用resourcemain.php文件并且然后必须将输入的值插入到resourcemain.php文件中写入的表中。

任何帮助将不胜感激,谢谢你提前。

0 个答案:

没有答案