Bind Statement Error

Hi

I am learning as many of us are and i have come across something that has stumped me for a few days as was wondering if anyone could help.

the below code is throwing a error. I think its around the “insert into” section or where i bind paramiters but could be wrong. Could any help?

<?php
$dBServername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystemtut";

// Create connection
$conn = mysqli_connect($dBServername, $dBUsername, $dBPassword, $dBName);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

    if(isset($_POST['insert']))
    {
      $name = $_POST['name'];
      $gender = $_POST['gender'];

      $sql = "INSERT INTO asdf (name,gender) values ($name, $Gender);";
      $stmt = mysqli_stmt_init($conn);
      if (!mysqli_stmt_prepare($stmt, $sql)) {
          // If there is an error we send the user back to the signup page.
        header("Location: ../permnew1.php?error=sqlerror");
        exit();
      }
      else {

      mysqli_stmt_bind_param($stmt, "ss", $name, $gender);
      mysqli_stmt_execute($stmt);
   

      if (!mysqli_stmt_prepare($stmt, $sql)) {
        echo "New record created successfully";
      } 
      else 
      {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
      }
    }
  }
?>

Below is the code for the form

<?php

require “header.php”;
?>

<main>
  <div class="wrapper-main">
    <section class="section-default">
      <h1>Signup</h1>
      <?php
      
      if (isset($_GET["error"])) {
        if ($_GET["error"] == "emptyfields") {
          echo '<p class="signuperror">Please Fill In All Fields!</p>';
        }
      }
      
      ?>
      <form class="form-add" action="" method="post">
        <label>Name</label><br>
        <input type="text" name="name" placeholder="name"><br>

        <LABEL>Gender</LABEL><br>
        <select name="gender">
          <option value="">--Select--</option>
          <option value="Male">Male</option>
          <option value="Female">Female</option>
        </select><br>

        <input type="submit" name="insert" value="Insert Data">
      </form>
    </section>
  </div>
</main>

Can you include the error you’re getting in your question?

thanks for the quick reply

Its not giving me a error as such, its just directing me to permnew1.php?error=sqlerror") which is what i set to do if there was a error

$gender and $Gender are not the same and is probably causing your error

Hi

Really appreciate the reply

I have changed the case as you suggested to make sure it all is correct but still no change.

does anything else spring to mind?

Thanks

I’m not familiar with mysqli as I use the much simpler pdo, which I suggest you look into. Hopefully, someone with mysqli experience will be along soon to help you further.

Also, if continuing to use mysqli, place this line before connecting to the db server

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

Make your code actually log the error somewhere, then you’ll be able to see what’s wrong.

The OP solved this on a different forum, about 3 hours ago.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service