I see some php course then I learn mostly by practical way so my question is very newbie , I didn't understand why this code don't work solution for duplicate username while registre , and every time i receive probleme with header
function signup($conn) {
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
if (isset($_POST['signupSubmit'])) {
$query = mysql_query("select * from user where uid='$uid'");
$encrypted_password = password_hash($pwd, PASSWORD_DEFAULT);
if(mysql_num_rows(query)>0)
{
echo "<script> window.location.replace('test.php') </script>" ;
}
else {
$sql = "insert into usi (uid, pwd) values('$uid','$encrypted_password')";
$result = $conn->query($sql);
header("Location: test.php");
}
}
}
this:
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
if (isset($_POST['signupSubmit'])) {
should be:
if (isset($_POST['signupSubmit'])) {
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
i don't have solution for mysql_ but here is PDO example i made, hope it helps
$dbhost = "localhost";
$dbname = "database";
$mysqlusr = "root";
$mysqlpass = "";
try {
$db = new PDO("mysql:host={$dbhost};dbname={$dbname}", $mysqlusr, $mysqlpass);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (PDOException $e){
echo $e->getMessage();
}
if(isset($_POST['signupSubmit'])){
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$encrypted_password = password_hash($pwd, PASSWORD_DEFAULT);
try {
$sql = "SELECT uid FROM user WHERE uid=:uid";
$statement = $db->prepare($sql);
$statement->bindParam(':uid', $uid, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
if (empty($result)) {
try {
$sql = "INSERT INTO user (uid, pwd) VALUES (:uid, :pwd)";
$statement = $db->prepare($sql);
$statement->bindParam(':uid', $uid, PDO::PARAM_STR);
$statement->bindParam(':pwd', $encrypted_password, PDO::PARAM_STR);
$statement->execute();
echo "new user registered";
}
catch (PDOException $e){
echo $e->getMessage();
}
}
else {
echo "username already exists";
}
}
catch (PDOException $e){
echo $e->getMessage();
}
}