我已经按照它的教程使用两个php文件来更新我的数据库。
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action =update.php method=post>";
echo "<td><input type=text name=Cname value='".$row['CustomerName']."'></td>";
echo "<td><input type=number name=size min=1 value='".$row['TableSize']."'></td>";
echo "<td><input type=date name=Adate value='".$row['DateA']."'></td>";
echo "<td><input type=time name=Atime value='".$row['TimeA']."'></td>";
echo "<td><input type=tel name=phonenumber value='".$row['PhoneNumber']."'></td>";
echo "<input type=hidden name=id value='".$row['TableID']."'>";
echo "<td><input type=submit>";
echo"</form></tr>";
}
?>
这是我用于第一个php文件的内容 至于update.php:
<?php
$con = mysqli_connect('127.0.0.1','root','');
mysqli_select_db($con,'restaurant');
$sql = "UPDATE addtable SET CustomerName='$_POST[Cname]', TableSize='$_POST[size]', DateA='$_POST[Adate]',TimeA='$_POST[Atime]',PhoneNumber='$_POST[phonenumber]', WHERE TableID=$_POST[id]";
if(mysqli_query($con,$sql))
header("refresh:1; url=AssignBooking.php");
else
echo "Not Update";
?>
但$ sql行只是没有工作,因为它说
未定义的索引:Cname和其他索引。
答案 0 :(得分:1)
将引号放在post变量之外:
$sql = "UPDATE addtable SET CustomerName='".$_POST['Cname']."', TableSize='".$_POST['size']."', DateA='".$_POST['Adate']."',TimeA='".$_POST['Atime']."',PhoneNumber='".$_POST['phonenumber']."', WHERE TableID=".$_POST['id'];
答案 1 :(得分:0)
根据您的代码,将名称属性值'
单引号。
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action =update.php method=post>";
echo "<td><input type=text name='Cname' value='".$row['CustomerName']."'></td>";
echo "<td><input type=number name='size' min=1 value='".$row['TableSize']."'></td>";
echo "<td><input type=date name='Adate' value='".$row['DateA']."'></td>";
echo "<td><input type=time name='Atime' value='".$row['TimeA']."'></td>";
echo "<td><input type=tel name='phonenumber' value='".$row['PhoneNumber']."'></td>";
echo "<input type=hidden name="id" value='".$row['TableID']."'>";
echo "<td><input type=submit>";
echo"</form></tr>";
}
?>
相应地加上引号
UPDATE addtable SET CustomerName='".$_POST['Cname']."',TableSize='".$_POST['size']."', DateA='".$_POST['Adate']."',TimeA='".$_POST['Atime']."',PhoneNumber='".$_POST['phonenumber']."' WHERE TableID=$_POST['id'];