mattsphp
10-31-2003, 05:42 PM
I will show you how to insert data into a MySQL table and how to retrieve data.
PART 1: INSERTING DATA!
For this to work, you need to already have a MySQL database with the following table:
- People
You need the following field names:
>> Name
>> Age
------------------------------
Page 1: Insert info.html
-------------------------------
<HTML>
<title>Sample form</title>
<body>
<form method="POST" action="addinfo.php">
Name:<input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
I'm sure I don't need to break this down, just a normal HTML form!
-------------------------
page 2: addinfo.php
-------------------------
<HTML>
<title>Adding information...</title>
<body>
<?PHP
$name = $_POST['name'];
$age = $_POST['age'];
mysql_connect("db_host","db_user","db_pass")or die(mysql_error());
mysql_select_db("db_name")or die(mysql_error());
mysql_query("INSERT INTO people VALUES('$name','$age')");
echo "The following values:<br>
Name: $name<br>
Age: $age<br>
was successfully added to the db";
mysql_close();
?>
OK, let me break it down.
$name = $_POST['name'];
$age = $_POST['age'];
The code above simply converts the values in the form into varibles so it can be read by PHP.
mysql_connect("db_host","db_user","db_pass")or die(mysql_error());
mysql_select_db("db_name")or die(mysql_error());
The code above connects to the database. The 'or die' command is just error control, if the script can't connect to the DB, it will come up with a message
mysql_query("INSERT INTO people VALUES('$name','$age')");
The code above just inserts the values of the form into the database.
echo "The following values:<br>
Name: $name<br>
Age: $age<br>
was successfully added to the db";
This piece of code will display in the users browser what values were added.
mysql_close();
This piece of code simply closes the Database connection
----------------------------------
Page 3: viewinfo.php
----------------------------------
<HTML>
<title>Displaying information</title>
<body>
<?PHP
mysql_connect("db_host","db_user","db_pass")or die(mysql_error());
mysql_select_db("db_name")or die(mysql_error());
$sql1 = mysql_query("SELECT * FROM people");
$numsql1 = mysql_numrows($sql1);
if ($numsql1 == 0) {
echo "No records at this time. Please try later";
}
$i = 0;
while ($i < $numsql1) {
$name = mysql_result($sql1,$i,"name");
$age = mysql_result($sql1,$i,"age");
echo "Name: $name<br> Age: $age<br>";
++$i;
}
mysql_close();
?>
OK, I'll explain.
mysql_connect("db_host","db_user","db_pass")or die(mysql_error());
mysql_select_db("db_name")or die(mysql_error());
This simply connects to the Database as we discussed before.
$sql1 = mysql_query("SELECT * FROM people");
This' the query. It extracts data from the database. There are many things you can do with a query. If you want to only select certain fields with certain bits of information, you write:
$condition="Something";
mysql_query("SELECT * FROM people WHERE fieldname='$condition'");
You can even order records e.g. $sql1 = mysql_query("SELECT * FROM people WHERE fieldname='$condition' ORDER BY name ASC"); which will order records by name Alphabeticaly.
$numsql1 = mysql_numrows($sql1);
This just counts how many rows there are in the database.
if ($numsql1 == 0) {
echo "No records at this time. Please try later";
}
This simply means: If there are no records, then display text 'No records at this time'.
$i = 0;
This' very important, you need this everytime you do this type of code.
while ($i < $numsql1) {
This sets up the loop. It means if 0 (The value of i) is less than the number of rows in Database then do whatever code is below for every single record so if you wrote: while ($i < $numsql1) {
echo "Yes<br>"; You would get the word 'Yes' written for every record.
$name = mysql_result($sql1,$i,"name");
$age = mysql_result($sql1,$i,"age");
This is grabbing the information from the DB
The rest of the code you can work out for yourself apart from the ++$i; YOU NEED THIS!!!
PART 1: INSERTING DATA!
For this to work, you need to already have a MySQL database with the following table:
- People
You need the following field names:
>> Name
>> Age
------------------------------
Page 1: Insert info.html
-------------------------------
<HTML>
<title>Sample form</title>
<body>
<form method="POST" action="addinfo.php">
Name:<input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
I'm sure I don't need to break this down, just a normal HTML form!
-------------------------
page 2: addinfo.php
-------------------------
<HTML>
<title>Adding information...</title>
<body>
<?PHP
$name = $_POST['name'];
$age = $_POST['age'];
mysql_connect("db_host","db_user","db_pass")or die(mysql_error());
mysql_select_db("db_name")or die(mysql_error());
mysql_query("INSERT INTO people VALUES('$name','$age')");
echo "The following values:<br>
Name: $name<br>
Age: $age<br>
was successfully added to the db";
mysql_close();
?>
OK, let me break it down.
$name = $_POST['name'];
$age = $_POST['age'];
The code above simply converts the values in the form into varibles so it can be read by PHP.
mysql_connect("db_host","db_user","db_pass")or die(mysql_error());
mysql_select_db("db_name")or die(mysql_error());
The code above connects to the database. The 'or die' command is just error control, if the script can't connect to the DB, it will come up with a message
mysql_query("INSERT INTO people VALUES('$name','$age')");
The code above just inserts the values of the form into the database.
echo "The following values:<br>
Name: $name<br>
Age: $age<br>
was successfully added to the db";
This piece of code will display in the users browser what values were added.
mysql_close();
This piece of code simply closes the Database connection
----------------------------------
Page 3: viewinfo.php
----------------------------------
<HTML>
<title>Displaying information</title>
<body>
<?PHP
mysql_connect("db_host","db_user","db_pass")or die(mysql_error());
mysql_select_db("db_name")or die(mysql_error());
$sql1 = mysql_query("SELECT * FROM people");
$numsql1 = mysql_numrows($sql1);
if ($numsql1 == 0) {
echo "No records at this time. Please try later";
}
$i = 0;
while ($i < $numsql1) {
$name = mysql_result($sql1,$i,"name");
$age = mysql_result($sql1,$i,"age");
echo "Name: $name<br> Age: $age<br>";
++$i;
}
mysql_close();
?>
OK, I'll explain.
mysql_connect("db_host","db_user","db_pass")or die(mysql_error());
mysql_select_db("db_name")or die(mysql_error());
This simply connects to the Database as we discussed before.
$sql1 = mysql_query("SELECT * FROM people");
This' the query. It extracts data from the database. There are many things you can do with a query. If you want to only select certain fields with certain bits of information, you write:
$condition="Something";
mysql_query("SELECT * FROM people WHERE fieldname='$condition'");
You can even order records e.g. $sql1 = mysql_query("SELECT * FROM people WHERE fieldname='$condition' ORDER BY name ASC"); which will order records by name Alphabeticaly.
$numsql1 = mysql_numrows($sql1);
This just counts how many rows there are in the database.
if ($numsql1 == 0) {
echo "No records at this time. Please try later";
}
This simply means: If there are no records, then display text 'No records at this time'.
$i = 0;
This' very important, you need this everytime you do this type of code.
while ($i < $numsql1) {
This sets up the loop. It means if 0 (The value of i) is less than the number of rows in Database then do whatever code is below for every single record so if you wrote: while ($i < $numsql1) {
echo "Yes<br>"; You would get the word 'Yes' written for every record.
$name = mysql_result($sql1,$i,"name");
$age = mysql_result($sql1,$i,"age");
This is grabbing the information from the DB
The rest of the code you can work out for yourself apart from the ++$i; YOU NEED THIS!!!