Jump to content

Recommended Posts

 I've had to rearrange a lot of code and I've been trying to put together a prepared statement in a registration form. I'm having a really hard time and being very new to PHP the issue is really confusing for me.

first, I have this function:

//PROCESS DB
function process_database($post) {
  global $table;
  global $conn;

  //THIS FUCKING THING IS DRIVING ME
  //check database connection
  if ($conn->connect_error) {
    return false;

  } else {
    if($statement = $conn->prepare("INSERT INTO $table (username, email, password) VALUES ( ?, ?, ? )")){
      
      $username = $post['username'];
      $email = $post['email'];
      $password = $post['password'];

      $statement->bind_param("sss", $username, $email, $password);

      $statement->execute();
      
      //DEBUGGING
      echo "Added: ".$username.", ".$email.", ".$password."<br>";

      if(!$statement->execute()){
        printf("Connect Failed: %s\n", $conn->connect_error);
      } else {
        echo 'fuckin ay!!!';
      }
      //END DEBUG BLOCK

    } else {
      return false;
    }
  }
  return true; 
}

The issue is very strange. I'll post the function call so it's clear:

//process database actions
  if (!process_database($data) ) {
    return array( 'status' => 0, 'message' => 'Unable to process database request' );
  }

When I run the registration.py without process_database() everything is fine, so I'm confident in the error processing. Here's where it get weird -

when I process the form

Quote

echo "Added: ".$username.", ".$email.", ".$password."<br>";

is returned from the //DEBUGGING BLOCK but I also get back the error from the following if statement - "Connection Failed: ...."

BUT I also get back the registration successful message that only shows if the function returns true

In short, it's giving me 2 positive affirmations but also the Connection failed message and of course it's not adding anything to the database. I've been working this function all day, and I'm lost for answers. What's going on with this code? I can't see where I've gone wrong

Edited by TechnoDiver
Link to comment
https://forums.phpfreaks.com/topic/313023-prepared-statements-headache/
Share on other sites

That is calling the execute() a second time. If username or email must be unique that could be generating a duplicate key error - check mysql error, don't just assume it's a connection error.

Better still, put this line directly before your mysqli_connect() line

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

the all errors will throw a notification without all those if()s

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.