0

Hi i want to update id row when a user click the button below please help and sorry for my bad english

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);
}
?>
2
  • You need to look at ajax javascript calls (look at jquery for simplicity) and a way to read data in php (look at $_GET[''] or $_POST['']) Commented Apr 18, 2016 at 10:25
  • You've identified that you need to use JavaScript / Ajax, but there i no JavaScript in the question. Start by looking for an introductory Ajax tutorial. Commented Apr 18, 2016 at 10:27

1 Answer 1

2

In your index.php file

<!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>

And in your update.php

<?php 
if(!empty($_GET['id']))
{
    $id = $_GET['id'];
    include('database_connection.php');
    
    $update = "UPDATE id SET id = id + 1 WHERE id = ?";
    
    $connect->execute_query($update, [$id]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Doraemon welcome dear, glad to help you... vote my answer if it is helpful for you...
I have a question whenever it updates the id, it gives an alert. how can i stop this.
remove this line alert(xmlhttp.responseText); in ajax call..., this is alert for response of your ajax call..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.