doushi9474 2016-08-19 17:20
浏览 19
已采纳

使用php问题的mysql更新表单不断更新旧信息而不是新更新

I am totally new to PHP and am working on an address book that contains companies and contacts for said companies. When click submit to update the data it just replaces it with the old data that was originally in there here is the companies html form:

<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>

and here is the actual updating php sql query that does not work or something any input would help get rid of this headache I have.

<?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";
?>
  • 写回答

1条回答 默认 最新

  • douhao6271 2016-08-19 17:24
    关注

    First put here condition with WHERE clause $sql = "SELECT * FROM Companies"; that which record from database do you want to update. It seems like you are fetching all the record from database inside form

    Second you have closed your form tag at the begening of the while loop and keep rest of the input fields without form

    Change this

    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>";
        }
    

    to

    ?>
    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线