Click here to Skip to main content
15,885,767 members

Need guidance with my code below in PHP

simple world asked:

Open original thread
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>
Tags: PHP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900