A
Anonymous
Guest
I have a simple web form, which aims to increment the value of a sql database entry, I am achieving this via a php function. My problem is that the function executes on page load which is an issue as I only want it to execute when I click the ADD button. Thank you.
Code:
<form method="post">
<input type="submit" name="add" id="ant" value="ADD" /><br/>
</form>
<?php
function add_ant(&$connection)
{
mysqli_query($connection, "UPDATE `login_info` SET `clickrate`='1' WHERE `uname`='rvbvakama' && `password`='pass'");
}
session_start();
if(isset($_SESSION['log']))
{
$con=mysqli_connect("localhost","root","hiufe78t54h87","login"); //mysqli("localhost","username of database","password of database","database name")
if(array_key_exists('add',$_POST))
{
add_ant($con);
}
$result = mysqli_query($con,"SELECT * FROM login_info");
if (!$result)
{
printf("Error: %s\n", mysqli_error($con));
exit();
}
echo "<table border='1'>
<tr>
<th>username</th>
<th>clickrate</th>
<th>ants</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['uname'] . "</td>";
echo "<td>" . $row['clickrate'] . "</td>";
echo "<td>" . $row['ants'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Add ants</h1>
<a href="index.php" >logout</a>
</body>
</html>
<?php
}
else
{
echo "please fill proper details";
header("refresh:2;url=index.php");
}
?>