+ 3

PHP problem.... I need help guys

I tried running the below script and gives this error on line 35 concerning the mysqli_query Error: Parse error: syntax error, unexpected 'mysqli_query' (T_STRING) in C:\xampp\htdocs\server.php on line 35 Here is the script: <?php $username = ""; $email = ""; $errors = array(); //connecting to the database $db = mysqli_connect('localhost', 'root', '', 'registration'); //if the register button is clicked if (isset($_POST['register'])) { $username =mysql_real_escape_string($_POST['username']); $email =mysql_real_escape_string($_POST['email']); $password_1 =mysql_real_escape_string($_POST['password_1']); $password_2 =mysql_real_escape_string($_POST['password_2']); //ensure that form fields are filled properly if (empty($username)) { array_push($errors, "username is requied"); // add error to errors array } if (empty($email)) { array_push($errors, "Email is requied"); // add error to errors array } if (empty($password_1)) { array_push($errors, "Password is requied"); // add error to errors array } if ($password_1 != $password_2) { array_push($errors, "The two passwords does not match"); // add error to errors array } // if ther are no errors, save user too database if (count($errors == 0)) { $password = md5($password_1); //encrypt password before storing in database (security) $sql = "INSERT INTO users (username, email,password) VALUES ('$username', '$email', '$password');" mysqli_query($db, $sql); } } ?>

12th Dec 2017, 2:52 PM
Okoro Ikechukwu Stephen
Okoro Ikechukwu Stephen - avatar
4 Answers
+ 5
Adding one point, checking whether if there's anything in $errors should be done like: if(count($errors) == 0) { Rather than... if(count($errors == 0)) {
12th Dec 2017, 3:38 PM
Ipang
+ 4
PROBLEM: $sql = "INSERT INTO users (username, email,password) VALUES ('$username', '$email', '$password');" ^At the end, you put the semi-colon inside of your double quotes, instead of outside of it. CHANGE TO: $sql = "INSERT INTO users (username, email,password) VALUES ('$username', '$email', '$password')"; ^Semi-colon moved outside the double quotes.
12th Dec 2017, 3:13 PM
AgentSmith
+ 4
thanks guys for your help, am Now cleared
13th Dec 2017, 9:31 PM
Okoro Ikechukwu Stephen
Okoro Ikechukwu Stephen - avatar
+ 3
You added the semicolon before the closing doublequote sign instead of after it.(in the insert query Also I don't know which PHP version you're using but both mysql_real_escape_string and md5 functions are depreciated and should be avoided.
12th Dec 2017, 3:13 PM
CHMD
CHMD - avatar