TechnoDiver Posted July 1, 2021 Share Posted July 1, 2021 (edited) 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 July 1, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/313023-prepared-statements-headache/ Share on other sites More sharing options...
Barand Posted July 1, 2021 Share Posted July 1, 2021 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/313023-prepared-statements-headache/#findComment-1587729 Share on other sites More sharing options...
TechnoDiver Posted July 1, 2021 Author Share Posted July 1, 2021 Thank you, Barand. Good call. It turned out I hadn't set some NULL default values in phpmyadmin. I'd have been messing about with that code for hours still never thinking about that. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/313023-prepared-statements-headache/#findComment-1587731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.