So i have this php code and i am not sure whether its correct or wrong.
i would like some help whether its wrong or right and why.
My main problem is that the result variable in the last [if] works very well but not as supposed , i guess.
Thank you in advance.
What I have tried:
<pre><?php
include 'connect.php';
function safe($con, $data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = mysqli_real_escape_string($con, $data);
return $data;
}
$name;
$email;
$msg;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = safe($conn, $_POST['name']);
$email = safe($conn, $_POST['email']);
$msg = safe($conn, $_POST['message']);
$sql = "INSERT INTO msgs (name, email, message, datte) VALUES (?, ?, ?, NOW())";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "Error with the SQL";
}
else {
mysqli_stmt_bind_param($stmt, "sss", $name, $email, $msg);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if(!$result)
{
echo "Your Message Has Been Delivered Successfully. <br/> We Will Get Back To You As Soon As Possible.";
header( "refresh:1;url=index.php" );
}
else {
echo "There was a problem with the system. Retry again later.".mysqli_error($conn);
header( "refresh:1;url=index.php" );
}
}
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<style>
body{
background:#333;
color:#fff;
text-align: center;
font-size: 1.5em;
}
</style>
</head>
<body>
</body>
</html>
Well in the <pre>$result = mysqli_stmt_get_result($stmt);
the statement returns true if it succeded and false if it failed and below at the [if] i check if it failed in otrfer to procceed with the rest of the code
if(!$result)
{
echo "Your Message Has Been Delivered Successfully. <br/> We Will Get Back To You As Soon As Possible.";
header( "refresh:1;url=index.php" );
}
Here is the input for the code above.
<div id="contact" class="contact">
<form class="form" action="msg-process.php" method="post">
<input type="text" name="name" value="" placeholder="Name" required>
<input type="text" name="email" value="" placeholder="Enter Your Email..." required>
<textarea name="message" rows="8" cols="40" placeholder="Enter Your Message..." required></textarea>
<input type="submit">
</form>
</div>