我是PHP的新手,正在编写一本地址簿,其中包含所述公司的公司和联系人。当点击提交以更新数据时,它只是用最初在那里的旧数据替换它,这里是公司的html表单:
<html>
<head><title>Update Records in MYSQL Database</title>
<style>
body {background-color: powderblue;}
form {border: 5px solid midnightblue;
padding: 40px;}
label {color: darkblue;}
form {border: 5px solid midnightblue;
padding: 40px;
</style>
</head>
<body>
<?php
//Connect to the Database
$link = mysqli_connect('localhost', '', '');
//Select the Database
mysqli_select_db($link, 'ADDRESS_BOOK_DB');
//Select Query
$sql = "SELECT * FROM Companies";
//Execute the Query
$records = mysqli_query($link, $sql);
?>
<form>
<table>
<tr>
<th>CompanyName</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>ZipCode</th>
<th>PhoneNumber</th>
</tr>
<?php
while($row = mysqli_fetch_array($records)) {
echo "<tr><form action=update.php method='post'></form>";
echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
echo "<td><input type='submit'>";
echo "</form></tr>";
}
?>
</table>
</form>
</body>
</html>
这里是实际更新的php sql查询无法正常工作或任何输入有助于摆脱我的头痛。
<?php
//Connect to the Database
$link = mysqli_connect('localhost', '', '');
//Select the Database
mysqli_select_db($link, 'ADDRESS_BOOK_DB');
//Update Query
$sql = "UPDATE Companies SET CompanyName= $_POST[companyname1], Address= $_POST[address], City= $_POST[city], State= $_POST[state], ZipCode= $_POST[zipcode], PhoneNumber= $_POST[phone]
WHERE companies_id= $_POST[companies_id]";
//Execute the Query
if(mysqli_query($link, $sql))
header("refresh:1; url=edit.php");
enter code here
else
echo "Not Updated";
?>
答案 0 :(得分:1)
首先将此条件与WHERE
子句$sql = "SELECT * FROM Companies";
放在一起,您希望更新哪个数据库记录。看起来你正在从form
内的数据库中获取所有记录
其次你在while循环的begening处关闭了你的form
标记并保留了其余的输入字段没有form
改变这个
while($row = mysqli_fetch_array($records)) {
echo "<tr><form action=update.php method='post'></form>";
echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
echo "<td><input type='submit'>";
echo "</form></tr>";
}
到
?>
<form action=update.php method='post'>
<?php
while($row = mysqli_fetch_array($records)) {
echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>";
echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>";
echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>";
echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>";
echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>";
echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>";
echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>";
echo "<td><input type='submit'>";
}
?>
</form>