2012-05-16 82 views
1

我有一些jquery ajax代碼來更新記錄,但它不像預期的那樣工作。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); 

?> 
+1

哪裏代碼休息的價值?成功函數是否被調用?如果你在瀏覽器中打開update_record.php會怎麼樣?你的HTML在哪裏? –

回答

1

的例子我能不明白你的查詢返回。 也許你需要選擇其中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()); 
+0

我已將代碼更新到代碼中,但沒有更新,也沒有保存的消息 – Satch3000

+0

現在,您在'data'中沒有'id'字段,因此沒有$ _REQUEST ['id'] – dezso

0

u需要

$id = $_REQUEST['id']; 

我想你忘記執行更新查詢前加入這一行。從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); 
}); 

1

我看你有:

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

我沒有看到你拿的$id