chokk Posted March 21, 2011 Share Posted March 21, 2011 You have a typo on this line: die("Server error: Access denied : . " mysql_error() ); The corrected line: die("Server error: Access denied : " . mysql_error()); Link to comment Share on other sites More sharing options...
chand.sethi77 Posted March 21, 2011 Author Share Posted March 21, 2011 It is even worse now. It says that firstname, lastname, age etc are undefined and echoed that complete line "INSERT INTO (first....) values (...) Link to comment Share on other sites More sharing options...
chand.sethi77 Posted March 21, 2011 Author Share Posted March 21, 2011 This is the updated code. <?php $connect = mysql_connect ("localhost", "root", ""); if (!$connect) { die ("Could not connect to mysql : " . mysql_error() ); } $db_select = mysql_select_db ("users", $connect) ; if (!$db_select) { die ("Could not select database : " .mysql_error() ); } $select = mysql_query ("SELECT * FROM users ", $connect); if (!$select) { die ("Table selection failed " .mysql_error() ); }?><?php$insert = "INSERT INTO users (firstname, lastname, password, age) VALUES (" . $_POST['firstname'] . ", " . $_POST['lastname'] . ", " . md5($_POST['password']) . ", " . $_POST['age'] . ")";echo $insert;if (!$insert) { die ("Server error: Access denied : " . mysql_error() ); }?> Link to comment Share on other sites More sharing options...
chand.sethi77 Posted March 21, 2011 Author Share Posted March 21, 2011 I found the mistake, it is in quotes. Lemme cure it and refresh the code here Link to comment Share on other sites More sharing options...
chokk Posted March 21, 2011 Share Posted March 21, 2011 It is even worse now. It says that firstname, lastname, age etc are undefined and echoed that complete line "INSERT INTO (first....) values (...) If they are undefined that means you haven't set them. Undefined means they don't exist; the index of firstname, lastname, etc contain nothing. Are you sure your script is getting data as it is supposed to?Could you post the code of your form?Does it work now? Link to comment Share on other sites More sharing options...
chand.sethi77 Posted March 21, 2011 Author Share Posted March 21, 2011 <?php$insert = "INSERT INTO users (firstname, lastname, password, age) VALUES (\" . $_POST['firstname'] . \", \" . $_POST['lastname'] . \", \" . md5($_POST['password']) . \", \" . $_POST['age'] . \")";echo $insert;if (!$insert) { die ("Server error: Access denied : " . mysql_error() ); }?> Finally this is the code and the error is unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on Line : VALUES (\" . $_POST['firstname'] . \", \" . $_POST['lastname'] . \", \" . md5($_POST['password']) . \", \" . $_POST['age'] . \")"; Link to comment Share on other sites More sharing options...
chokk Posted March 21, 2011 Share Posted March 21, 2011 Why are you escaping the quotes? Link to comment Share on other sites More sharing options...
chand.sethi77 Posted March 21, 2011 Author Share Posted March 21, 2011 Why are you escaping the quotes?I am confused where to use single quotes and where to use double, Please re-write the correct code. I am struggling since morning Link to comment Share on other sites More sharing options...
chand.sethi77 Posted March 21, 2011 Author Share Posted March 21, 2011 there are only two quotes " " and ' ' I am using "" to specify that INSERT INTO... is a stringand ' ' to specify firstname, lastname etc.And i wanted to ask do i need to put double quotes for $_POST['firstname'] etc? Link to comment Share on other sites More sharing options...
birbal Posted March 21, 2011 Share Posted March 21, 2011 I am confused where to use single quotes and where to use double http://php.net/language.types.string for more details Link to comment Share on other sites More sharing options...
chokk Posted March 21, 2011 Share Posted March 21, 2011 I'm curious, do you have a table named "users" in your database "users"?I rewrote your code.. <?php//SQL variables -->$host = "localhost";$username = "root";$password = "";$database = "users";//Connect to the database -->$connect = mysqli_connect($host, $username, $password, $database) or die(mysqli_error($connect));//Check for data-->if($_POST){ //Post variables--> $fname = $_POST['firstname']; $lname = $_POST['lastname']; $pass = md5($_POST['password']); $age = $_POST['age']; //If there is data insert it into the database --> $SQL = "INSERT INTO users (firstname, lastname, password, age) " . "VALUES ('$fname', '$lname', '$pass', '$age')"; mysqli_query($connect, $SQL) or die(mysqli_error($connect)); echo "Data succesfully inserted!";}else{ //If not, then there's a problem! echo "You have a problem! There is no data to insert!";}?> Tested and works! Link to comment Share on other sites More sharing options...
chand.sethi77 Posted March 21, 2011 Author Share Posted March 21, 2011 Even I tried copying w3school's code and changed the values, i even remove .md5 to be sure still ERROR. please write the code for me. I have been trying for 6 hours! Link to comment Share on other sites More sharing options...
chand.sethi77 Posted March 21, 2011 Author Share Posted March 21, 2011 I don't know how bu t i am only able to insert email in phpmyadmin Link to comment Share on other sites More sharing options...
chokk Posted March 21, 2011 Share Posted March 21, 2011 Did you test the code I posted? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.