Below is the code against which I'm receiving error : I don't see any error in the SQL syntax therefore I'm unable to resolve the query. Kindly help me out on this one. Thank you. <!DOCTYPE HTML> <html> <head> <title> Test Sign Up </title> </head> <body> <?php $con = mysqli_connect('localhost', 'root', '', 'my_database'); if (mysqli_connect_errno()) { echo "Failed to connect to the database" . mysqli_connect_error(); } else { echo "Connected <br>"; } $fullName = $_POST['full_name']; $sql = "INSERT INTO 'user_information' " . "('First Name') " . "VALUES ('$fullName')"; //echo $sql . "<br>"; $selectDB = mysqli_select_db($con, 'my_database'); if (!$selectDB) { echo "Database could not be selected ". mysqli_error($con); } $retval = mysqli_query($con, $sql); if (!$retval) { Die ('Could not enter data '. mysqli_error($con)); } ?> </body> </html> Code (markup):
Update I updated my SQL code and received the following message. 'User Name' is the column of the table user_information in which I'm trying to insert the data. $sql = "INSERT INTO `user_information` " . "(`First Name`) " . "VALUES ('$fullName')"; Code (markup): So I would appreciate if someone explains this behaviour as well.
Update I have resolve the above issue as well however, a new issue has raised its head. The error popping up now is: The code includes two different pages. So I'm pasting the code for both the pages. test_signup.php <!DOCTYPE HTML> <html> <head> <title> Test Sign Up </title> </head> <body> <form action = "test_signup_insert.php" method = "POST" name = "test_signup"> Full Name: <input type = "text" name = 'full_name'> User Name: <input type = 'text' name = 'user_name'> <?php $_SESSION['user_name'] = $_POST['user_name']; ?> Email: <input type = 'text' name = 'email_add'> <input type = "submit" name = "submit"> </form> <?php ?> </body> </html> Code (markup): And now the code for the second page which is: test_signup_insert.php <!DOCTYPE HTML> <html> <head> <title> Test Sign Up </title> </head> <body> <?php $con = mysqli_connect('localhost', 'root', '', 'my_database'); if (mysqli_connect_errno()) { echo "Failed to connect to the database" . mysqli_connect_error(); } $fullName = $_POST['full_name']; $userName = $_POST['user_name']; $emailAdd = $_POST['email_add']; $sql = "INSERT INTO `user_information` " . "(`First Name`, `User Name`, `Email`) " . "VALUES ('$fullName', '$userName', '$emailAdd')"; echo $sql . "<br>"; $selectDB = mysqli_select_db($con, 'my_database'); if (!$selectDB) { echo "Database could not be selected ". mysqli_error($con); } $retval = mysqli_query($con, $sql); if (!$retval) { Die ('Could not enter data '. mysqli_error($con)); } ?> </body> </html> Code (markup): As it can be clearly seen that I've defined the $userName variable but still it keeps displaying the error. Any suggestion for this problem? Thank you.
The error is occurring because you did not enter anything for the "User Name" field. http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index
Just an observation, but you're using mysqli, why the devil are you building the query as a string mysql_ style? That **** should have been pitched out the window a decade ago; Has NO business being done if you're using mysqli or PDO... that's PREPARE and BINDPARAM's job! ESPECIALLY if you're just going to blindly dump postData into it without any form of sanitation. I would also suggest you STOP wasting memory creating variables for nothing... and there's no reason to be building the query or it's value before you have the database selected. Similarly you might as well bite the bullet and use the object model instead of the outdated procedural wrappers. Also... hmm... you're 'retval' would be a statement object even if no rows were affected -- not sure you want that result. $con = new mysqli('localhost', 'root', '', 'my_database'); if ($con->connect_error) { echo 'Failed to connect to the database' . $con->connect_error); } else { echo 'Connected<br />'; /* you should nest the rest HERE so it doesn't try to run if connection failed! Also you created mysqli with my_database selected -- so you don't need to run $con->select_db here! */ $statement = $con->prepare(' INSERT INTO user_information (`first name`) values ( ? ) '); // Also, it's usually a bad idea to have spaces in field names. $statement->bindParam('s', $_POST['full_name']); $statement->execute(); if ($con->affected_rows > 0) { echo $con->affected_rows, ' Rows added<br />'; } else echo 'No rows were added!'; } Code (markup):