1

我有一些 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);  

?>
4

3 回答 3

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());
于 2012-05-16T12:47:17.047 回答
1

我看到你有:

$result = mysql_query("UPDATE mytable SET title = 'something' where id=$id");

我看不出你的价值在哪里$id

于 2012-05-16T12:52:05.200 回答
0

你需要

$id = $_REQUEST['id'];

我认为您在执行更新查询之前忘记包含此行。

于 2012-05-16T12:47:04.317 回答