Prevent function execution on page load?

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
RvBVakama
New php-forum User
New php-forum User
Posts: 2
Joined: Sat May 25, 2019 10:22 am

Sat May 25, 2019 10:26 am

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: Select all

<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");
}

?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 843
Joined: Mon Feb 22, 2016 5:52 pm

Sat May 25, 2019 11:34 am

You'll have to put some logic into your script for this to check for the add button.

RvBVakama
New php-forum User
New php-forum User
Posts: 2
Joined: Sat May 25, 2019 10:22 am

Sun May 26, 2019 12:48 am

hyper wrote:
Sat May 25, 2019 11:34 am
You'll have to put some logic into your script for this to check for the add button.
Thanks for the reply, I don't think that is the answer, but it does not matter, I have to scrap the idea.

Post Reply