View Full Version : Php??
Joe10 01-07-2007, 08:44 PM I need a code that will send Information to a database!
Username | Password | IP ADRESS
How can i make it send it looking just like that to my database?!!
Help much appreciated!
If you need my database link to do this, I will send it in a PM!.
Hopefully this isnt to hard!
Idiotic Creation 01-09-2007, 11:58 PM Actually it may be a little confusing at first. But once you understand it, it's very simple. I will assume you're using PHP, if not, then sorry.
Three steps (three functions) are used to insert data into a SQL database.
The first step is connection, using the function: mysql_connect(). The protocol for this function is:
mysql_connect('host', 'username', 'password');
Here is an application:
$link = mysql_connect ("mysql.opticalmusic.net", "my_username", "my_password") or
die("Cannot connect to server.");
Ok, there are two things to note here:
1. The return value of the function is assigned to a variable (of your choice) I named mine $link. This is a boolean value, meaning it is only true or false. It also acts as a reference to this specific connection.
2. or die(); - I added that on the end, it is basically a "fail-safe" In case the connection fails it will halt execution of the script and return the error.
The second step is to select your database (in case you have more than one). Even if you only have one database, it is good practice. The function to do this is mysql_select_db(). The protocol for it is:
mysql_select_db('Database_name', connection_reference);
Here is an application:
mysql_select_db ("my_database" , $link)
or die ("Can Not Connect to database.");
We also used a fail-safe "die" function here.
Finally, The third step is to make your query. You will need to understand SQL(sequential query language), which is different from php, and I can't teach it to you right here (although it is very simple) I would recommend http://www.w3schools.com
The function to make a query is mysql_query(). The protocol is
mysql_query("Query Here", connection_reference);
Here is an application:
$result = mysql_query ("SELECT * FROM users WHERE username='$username'", $link);
Ok, some stuff to note:
1. The results of your query need to be assigned into a variable (unless you are deleting or inserting, then it isn't necessary)
2. You CAN use php variables in queries.
3. If you are getting data out of the database (using SELECT) the value you placed in $results will not be the data you are expecting. It will be a reference to the data.
Ok, thats all I feel like explaining, but to retrieve data from a database you will also need to use: mysql_fetch_array() or something similar.
Good Luck, (I hope this was helpful)
David
Joe10 01-10-2007, 02:55 AM Well cant you just make one whole php code for that instead of piecing it together???
I am confuesed i just want it to pull the info from my one html page and send it to a database in a certain order!
Idiotic Creation 01-11-2007, 09:35 PM Sorry I didn't mean to confuse you. That is one script. I just broke it into pieces to make it easier to explain. It would probably be convient to put the first to together in one script and call it config.php. Its contents would be this:
$link = mysql_connect ("mysql.opticalmusic.net", "my_username", "my_password") or
die("Cannot connect to server.");
mysql_select_db ("my_database" , $link)
or die ("Can Not Connect to database.");
That way, anywhere you ever need to connect to your database you can use this:
require("config.php");
Then you can make your queries. But once again I remind you, you need to know SQL before you can work with your database. Its very simple.
SELECT means to get information
DELETE means to delete
UPDATE means to change an existing field
INSERT means you want to add new data
those are the basic commands of SQL, so you put what you want to do then you tell the database what fields you want. These depend on what fields are in your table
* Selects all the fields in the table
or you can name a specific field
Now your query is
SELECT *
next insert the keyword "FROM":
SELECT * FROM
Then add the name of the table you want to access (say "users"):
SELECT * FROM users
and that is a perfectly legitimate SQL query
If you wanted to insert, the query goes something like this:
INSERT INTO users (username, password) VALUES ('$username', '$password')
This says to insert $username and $password into the table called `users` and the fields called `username` and `password`.
Also one last side note. If you're wanting to make this thing you are trying to make your self, then you are just going to have to strap your self in for the ride and read the tutorials. Otherwise you should check out hot scripts for pre-made database systems.
Good Luck,
David
Joe10 01-12-2007, 01:36 AM that doesnt make it store anything?? It doesnt put the stuff i requested ib there!!
no ip is stored or anything!
Idiotic Creation 01-12-2007, 02:09 AM Well, post the script you are using and I'll have a look.
Are you getting an error when you try to use this, or is it just not inserting?
Joe10 01-12-2007, 04:12 PM I uswed the script he gave mne the last time but alls it does is open mysql but it doesnt store anything cant you guys just gimme the code to do it!!!!!!!!!!!!!!!!!!!!!!! :confused: :confused: :tired: :tired:
Douglas 01-12-2007, 05:42 PM Don't get mad, get glad. Anyway, you want to insert data into the table? Well then, I assume you already have a database setup and such, so this script will connect and create a new table (replace the necessary items):
<?php
$database_host = 'mysql.opticalmusic.net';
$database_username = 'REPLACE_ME';
$database_password = 'REPLACE_ME';
$database_name = 'REPLACE_ME';
mysql_connect($database_host, $database_username, $database_password) or die('Error connecting!');
mysql_select_db($database_name);
mysql_query("CREATE TABLE `users`(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`ip_address` VARCHAR(16) NOT NULL,
PRIMARY KEY(`id`),
INDEX(`username`))") or die('Could not create table!');
?>
After you run that, you can insert the data into the table like so:
<?php
$username_to_be_inserted = 'Administrator';
$password_to_be_inserted = 'password';
$ip_address_to_be_inserted = $_SERVER['REMOTE_ADDR'];
$database_host = 'mysql.opticalmusic.net';
$database_username = 'REPLACE_ME';
$database_password = 'REPLACE_ME';
$database_name = 'REPLACE_ME';
mysql_connect($database_host, $database_username, $database_password) or die('Error connecting!');
mysql_select_db($database_name);
mysql_query("INSERT INTO `users` (`username`,`password`,`ip_address`) VALUES('
{$username_to_be_inserted}','{$password_to_be_inse rted}','{$ip_address_to_be_inserted}')") or die('Could not insert data!');
?>
Is this what you want?
Idiotic Creation 01-12-2007, 08:19 PM replace the necessary items
Its important that you don't miss that, because there is nothing wrong with the code I gave you, but you have to understand it was not written specifically for you, so you will need to change things. Also don't get angry with people who are just trying to help, because they don't have to teach you. They could just say, Go read the tutorials.
|