0

In this system, user should be able to add specialist of their choice after registered through the system. So, when user sign up, the column idSpec will be null. Attribute idSpec is foreign key of specialist table. I've already drop the constraint so that user able to sign up without entering idSpec. After user pick a specialist, then the system will update table user by inserting the id of that particular specialist.

However, when I run the code, it says that the data has been updated but when I check database, it doesn't updated at all.

Here is my code for update function:

<?php
include "dbconfig.php";

$pass = md5($_POST['pass']);

$stmt = $db_con->prepare("UPDATE user SET  idUser=:idUser, email=:email, pass=:pass, fName=:fName, lName=:lName, noPhone=:noPhone, address=:address, relationship=:relationship, idStudent=:idStudent, idSpec=:idSpec WHERE idUser=:idUser");

$stmt->bindParam(":idUser", $_POST['idUser']);
$stmt->bindParam(":email", $_POST['email']);
$stmt->bindParam(":pass", $pass);
$stmt->bindParam(":fName", $_POST['fName']);
$stmt->bindParam(":lName", $_POST['lName']);
$stmt->bindParam(":noPhone", $_POST['noPhone']);
$stmt->bindParam(":address", $_POST['address']);
$stmt->bindParam(":relationship", $_POST['relationship']);
$stmt->bindParam(":idStudent", $_POST['idStudent']);
$stmt->bindParam(":idSpec", $_POST['idSpec']);

if ($stmt->execute()) {
    $message = "Specialist " .$_POST['idSpec']. " has been successfully registered";

    echo"<script type='text/javascript'>alert('$message');</script>";
    echo "<script>document.location.href='index.php?user=specialistdetail&idUser=".$_SESSION['idUser']."&idSpec=".$_POST['idSpec']."';</script>";
}

else{
    echo "<script>alert('Error');document.location.href='index.php?user=specialistdetail&idUser=".$_SESSION['idUser']."&idSpec=".$_POST['idSpec']."';</script>";
}
?>

Here is the alert after run the code. The number '14' is the idSpec that has just been added.

I'm not sure if the error is in the code or database. Is there any idea or suggestion on how to fix this? Thank you in advance!!

Ana
  • 49
  • 3
  • Check your web server error log for any problems. – Jason K Jan 14 '21 at 22:08
  • **Warning!** Don't use md5 for password hashing! [The manual](https://www.php.net/manual/en/function.md5.php) even states: _"Warning - It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm"_. You should use PHP's [password_hash()](https://www.php.net/manual/en/function.password-hash.php) to create a secure hash. Then you can use [password_verify()](https://www.php.net/manual/en/function.password-verify.php) to verify a password against a hash. – M. Eriksson Jan 14 '21 at 22:10
  • Set PDO to throw exceptions when something goes wrong: https://stackoverflow.com/questions/8992795/set-pdo-to-throw-exceptions-by-default It helps when debugging. – M. Eriksson Jan 14 '21 at 22:11
  • 1
    $stmt->execute() will be true if no rows were updated. it will only be false if there is a true error. – Jason K Jan 14 '21 at 22:11
  • _Side note:_ There's no reason to set `idUser` when you already know that the value is correct (since it's what you used to search for the user) – M. Eriksson Jan 14 '21 at 22:15
  • Thank you so much for your recommendation! I've checked the error log and manage to solve it. It's a silly mistakes actually but I'm stuck for 3 days! Anyway thankyou again :)) @JasonK – Ana Jan 16 '21 at 14:23
  • Oh really? Thank you I'll definitely do more research about this! @MagnusEriksson – Ana Jan 16 '21 at 14:25

0 Answers0