【发布时间】:2016-04-11 14:31:29
【问题描述】:
我不知何故被卡住了,可能很傻但不能安静地锻炼。 可以说,我有 2 张桌子 - 位置(盖子,公司,位置) 打印机(pid、盖子、型号、价格) 在我的索引页面中,我有一个添加新打印机的表单。这里都是文本输入字段,但位置是从下拉框中选择的。在下拉列表中,公司和位置都将显示为被选中,当其中一个被选中时,位置 ID 将被视为值,需要插入打印机表以链接到位置表以供以后使用。 一切正常,两列值一起显示以供选择,但其 id 值即使在选择后也不会插入打印机表中。你能告诉我该怎么做吗?
Index.php 页面:
<?php
error_reporting(E_ALL ^ E_NOTICE);
?>
<?php include './connect.php'; ?>
<html>
<head>
<title>Printer Page</title>
</head>
<body>
<?php
if($_POST['addprintbtn']){
$pserial = $_POST['pserial'];
$pmodel = $_POST['pmodel'];
$plocation = $_POST['plocation'];
$pprice = $_POST['pprice'];
$sql2 = mysqli_query($connect, "INSERT INTO printer(pid, model, lid, price) VALUES($pserial, $pmodel, $plocation, $pprice)");
if($sql2 == false){
echo "<p style='color:red'>Something is wrong! Please try again!</p> ";
}
else
echo "<p style='color:green'>Printer added successfully!</p>";
}
echo "<form action='./index.php' method='post' class='form'>
<h3>Add a New Printer</h3>
<label>Printer ID/Serial </label>
<input type='text' name='pserial' required /><br/><br/>
<label>Printer Model </label>
<input type='text' name='pmodel' required /><br/><br/>
<label>Printer Price </label>
<input type='number' name='pprice' required /> BDT<br/><br/>
<label>Location </label>
<select name='plocation'>";
$sql= mysqli_query($connect, "select * from location");
foreach ($sql as $row) {
$loc1=$row['lid'];
$loc2 = $row['company'];
$loc3 = $row['location'];
echo "<option value=\"$loc1\">$loc2, $loc3</option>";
}
echo "<input style='background-color:#557755; color:#ffffff; font-weight:bold; font-size:11px;' name='addprintbtn' type='submit' value='Add Printer'>
</form>";
?>
</body>
</html>
我的数据库连接页面connect.php
<?php
$connect = mysqli_connect("localhost", "root", "", "test");
if(mysqli_connect_errno($connect))
{
echo 'Failed to connect';
}
?>
但是,当我提交表单时,它不会插入数据并显示错误。 任何帮助将不胜感激!再次感谢!
【问题讨论】:
-
你遇到了什么错误?
-
@Epodax 嗨,没有错误。我提交后,它只是没有按预期将表单数据插入数据库中。但是,我尝试回显应该在 mysqli 查询之前插入的数据,并且它回显了数据就好了。所以我猜在提交后它会获取数据但无法插入数据库!
标签: php html css for-loop mysqli