Click here to Skip to main content
14,574,789 members
Rate this:
Please Sign up or sign in to vote.
See more:
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>
Posted
Updated 4-Feb-20 15:37pm
v2
Comments
k5054 1-Feb-20 20:36pm
   
"My main problem is that the result variable in the last [if] works very well but not as supposed"

Please explain. What were you expecting, what are you getting, and why does this not meet your expectations? Maybe also include some sample input.
simple world 1-Feb-20 21:12pm
   
i updated the question
Rate this:
Please Sign up or sign in to vote.

Solution 3

the statement returns true if it succeded and false if it failed
Then why are you implementing the exact opposite behaviour?
Try to remove the boolean negative operator from your if condition:
if ($result)
{
   // success
}
else
{
   // failure
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

We have no idea what your code is supposed to do, so we can't say if it "works right" or "works wrong" - you need to know what is expected of code to have any idea if it's doing what it is supposed to!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

Quote:
So i have this php code and i am not sure whether its correct or wrong.

First, the fact of getting correct result or not is an indication.
When you want to see what your code is doing and how, the debugger is the tool of choice.
-----
There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

Thank you I will learn how to use the debugger
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100