Input Data to Database with Form
Follow steps below to make form for input data into database. Let's follow steps.
1. Write script below use your text editor to make file connection to connect to localhost and select database and then save with name connect.php.
<?php
$server="localhost";
$user="root";
$password="";
mysql_connect("$server","$user","$password") or exit("Failed select DB.");
mysql_select_db("db_exercise") or exit("Failed select DB.");
?>
2. Write script below use your text editor to make form and save with name form_biodata.php.
<html>
<head>
<title>Form Biodata</title>
</head>
<body>
<h3>Form Biodata</h3>
<form action="process_input.php" method="post">
<table bg>
<tr>
<td>Name</td>
<td>:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td><textarea name="address"></textarea></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" value="Save"/></td>
</tr>
</table>
</form>
</body>
</html>
3. Write script below use your text editor to make file program process input and then save with name process_input.php.
<?php
include("connect.php");
$name=$_POST['name'];
$address=$_POST['address'];
$query="INSERT INTO biodata (name,address) VALUES ('".$name."','".$address."')";
$sql=mysql_query($query);
echo "Data Successfully Added";
echo "<br />";
include("view_data.php");
?>
4. Write script below use your text editor to make view data in database db_exercise and then save with name view_data.php.
<html>
<head>
<title>View Data</title>
</head>
<body>
<h2>List Biodata</h2>
<table width="600px "border="1">
<tr style="background:#ccc000">
<th width="10%">ID</th>
<th width="35%">Nama</th>
<th>Alamat</th>
</tr>
<?php
include("connect.php");
$query= "SELECT id,name,address FROM biodata";
$sql= mysql_query($query) or exit("Error query: <b>".$sql."</b>.");
while($data= mysql_fetch_assoc($sql)){ ?>
<tr>
<td align="center"><?php echo $data['id']; ?></td>
<td><?php echo $data['name']; ?></td>
<td><?php echo $data['address']; ?></td>
</tr>
<?php
}
?>
</table>
<br /><b><a href='form_biodata.php'>Add Data</a><b>
</body>
</html>
Now let's see output of scripts above.
Output of form_biodata.php.
Typing of data in form form_biodata.php.
Function include("view_data.php"); in file process_input.php is use to include the code in the file viwe_data.php in file process_input.php.
Learn and learn again...
Wah, keren...
ReplyDeleteTrima kasih :)
Delete