I'm working on creating a user registration script in PHP. I have two tables that need to be inserted into. One that stores some general user information, and another that stores their login credentials. My issue is that my code to perform these insert is a mess of if/else blocks and I'm trying to figure out a good way to condense this down into a logical and clean code block.
Note that for now I have just placed some echo's that will later be replaced by a real error handling system once I truly know how many paths I'm going to need.
Can someone please review this script and let me know some improvements I can make to it? I know it's quite ugly right now.
$mysqli = getMysqlConnection(); if ($stmt = $mysqli->prepare("INSERT INTO users (username, email, regtime, emailverified, type) values (?,?,?,?,?);")) { date_default_timezone_set("America/New_York"); $dateStr = date("m-d-Y h:i:s"); $emailverified = 0; $type = 0; $rc = $stmt->bind_param('sssii', $username, $email, $dateStr, $emailverified, $type); if($rc === true){ $rc = $stmt->execute(); if($rc === true) { if ($stmt = $mysqli->prepare("INSERT INTO usercreds (username, hash) VALUES (?, ?);")) { $rc = $stmt->bind_param('ss', $username, $hash); if($rc === true){ $rc = $stmt->execute(); if($rc !== true) { echo "so close..."; } } else { echo "well, shit..."; } } else { { echo "whoops..."; } } } else { echo "noooooooooo!"; } } } else { echo "uh oh"; } echo "done";
echo
messages serve what purpose exactly?\$\endgroup\$