[go: up one dir, main page]

0% found this document useful (0 votes)
13 views3 pages

Pertemuan 11

Uploaded by

yatimollahali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Pertemuan 11

Uploaded by

yatimollahali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Pertemuan11

Insert Data From a Form Into a Database

Now we will create an HTML form that can be used to add new records to the "Person" table.

Here is the HTML form:

<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

When a user clicks the submit button in the HTML form in the example above, the form data is sent
to "insert.php". The "insert.php" file connects to a database, and retrieves the values from the form
with the PHP $_POST variables. Then, the mysql_query() function executes the INSERT INTO
statement, and a new record will be added to the database table.

Below is the code in the "insert.php" page:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
The SELECT statement is used to select data from a database.

Select Data From a Database Table

The SELECT statement is used to select data from a database.

Syntax
SELECT column_name(s)
FROM table_name

Note: SQL statements are not case sensitive. SELECT is the same as select.

To get PHP to execute the statement above we must use the mysql_query() function. This function is
used to send a query or command to a MySQL connection.

Example

The following example selects all the data stored in the "Person" table
(The * character selects all of the data in the table):

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM person");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysql_close($con);
?>

The example above stores the data returned by the mysql_query() function in the $result variable.
Next, we use the mysql_fetch_array() function to return the first row from the recordset as an array.
Each subsequent call to mysql_fetch_array() returns the next row in the recordset. The while loop
loops through all the records in the recordset. To print the value of each row, we use the PHP $row
variable ($row['FirstName'] and $row['LastName']).

The output of the code above will be:

Peter Griffin
Glenn Quagmire

Display the Result in an HTML Table


The following example selects the same data as the example above,
but will display the data in an HTML table:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM person");

echo "<table border='1'>


<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

The output of the code above will be:

Firstname Lastname
Glenn Quagmire
Peter Griffin

You might also like