我似乎无法找到解决我在任何线程上遇到的问题的方法。 我有2页,一个HTML页面,然后是一个PHP页面。 HTML页面简单地从数据库中的列填充下拉列表。这是它的要点:
<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>
然后,PHP回显一个HTML表单,并用文本信息填充文本框。问题是当我点击提交按钮时,信息是数据库被删除,除了网站的名称字段:
<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 "; 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();
?>
任何人都可以看到我做错了或者我错过了一些语法吗?我已经坚持了一段时间。
答案 0 :(得分:0)
在 SELECT查询之前放置您的UPDATE查询。您正在从数据库中的信息构建表单,然后更新数据库。
做评论者所说的话,并防范SQL injection。
答案 1 :(得分:0)
在代码的这一部分:
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']
已设置,$_POST['site_name']
已设置,其他$_POST
字段为空(因为您的ajax帖子不会发送它们)。因此,正如您所见,您的表更新会将这些引用的字段设置为空白。