0

I'm trying to insert users into a local mysql database table but the header keeps saying stmtfailed instead of error=name. Any idea as to why I might be getting this issue

function createUser($conn, $name, $email, $password){
    $sql = "INSERT INTO users (UserName, UserEmail, UserPassword) VALUES (?, ?, ?);";
    $stmt = mysqli_stmt_init($conn);

    if (!mysqli_stmt_prepare($stmt, $sql)){        
        header("location: ../Views/signup.php?error=stmtfailed");           
        exit();
    } 

    $hashed_psw = password_hash($password, PASSWORD_DEFAULT);

    mysqli_stmt_bind_param($stmt, "sss", $name, $email, $hashed_psw);
    mysqli_stmt_execute($stmt);

    mysqli_stmt_close();    
    $conn->close();  
    header("location: ../Views/signup.php?error=none");
    exit(); 
} 
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Where did you learn to write code like this? You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Oct 15 '21 at 11:46
  • @Dharman I am still learning, I want to be comfortable with what I write before I try to write code like a professional – PureWare -Legends Of Gaming Oct 15 '21 at 11:48
  • Ok, makes sense. If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection – Dharman Oct 15 '21 at 11:48
  • @Dharman I started with mysql when I learned php, for now its what I'm focusing on since a lot of tutorials are using mysqli and its easier for me to understand – PureWare -Legends Of Gaming Oct 15 '21 at 11:49
  • 1
    Please do not use these tutorials, you are only going to learn bad stuff. All mysqli tutorials are mostly outdated. Look for ones that teach PDO. Trust me PDO is easier and works better for MySQL – Dharman Oct 15 '21 at 11:50
  • They've worked in the past for me, but I'll look into PDO – PureWare -Legends Of Gaming Oct 15 '21 at 11:51
  • For example, the code from your function should be only 3 lines long. Wouldn't that be easier? – Dharman Oct 15 '21 at 11:52
  • Of course it would, but wouldn't you say understanding something and working your way up is better than going in blindly just because people on the internet told you to, I did that once didn't work at all – PureWare -Legends Of Gaming Oct 15 '21 at 11:52
  • 1
    I would agree with Dharman that PDO is likely to make things more straightforward, but if you want to stick with mysqli then follow the advice in the links in the first comment and set up error handling properly. Sane error handling isn't something only for professionals, it should be part of the basic setup for everyone - for a beginner actually it makes things a lot simpler because you don't end up with a big pile of complicated spaghetti code just to trap the occasional error. It's crazy that mysqli doesn't enable it by default tbh, but we have to work with what we're given. – ADyson Oct 15 '21 at 12:09
  • @ADyson It does enable it by default since PHP 8.1. – Dharman Oct 15 '21 at 12:20
  • @Dharman good to know. at long last... – ADyson Oct 15 '21 at 12:31

0 Answers0