Very often you will need to use a MySQL table to store data inside it and then output that data by using a PHP script. To display the table data it is best to use HTML, which upon filling in some data on the page invokes a PHP script which will update the MySQL table.
To populate a new database table with data you will first need an HTML page which will collect that data from the user. The following HTML code that and passes the information to a PHP script:
1
2
3
4
5
6
7
|
<form action="insert.php" method="post">
Value1: <input type="text" name="field1-name" />
Value2: <input type="text" name="field2-name" />
Value3: <input type="text" name="field3-name" />
Value4: <input type="text" name="field4-name" />
Value5: <input type="text" name="field5-name" />
<input type="Submit" /></form>
|
The above HTML code will show the user 5 text fields, in which the user can input data and a Submit button. Upon clicking the Submit button the data submitted by the user will be passed to a script named insert.php.
That script can have a syntax similar to the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
$username = "your_username";
$password = "your_password";
$database = "your_database";
$field1-name=$_POST['Value1'];
$field2-name=$_POST['Value2'];
$field3-name=$_POST['Value3'];
$field4-name=$_POST['Value4'];
$field5-name=$_POST['Value5'];
$mysqli = new mysqli("localhost", $username, $password, $database);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO tablename VALUES('','$field1-name','$field2-name','$field3-name','$field4-name','$field5-name')";
$mysqli->query($query);
$mysqli->close();
?>
|
After the user submits the information, the insert.php script will save it in the database table. Then you may want to output that information, so that the user can see it on the page. The first command you will need to use is the SELECT FROM MySQL statement that has the following syntax:
1
|
SELECT * FROM tablename;
|
This is a basic MySQL query which will tell the script to select all the records from the tablename table. After the query is executed, usually you would want the result from it stored inside a variable. This can be done with the following PHP code:
1
2
|
$query2="SELECT * FROM tablename";
$result=$mysqli->query($query2);
|
The whole content of the table is now included in a PHP array with the name $result. Before you can output this data you should change each piece into a separate variable. There are two stages.
The first one is counting the rows. Before you can go through the data in your result variable, you should know the number of the database rows. You could, of course, just type this into your code but it is not a very good solution as the script code will have to be changed every time a new row is added. Instead you can use the command:
1
|
$num=$mysqli->mysqli_num_rows($result);
|
The $num value will be the number of rows stored in $result. This will be used in a loop to get all the data and display it on the screen.
The second stage is to set up the loop. It will take each row of the result and print the data stored there. In the code below, $iis the number of times the loop runs. This way all the records are displayed.
1
2
3
4
5
|
$i=0;
while ($i < $num) {
CODE
$i++;
}
|
This is a basic PHP loop and will execute CODE the correct number of times. Each time $i will be incremented by one. This is useful, as $i will tell the script which line of the results should be read. As the first line in MySQL output is 0, this will work correctly.
The final part of the output script is to assign each piece of data to its own variable:
1
2
3
4
5
|
$field1-name=mysql_result($result,$i,"field1-name");
$field2-name=mysql_result($result,$i,"field2-name");
$field3-name=mysql_result($result,$i,"field3-name");
$field4-name=mysql_result($result,$i,"field4-name");
$field5-name=mysql_result($result,$i,"field5-name");
|
You do not need to get the ID field because there is no use for it in the output page. You can now write a full script to output the data. In this script the data is not formatted when it is printed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php
$username="username";
$password="password";
$database="your_database";
$mysqli = new mysqli("localhost", $username, $password, $database);
@mysql_select_db($database) or die( "Unable to select database");
$query2="SELECT * FROM tablename";
$result=$mysqli->query($query2);
$num=$mysqli->mysqli_num_rows($result);
$mysqli->close();
echo "<b>
<center>Database Output</center>
</b>
<br>
<br>";
$i=0;
while ($i < $num) {
$field1-name=mysql_result($result,$i,"field1-name");
$field2-name=mysql_result($result,$i,"field2-name");
$field3-name=mysql_result($result,$i,"field3-name");
$field4-name=mysql_result($result,$i,"field4-name");
$field5-name=mysql_result($result,$i,"field5-name");
echo "<b>
$field1-name $field2-name2</b>
<br>
$field3-name<br>
$field4-name<br>
$field5-name<hr>
<br>";
$i++;
}
?>
|
This outputs a list of all the values stored in the database. This will give you a very basic output which is not useful for a live website. Instead, it would be better if you could format it into a table and display the information in it. To apply formatting you need to use HTML to print the result by including the variables in the correct spaces. The easiest way to do this is by closing the PHP tag and entering HTML normally. When you reach a variable position, include it as follows:
1
|
<? echo $variablename; ?>
|
in the correct position in your code.
You can also use the PHP loop to repeat the appropriate code and include it as part of a larger table. The final output is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<html>
<body>
<?php
$username="username";
$password="password";
$database="your_database";
$mysqli = new mysqli("localhost", $username, $password, $database); @mysql_select_db($database) or die( "Unable to select database");
$query2="SELECT * FROM tablename";
$result=$mysqli->query($query2);
$num=$mysqli->mysqli_num_rows($result);?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
<font face="Arial, Helvetica, sans-serif">Value1</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Value2</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Value3</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Value4</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Value5</font>
</td>
</tr>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"field1");
$f2=mysql_result($result,$i,"field2");
$f3=mysql_result($result,$i,"field3");
$f4=mysql_result($result,$i,"field4");
$f5=mysql_result($result,$i,"field5");?>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font>
</td>
</tr>
<?php
$i++;
}?>
</body>
</html>
|
This code will print out table content and add an extra row for each record in the database, formatting the data as it is printed.
0 comments:
Post a Comment