IllusiveBroker Posted April 1, 2016 Share Posted April 1, 2016 <?php error_reporting(E_ALL); ini_set('display_errors', 1); session_start(); require_once 'ConnectorCode.php'; if(isset($_POST['submit'])) { $FName = mysqli_real_escape_string($conn, $_POST['First_Name']); $LName = mysqli_real_escape_string($conn, $_POST['Last_Name']); $Email = mysqli_real_escape_string($conn, $_POST['Email']); $UName = mysqli_real_escape_string($conn, $_POST['User_Name']); $Password = mysqli_real_escape_string($conn, $_POST['Password']); $sql = "SELECT * FROM tbl_Users WHERE Email='$Email'"; $result = mysqli_query($conn, $sql); if(count($result) == 0) { $insert_sql = "INSERT INTO tbl_Users (First_Name,Last_Name,Email,User_Name,Password) VALUES ('$FName','$LName','$Email','$UName','$Password')"; if(mysqli_query($conn, $insert_sql)) { echo "Thank you for registering"; header('Location: index.php'); exit; } } else { echo "Sorry, that email already exists!"; } mysqli_close($conn); } ?> Long story short. I'm trying to set up a registration form for users to use to create an account in which will then send the information they enter into my html form into my Users table on my MySQL database. I have everything set up properly but all I seem to get now is the else echo message which is: else { echo "Sorry, that email already exists!"; } It always pops up whenever submit is clicked! I don't know why! Why is it doing this I'm so close to having this file finished! Register.php Quote Link to comment https://forums.phpfreaks.com/topic/301134-why-do-i-keep-getting-the-error-echo-message/ Share on other sites More sharing options...
maxxd Posted April 1, 2016 Share Posted April 1, 2016 var_dump $result and see what's coming back. And this is one of those dumb questions that has to be asked, but you are using different email addresses or deleting any inserted data between rounds of testing, yes? Quote Link to comment https://forums.phpfreaks.com/topic/301134-why-do-i-keep-getting-the-error-echo-message/#findComment-1532699 Share on other sites More sharing options...
IllusiveBroker Posted April 1, 2016 Author Share Posted April 1, 2016 I've put var_dump($result) after the $result= mysqli_query($conn, $sql) line but nothings coming up. and there is no data being inserted! That's another issue, it won't even input any of the data I'm putting into the fields! Quote Link to comment https://forums.phpfreaks.com/topic/301134-why-do-i-keep-getting-the-error-echo-message/#findComment-1532700 Share on other sites More sharing options...
ginerjm Posted April 1, 2016 Share Posted April 1, 2016 PS - a header() call following any echo statement (any output) will always fail. Quote Link to comment https://forums.phpfreaks.com/topic/301134-why-do-i-keep-getting-the-error-echo-message/#findComment-1532702 Share on other sites More sharing options...
mac_gyver Posted April 1, 2016 Share Posted April 1, 2016 the mysqli result object returned by a select query ISN'T the data from the query. you would need to test the num_rows property of that object to find how many rows the query matched. Quote Link to comment https://forums.phpfreaks.com/topic/301134-why-do-i-keep-getting-the-error-echo-message/#findComment-1532712 Share on other sites More sharing options...
DaveyK Posted April 4, 2016 Share Posted April 4, 2016 $sql = "SELECT * FROM tbl_Users WHERE Email='$Email'"; $result = mysqli_query($conn, $sql); the $result value in this case is the mysqli object, but as mac_gyver previously mentioned: it is not the actual result of the query. You will still need another mysqli_fetch_* function to retrieve the data. Since all you need the above function to perform is return a count, you can implement that in the SQL itself, which is far more efficient. You should look into editing your query to implement a COUNT, as well as adding a mysqli_fetch_* function to return said count. Which mysqli_fetch_ function you should use, you can lookup in the documentation. A good practise would be to make sure the returned COUNT works before trying to insert anything into the database. It makes debugging a lot easier. Quote Link to comment https://forums.phpfreaks.com/topic/301134-why-do-i-keep-getting-the-error-echo-message/#findComment-1532788 Share on other sites More sharing options...
mac_gyver Posted April 4, 2016 Share Posted April 4, 2016 upon further review (i stopped looking the first time when i discovered the OP wasn't operating on the value correctly), the only thing to OP should be doing in this code is running the INSERT query. the user_name column and the email column should be set up as unique indexes, to prevent duplicate values in those columns. the INSERT query will fail with a duplicate key error (you can look up what error number this is and then check what error number is returned when the query fails) if either the user_name or the email already exist in the table. you should also be hashing the password using the php password_hash() function and store the hashed password in the database table. Quote Link to comment https://forums.phpfreaks.com/topic/301134-why-do-i-keep-getting-the-error-echo-message/#findComment-1532791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.