我有一些jquery ajax代码来更新记录,但它没有按预期工作。
以下是代码:
function update_records() {
$.ajax({
type: "POST", // Set the type then $_GET['id'] or $_POST['id']
url: "update_record.php",
data: { category: "John", image: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
}; //end function
Then the php
<?php
$id = $_REQUEST['id'];
include 'config.php';
$result = mysql_query("UPDATE mytable SET title = 'something' where id=$id");
$result = mysql_query("SELECT * FROM mytable WHERE id = '$id'");
$array = mysql_fetch_row($result);
?>
答案 0 :(得分:1)
来自jQuery.com的示例 http://api.jquery.com/jQuery.ajax/
$.ajax({
type: "POST", // Set the type then $_GET['id'] or $_POST['id']
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
我无法理解您的查询返回的内容。
也许您需要选择id = $id;
// $id = $_POST['id'] or $_GET['id'] Where is $id???
$result = mysql_query("UPDATE mytable SET title = 'something' where id=$id");
$result = mysql_query("SELECT * FROM mytable WHERE id = '$id'");
$array = mysql_fetch_row($result);
//You can use mysql_error() function to see the error.
$result = mysql_query("UPDATE mytable SET title = 'something' where id=$id") or die(mysql_error());
答案 1 :(得分:1)
我看到你有:
$result = mysql_query("UPDATE mytable SET title = 'something' where id=$id");
我看不出你在哪里取$id
答案 2 :(得分:0)
$id = $_REQUEST['id'];
我认为您在执行更新查询之前忘记包含此行。