dongma7796 2015-12-07 19:21
浏览 29

PHP页面没有更新MySQL数据

I can't seem to find the solution to the problem I've been having on any thread. I have 2 pages, an HTML page and then a PHP page. The HTML page simple populates a drop down list from a column in the database. This is the gist of it:

        <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#select_site_form').submit(function(event) {
                $.ajax({
                  type: "POST",
                  url: 'project_testing.php',
                  data: { site_name: $("#site_name_id").val() },
                });
            }); // end of submit function
        }); //end of document.ready function
    </script>
    </head>

    <body>
    <form  method="POST" action="project_testing.php" id="select_site_form">
    <?php
        $username = "root";
        $password = "";
        $hostname = "localhost"; 

        //connection to the database
        $dbhandle = mysql_connect($hostname, $username, $password) 
          or die("Unable to connect to MySQL");

        $selected = mysql_select_db("database_name", $dbhandle) 
          or die("Could not select database");


        $sql = "SELECT site_name FROM table_name where site_name != ''";
        $result = mysql_query($sql);

        echo "Please select which site's information you would like to update.<br><br>";
        echo "<select name='site_name' id='site_name_id'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['site_name'] . "'>" . $row['site_name'] . "</option>";
        }
        echo "</select>";
    ?>
    <input type="submit" name="submit" value="Select">
    </form>
    </body>

The PHP then echoes out a HTML Form and fills the textboxes with the site's information. The problem is when I'm clicking the submit button, the information is the database is deleted except for the site's name field:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $('#update_info_form').submit(function(event) {
            var region = document.getElementById('region').value;
            $sql="UPDATE `internal_tracker` SET `region`= document.getElementsByName('region').value";
        }); // end of submit function
    }); //end of document.ready function
</script>
</head>

<?php

$username = "root";
$password = "";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");

$selected = mysql_select_db("database_name",$dbhandle) 
  or die("Could not select database");

$site_name = $_POST["site_name"];

echo "Updating site information for &nbsp;&nbsp;"; echo $site_name;

//Retrieve data from database
$sql="SELECT * FROM table_name WHERE site_name='$site_name'";
$result=mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    echo '<form method="POST" action="project_testing.php" id="update_info_form">
        <table>
            <tr>
                <td><h3>Property Data</h3></td>
            </tr>
            <tr>
            <tr>
                <td> Region: </td><td> <input type="text" "name="region" id="region" value="'.$row['region'].'"></td>
            </tr>
            </tr>
            <tr>
                <td> Site Name: </td><td> <input type="text" name="site_name" value="'.$row['site_name'].'"></td>
            </tr>
            <tr>
                <td> Street Address: </td><td> <input type="text" name="street_address" value="'.$row['street_address'].'"></td>
            </tr>
            <tr>
                <td> City/State/Zip: </td><td> <input type="text" name="city_state_zip" value="'.$row['csz'].'"></td>
            </tr>
            <tr>
                <td> Priority Ranking: </td><td> <input type="text" name="priority_ranking" value="'.$row['priority_ranking'].'"></td>
            </tr>
      </table>

      <input type="submit" name="submit" value="Update">';
}

if(isset($_POST['submit'])) {
    $query = "UPDATE table_name SET region='".addslashes($_POST['region'])."', street_address='".addslashes($_POST['street_address'])."', csz='".addslashes($_POST['csz'])."', priority_ranking='".addslashes($_POST['priority_ranking'])."' WHERE site_name='".addslashes($_POST['site_name'])."'";
    mysql_query($query);
}

mysql_close();
?>

Can anyone see what I did wrong or if I am missing some syntax? I've been stuck on this for a while.

  • 写回答

2条回答 默认 最新

  • doujubeng2942 2015-12-07 20:23
    关注

    Place your UPDATE query before your SELECT query. You're constructing the form from the information in the database, then updating the database afterwards.

    And do what the commenters said, and protect against SQL injection.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端