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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,