I have the following code in my php file:
if (isset($_POST['Add']) and $_POST['what to put in here']!="") {
$sql2="INSERT INTO $tbl_name (id, name, auftrag, bemerkungen) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT)";
$result2=mysqli_query($link, $sql2);
}
'Add' is the name of the button I would like to use to add rows to my table but every time I refresh a row gets added. It should only do so if I click on the button. My question is why it does so every refresh.
If I didn't provide enough information please let me know.
Update:
I've updated my code above with my current problem which is me not knowing what to put in the brackets where I wrote 'what to put in here'. I'm referring to the explanation from the user 'Zaid Bin Khalid' which I didn't quite understand.
You should check the value of POST
on isset($_POST['val])
not click button
Try this and in You Post Value pass your post array index.
if (isset($_POST['Add']) and $_POST['You_post_value']!="") {
$sql2="INSERT INTO $tbl_name (id, name, auftrag, bemerkungen) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT)";
$result2=mysqli_query($link, $sql2);
}
OR
if (isset($_POST['You_post_value']) and $_POST['You_post_value']!="") {
$sql2="INSERT INTO $tbl_name (id, name, auftrag, bemerkungen) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT)";
$result2=mysqli_query($link, $sql2);
}
Note: And it should be your post value, Ignore button click.
you can use jquery to listen to your button click
and send the command to your PHP file without refreshing the page.
$(document).ready(function(){
$("#yourbuttonid").click(function(){
$.ajax({
type: 'POST',
url: 'php_file_name.php',
success: function(data) {
alert(data);
$("p").text(data);
}
});
});
});
php_file_name.php
$sql2="INSERT INTO $tbl_name (id, name, auftrag, bemerkungen) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT)";
$result2=mysqli_query($link, $sql2);
echo "inserted";