简体   繁体   中英

PHP page not updating MySQL data

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.

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 .

In this section of your code:

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

$_POST['submit'] is set, $_POST['site_name'] is set, and the other $_POST fields are empty (because your ajax post isn't sending them). So your table update will set those referenced fields to blank, as you are seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM