2016-04-18 94 views
0

嗨,我想更新ID行,當用戶點擊下面的按鈕,請幫助和我的英語不好更新數據庫阿賈克斯PHP

的index.php

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 

<body> 

<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a> 
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a> 
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a> 

</body> 
</html> 

update.php對不起

<?php 
include('database_connection.php'); 

$update = "UPDATE id SET id = id + 1 WHERE id = updateId"; 

if (mysqli_query($connect, $update)) { 
    echo "Record updated successfully"; 
} else { 
    echo "Error updating record: " . mysqli_error($connect); 
} 
?> 
+0

你需要看看AJAX調用JavaScript(瀏覽jQuery爲簡單起見)和一種方法來讀取PHP數據(看$ _GET [「」]或$ _ POST [ '']) –

+0

你已經確定你需要使用JavaScript/Ajax,但是我沒有問題中的JavaScript。首先查找介紹性的Ajax教程。 – Quentin

回答

2

在您的index.php文件

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 
<body> 

<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a> 
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a> 
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a> 

</body> 
</html> 

<script> 
function updateId(id) 
{ 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
      alert(xmlhttp.responseText); 
     } 
    }; 
    xmlhttp.open("GET", "update.php?id=" +id, true); 
    xmlhttp.send(); 
} 
</script> 

而在你update.php

<?php 
if(isset($_GET['id']) && !empty($_GET['id'])) 
{ 
    $id = $_GET['id']; 
    include('database_connection.php'); 

    $update = "UPDATE id SET id = id + 1 WHERE id = '".$id."'"; 

    if (mysqli_query($connect, $update)) 
    { 
     echo "Record updated successfully"; 
    } 
    else 
    { 
     echo "Error updating record: " . mysqli_error($connect); 
    } 
    die; 
} 
?> 
+0

@downVoter請原諒倒票???有什麼不對.. –

+0

這就是我一直在尋找的!謝謝^ _^ – Alex

+0

@Doraemon歡迎親愛的,很高興幫助你...投我的答案,如果它對你有幫助... –