Jump to content

The Insert Function? - Help


Recommended Posts

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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().

 

 

 

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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.:

 


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.