这可能很简单,但我无法让最后一点工作。我有一个PHP / HTML表单。通过查询数据库填写零件,并由用户填写零件。一切正常,除了PHP写入数据库中的最后一个$ sql语句。基本上,表单页面的第二部分检索许多用户存储的信封,然后允许用户输入要放入该信封的数量。我需要存储该数量,但有很多信封,我需要确保输入的数量与正确的信封相对应。你可以看到我迷路了。
这是主要形式:
<form action="newpaycheck.php" method="post">
<div id="col2top">
<?php
include 'includes/connection.php';
echo "<select name= 'bankaccount'>";
echo '<option value="">'.'--- Bank Account ---'.'</option>';
$query = mysqli_query($con,"SELECT bankaccount FROM bankaccounts");
$query_display = mysqli_query($con,"SELECT bankaccount FROM bankaccounts");
while($row=mysqli_fetch_array($query))
{
echo "<option value='". $row['bankaccount']."'>".$row['bankaccount']
.'</option>';
}
echo '</select>';
?><br><br>
Amount: <input type="text" name="paycheckamount"><br><br>
Name: <input type="text" name="paycheckname"><br><br>
Date: <input type="text" name="normaldate">
</div>
<div id="col2bottom">
<?php
$result = mysqli_query($con,"SELECT envelopename, envelopebudget FROM envelopes");
echo "<table border='1'>
<tr>
<th>Envelope</th>
<th>Budget</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
?>
<input type="submit">
</div>
</form>
</div>
这就是将所有内容写入数据库的内容:
<?php
//MySQL Database Connect
include 'includes/connection.php';
// escape variables for security
$bankaccount = mysqli_real_escape_string($con, $_POST['bankaccount']);
$paycheckamount = mysqli_real_escape_string($con, $_POST['paycheckamount']);
$paycheckname = mysqli_real_escape_string($con, $_POST['paycheckname']);
$normaldate = mysqli_real_escape_string($con, $_POST['normaldate']);
$budgetamount = mysqli_real_escape_string($con, $_POST['budgetamount']);
$sql="INSERT INTO paychecks (bankaccount, paycheckamount, paycheckname, normaldate)
VALUES ('$bankaccount','$paycheckamount','$paycheckname','$normaldate')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$sql="ALTER TABLE envelopes ADD COLUMN $paycheckname varchar (50)";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$sql="UPDATE envelopes SET '$paycheckname'='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
答案 0 :(得分:1)
不应引用列名。变化
$sql="UPDATE envelopes SET '$paycheckname'='$budgetamount' WHERE envelopename ='$envelopename'";
到
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
如果你愿意,可以使用反手
$sql="UPDATE envelopes SET `$paycheckname`='$budgetamount' WHERE envelopename ='$envelopename'";