首先注意:我已经查看了一些关于 SO 的建议答案,不幸的是我仍然没有找到答案。
我注意到在我的更新语句中,我试图编辑的项目的 ID 值没有被传递给 editUrl.php。但是,如果我硬编码一个 id 号(即WHERE
id = '13'";
),则更新可以完美运行。
问题:为什么'id'的值没有被传递给editUrl.php?
代码:
editUrlForm.php 文件:
<!-- Include AJAX Framework -->
<script src="js/ajax.js" language="javascript"></script>
<!--Include Database connections info-->
<?php include('config.php'); ?>
<?php
$id = $_GET['id'];
if(isset($_GET['id'])) {
$cdquery="SELECT * FROM links WHERE `id` = '$id'";
$cdresult=mysql_query($cdquery) or die ("Query to get data from first table failed: ".mysql_error());
while ($row = mysql_fetch_assoc($cdresult)) {
echo "Edit URL:";
echo "<form action='javascript:update()' method='get'>";
echo "<table>";
echo "<tr>";
echo "<th>URL ID:</th> <td><label for='urlId'>". $row['id'] . "</label></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Name:</th> <td><input name='name' type='text' id='name' value='". $row['name'] . "' size'30'></input></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Release Time:</th> <td><input name='releaseTime' type='time' id='releaseTime' value='". $row['releaseTime'] . "'></input></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Release Day:</th> <td><select name='releaseDay' id='releaseDay' value='". $row['releaseDay'] . "'> <option value='monday'>Monday</option> <option value='tuesday'>Tuesday</option> <option value='wednesday'>Wednesday</option> <option value='thursday'>Thursday</option> <option value='friday'>Friday</option> <option value='saturday'>Saturday</option> <option value='sunday'>Sunday</option> </select></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Category:</th> <td><select name='category' id='category' value='". $row['category'] . "'> <option value='television'>Television</option> <option value='movie'>Movie</option> <option value='music'>Music</option> </select></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Genre:</th> <td><select name='genre' id='genre' value='". $row['genre'] . "'> <option value='action'>Action</option> <option value='drama'>Drama</option> <option value='comedy'>Comedy</option> <option value='thriller'>Thriller</option> <option value='horror'>Horror</option> <option value='childrens'>Childrens</option> <option value='romantic'>Romantic</option> </select></td>";
echo "</tr>";
echo "<tr>";
echo "<th>URL:</th> <td><input name='url' type='text' id='url' value='". $row['url'] . "' size'250'></input></td>";
echo "</tr>";
echo "</table><br />";
echo "<input type='submit' name='Submit' value='Edit URL'/>";
echo "<input type='button' value='Cancel' onClick='actionCancel();return false;'/>";
echo "</FORM>";
}
}
?>
ajax函数:
function update()
{
var name= encodeURIComponent(document.getElementById('name').value);
var releaseTime = encodeURIComponent(document.getElementById('releaseTime').value);
var releaseDay = encodeURIComponent(document.getElementById('releaseDay').value);
var category = encodeURIComponent(document.getElementById('category').value);
var genre = encodeURIComponent(document.getElementById('genre').value);
var url= encodeURIComponent(document.getElementById('url').value);
xmlhttp.open('get', 'editUrl.php?name='+name+'& releaseTime=' +releaseTime+'& releaseDay=' +releaseDay+'& category=' +category+'& genre=' +genre+'& url=' +url);
xmlhttp.onreadystatechange = urlRefresh;
document.getElementById("content02").innerHTML = "Processing Request. Please wait a moment...";
xmlhttp.send(null);
return;
}
editUrl.php 文件:
<!-- Include Database connections info. -->
<?php include('config.php'); ?>
<?php
$id = $_GET['id'];
$name = $_GET['name'];
$releaseTime = $_GET['releaseTime'];
$releaseDay = $_GET['releaseDay'];
$category = $_GET['category'];
$genre = $_GET['genre'];
$url = $_GET['url'];
$editUrl_sql = "UPDATE `links` SET `id` = '{$id}', `name` = '{$name}', `releaseTime` = '{$releaseTime}', `releaseDay` = '{$releaseDay}', `category` = '{$category}', `genre` = '{$genre}', `url` = '{$url}' WHERE `id` = '{$id}'";
$editUrl_sql= mysql_query($editUrl_sql) or die(mysql_error());
mysql_close($link);
?>