When i click on submit button,in database at a time two data saved one is duplicate.I show my code below
<?php
if (isset($_POST['submit'])) {
function email_subscription($data)
{
$db_connect = mysqli_connect('localhost', 'root', '');
if ($db_connect) {
$db_select = mysqli_select_db($db_connect, 'db_seip_php28');
if ($db_select) {
echo "Database Selected";
} else {
echo "Database Not Selected";
}
} else {
die('Connection Fail' . mysqli_error($db_connect));
}
$sql = "INSERT INTO db_newsletter(email_address) VALUES ('$data[email_address]')";
mysqli_query($db_connect, $sql);
if (mysqli_query($db_connect, $sql)) {
echo " Email address save successfully";
} else {
echo "Sorry May be some error happen";
}
email_subscription($_POST);
} ?>
You execute the query 2 times!
mysqli_query($db_connect, $sql);
if (mysqli_query($db_connect, $sql))
1)important thing is mysql query executed twice.
2)missing close parenthesis for if statement.
3)function call should be in outside of function.
<?php
if (isset($_POST['submit']))
{
function email_subscription($data)
{
$db_connect = mysqli_connect('localhost', 'root', '');
if ($db_connect) {
$db_select = mysqli_select_db($db_connect, 'db_seip_php28');
if ($db_select) {
echo "Database Selected";
} else {
echo "Database Not Selected";
}
} else {
die('Connection Fail' . mysqli_error($db_connect));
}
$sql = "INSERT INTO db_newsletter(email_address) VALUES ('$data[email_address]')";
// here query should be execute once only . your executing twice here take look your code
if (mysqli_query($db_connect, $sql)) {
echo " Email address save successfully";
} else {
echo "Sorry May be some error happen";
}
}
email_subscription($_POST); //function call should be in outside of function
} //missing close parenthesis of if statement here
?>