php - Revoke User Creation, write to DB -
this small school project. new php & sql. have form, in user enters desired username & password. thereafter, trying check database see whether account same username exists. here code:
$sqlvalidate = "select id useraccounnts username='$username'"; $resultvalidate = $conn->query($sqlvalidate); $sql = "insert useraccounts values($id, '$username', '$password')"; if ($resultvalidate != 0) { $date = new datetime(); //this returns current date time $time = $date->format('d-m-y-h-i-s'); $sqlerrorlog = "insert errorlog (errortype, errortime) values ('failed create new user, username exists', '$time')"; $result = $conn->query($sqlerrorlog); echo '<script type="text/javascript">' , 'alert("sorry,an account username exists. please choose one."); window.location.href = "user.php";' , '</script>' ; }
firstly, query database. purpose of doing check whether id
exists username equal username passed form. thereafter, use if
statement checks see whether id
is/is not equal 0 (to see whether such id exists). if does, write error log database.
the code doesn't work. ignores if statement , writes new user account database regardless. in advance help. don't mind whether code secure or not, long works.
ps, don't have else
statement if
statement.
in end decided take approach:
<?php $id=10001; $sql = "select id useraccounts order id desc"; $result = $conn->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $id = $row["id"] + 1; } $username = $_request["emailinput"]; $password = $_request["passwordinput"]; //the following query used check if username same //name username entered user exists $sqlvalidate = "select id useraccounts username='$username'"; $result = $conn->query($sqlvalidate); //running query //if query returns results, error inserted db using following code: if ($result->num_rows > 0) { $date = new datetime(); //this returns current date time $time = $date->format('d-m-y-h-i-s'); //the following code queries db $sqlerrorlog = "insert errorlog (errortype, errortime) values ('failed create new user, username exists', '$time')"; $result = $conn->query($sqlerrorlog); echo '<script type="text/javascript">' , 'alert("sorry,an account username exists. please choose one."); window.location.href = "user.php";' , '</script>' ; return; }
therefore, see whether there id
same username. if query returns results (if row such username), revokes request, writes db, , gives javascript alert notify user. works , writes db.
Comments
Post a Comment