energysuperstore09 Posted March 26, 2009 Share Posted March 26, 2009 How do add data from my login form when the users register to my database? Can anybody help me? Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/ Share on other sites More sharing options...
lonewolf217 Posted March 26, 2009 Share Posted March 26, 2009 http://www.w3schools.com/SQL/sql_insert.asp http://www.tizag.com/phpT/forms.php Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794667 Share on other sites More sharing options...
KPH71 Posted March 26, 2009 Share Posted March 26, 2009 Simple version of the query: $query = "INSERT INTO tablename (field1, field2, field3) VALUES ('value1', 'value2', 'value3')"; An Example for you: $query = "INSERT INTO login (username, password) VALUES ('$username', '$password')"; (Obviously replace the tablename, fields and values) Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794668 Share on other sites More sharing options...
energysuperstore09 Posted March 26, 2009 Author Share Posted March 26, 2009 I add this query to the database? Do I add this query to the PHP script? Sorry, I am new to this Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794670 Share on other sites More sharing options...
energysuperstore09 Posted March 26, 2009 Author Share Posted March 26, 2009 This is my php script for when they regsiter: <?php $email = $_POST['email']; $password = $_POST['pass']; $password2 = $_POST['pass2']; @mysql_connect("xxx","xxx","xxx") or die("Error: unable to connect to database"); @mysql_select_db("test09"); $result = mysql_query("SELECT * FROM `accounts` WHERE `email`='$email'"); if (mysql_num_rows($result) != 0) { die("Another account is already using that email"); } if ($password != $password2) { die("The two passwords did not match"); } $password = md5($password); mysql_query("INSERT INTO `accounts` (`email`, `password`) VALUES ('$email', '$password')"); echo "Your account was successfully created!"; ?> I have about 5 other fields other than email and password that I want to store Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794672 Share on other sites More sharing options...
KPH71 Posted March 26, 2009 Share Posted March 26, 2009 Here is a simplistic version for you so that you can grasp the basics. I do not advise you to use just what I have put here, however you can get the basics of mysql from it. First you need to setup a mysql connection and select the database (I am assuming you are using mysql here): <?php $host = "localhost"; $user = "username"; $pass = "password"; $database = "database"; mysql_connect($host, $user, $pass); mysql_select_db($database); ?> Then you can run the query (this assumes you have assigned $username and $password to the values you want to put in): $query = "INSERT INTO login (username, password) VALUES ('$username', '$password')"; $result = mysql_query($query) or die(mysql_error()); You may have to change the above to suit your database - e.g. add in extra fields etc Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794673 Share on other sites More sharing options...
energysuperstore09 Posted March 26, 2009 Author Share Posted March 26, 2009 Yeah I have created the database and have made the connection. The login registeration works but It only send the email and password data to the database. Ity doesn't send the name, phone number, etc. Do I have to do an INSERT command in the database and php to get it to send the data to the database? Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794676 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Just add them into this query. Looks like you already did most of your checks. For example, if you wanted to add first name into this query, you would have to: 1) Create a column in your table called, "fname" 2) Grab the value from the post like you do with email and password. 3) Add it into your INSERT query: mysql_query("INSERT INTO `accounts` (`email`, `password`, `fname`) VALUES ('$email', '$password', '$fname')"); echo "Your account was successfully created!"; NOTE* You should really sanitize your values that go into your database with mysql_real_escape_string(). Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794678 Share on other sites More sharing options...
energysuperstore09 Posted March 26, 2009 Author Share Posted March 26, 2009 when i enter this query into the database it gives me this error messege: #1054 - Unknown column '$repnumber' in 'field list' But i do have this column in my field list. Confused? Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794691 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Post the query. Also put or die(mysql_error()) at the end, like this; mysql_query("INSERT INTO `accounts` (`email`, `password`, `fname`) VALUES ('$email', '$password', '$fname')") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794692 Share on other sites More sharing options...
energysuperstore09 Posted March 26, 2009 Author Share Posted March 26, 2009 this may sound like a stupid question and probaly is: When you insert a mysql_query in php, it must be done in the database as well, right? when i enter this query it still doesn't send the fname data to the database Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794697 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 When you insert a mysql_query in php, it must be done in the database as well, right? I don't know what you mean by this but you need to have a field called "fname" with a VARCHAR datatype in your database for this to work. You need to get the fname like how you get the email: $email = $_POST['email']; and for fname: $fname = $_POST['fname']; *NOTE: fname is the name of your input field from the form you submitted from i.e.: Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794704 Share on other sites More sharing options...
energysuperstore09 Posted March 26, 2009 Author Share Posted March 26, 2009 i go the first name (fname) to work but when I try to do the last name (lname) like i did the first, MYSQL gives me an error messege stating that i don't have lname is an unknown column. The column is there and don't understand why it would give me this error messege. Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794708 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 lname should be the same exact process as fname... Can you show me your table structure, and your most recent query? Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794712 Share on other sites More sharing options...
energysuperstore09 Posted March 26, 2009 Author Share Posted March 26, 2009 ok. this is what I got: CREATE TABLE IF NOT EXISTS `accounts` ( `repnumber` text NOT NULL, `fname` text NOT NULL, `lname` text NOT NULL, `cname` text NOT NULL, `phone` text NOT NULL, `email` text NOT NULL, `username` text NOT NULL, `password` text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; This is my table structure My INSERT QUERY DID WORK Looks like this: mysql_query("INSERT INTO `accounts` (`repnumber`, `fname`, `lname`, `cname`, `phone`, `email`, `username`, `password`) VALUES ('$repnumber', '$fname', '$lname', '$cname', '$phone', '$email', '$username', '$password')"); The problem now is when I regsiter for the login the only data that shows up in my database is the fname, email, password. All the others such as lname, phone, cname, repnumber, doesn't show up. Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794725 Share on other sites More sharing options...
energysuperstore09 Posted March 26, 2009 Author Share Posted March 26, 2009 here is the link to my website where peopl would register: http://www.texasfluorescents.com/distributorlogin1.html Quote Link to comment https://forums.phpfreaks.com/topic/151280-the-insert-function-help/#findComment-794726 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.