MySQL Form his assignment you will be making a form that will do one of three th
ID: 664746 • Letter: M
Question
MySQL Form
his assignment you will be making a form that will do one of three things in a database
- It will add a record
- It will update a record
- It will search for a record
Your database will contain a table for keeping a record of all your friends and family and should contain:
First name
Last name
Phone number
Address
City
State
Zip
Birthdate
Username
Password
The sex of the person
And the relationship
You should also keep a primary key for the user
You will then have two pages a friends and family form page and a results page
The form will ask the user to enter the information and then choose to either
Create a new record
Update and existing record
Search for a record
The results page will display the information that they are requesting with either adding a record, updating it or displaying the results of a record.
Your page need to deliver the PHP files in a zipped format
Explanation / Answer
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="insert.php">insert</a>
<a href="update.php">update</a>
<a href="search.php">search</a>
</body>
</html>
dbconfig.php:
<?php
$host = "localhost";
$user = "root";
$password = "";
$datbase = "dbtuts";
mysql_connect($host,$user,$password);
mysql_select_db($datbase);
?>
insert.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-save']))
{
// variables for input data
$first_name = $_POST['first_name'];
$phone_number = $_POST['phone_number'];
$address = $_POST['address'];
$city_name = $_POST['city_name'];
$state_name = $_POST['state_name'];
$zip_code = $_POST['zip_code'];
$birth_date = $_POST['birth_date'];
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$relation = $_POST['relation'];
// variables for input data
// sql query for inserting data into database
$sql_query = "INSERT INTO users(first_name,last_name,phone_number,address,user_city,user_state,zipcode,birthdate,username,password,gender,relationship) VALUES('$first_name','$last_name','$phone_number','$address','$city_name','$state_name','$zip_code','$birth_date','$username','$password','$gender','$relationship')";
mysql_query($sql_query);
$message = "New User Added Successfully";
// sql query for inserting data into database
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div class="message"><?php if(isset($message)) { echo $message; } ?></div>
<div id="header">
<div id="content">
<label>CRUD Operations With PHP and MySql - By Cleartuts</label>
</div>
</div>
<div id="body">
<div id="content">
<form method="post">
<table align="center">
<tr>
<td align="center"><a href="index.php">back to main page</a></td>
</tr>
<tr>
<td><input type="text" name="first_name" placeholder="First Name" required /></td>
</tr>
<tr>
<td><input type="text" name="last_name" placeholder="Last Name" required /></td>
</tr>
<tr>
<td><input type="text" name="phone_number" placeholder="Phone Number" required /></td>
</tr>
<tr>
<td><input type="text" name="address" placeholder="Address" required /></td>
</tr>
<tr>
<td><input type="text" name="city_name" placeholder="City" required /></td>
</tr>
<tr>
<td><input type="text" name="state_name" placeholder="State" required /></td>
</tr>
<tr>
<td><input type="text" name="zip_code" placeholder="Zip" required /></td>
</tr>
<tr>
<td><input type="text" name=birth_date placeholder="BirthDate" required /></td>
</tr>
<tr>
<td><input type="text" name="username" placeholder="username" required /></td>
</tr>
<tr>
<td><input type="text" name="password" placeholder="password" required /></td>
</tr>
<tr>
<td><input type="text" name="gender" placeholder="gender" required /></td>
</tr>
<tr>
<td><input type="text" name="relation" placeholder="relation" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-save"><strong>SAVE</strong></button></td>
</tr>
</table>
</form>
</div>
</div>
</center>
</body>
update.php:
<?php
include_once 'dbconfig.php';
if(isset($_GET['edit_id']))
{
$sql_query="SELECT * FROM users WHERE user_id=".$_GET['edit_id'];
$result_set=mysql_query($sql_query);
$fetched_row=mysql_fetch_array($result_set);
}
if(isset($_GET['edit_id']))
{
$sql_query="SELECT * FROM users WHERE user_id=".$_GET['edit_id'];
$result_set=mysql_query($sql_query);
$fetched_row=mysql_fetch_array($result_set);
}
if(isset($_POST['btn-update']))
{
// variables for input data
$first_name = $_POST['first_name'];
$phone_number = $_POST['phone_number'];
$address = $_POST['address'];
$city_name = $_POST['city_name'];
$state_name = $_POST['state_name'];
$zip_code = $_POST['zip_code'];
$birth_date = $_POST['birth_date'];
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$relation = $_POST['relation'];
// variables for input data
// sql query for update data into database
$sql_query = "UPDATE users SET first_name='$first_name',last_name='$last_name',phone_number='$phone_number',address='$address',user_city='$city_name',user_state='$state_name',zip_code='$zip_code',birth_date='$birth_date',username='$username',password='$password',gender='$gender',relationship='$relation' WHERE user_id=".$_GET['edit_id'];
mysql_query($sql_query));
$message = "User Updated Successfully";
// sql query for update data into database
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div class="message"><?php if(isset($message)) { echo $message; } ?></div>
<div id="body">
<div id="content">
<form method="post"><
<table align="center">
<tr>
<td><input type="text" name="first_name" placeholder="First Name" value="<?php echo $fetched_row['first_name']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="last_name" placeholder="Last Name" value="<?php echo $fetched_row['last_name']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="city_name" placeholder="City" value="<?php echo $fetched_row['user_city']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="state_name" placeholder="State" value="<?php echo $fetched_row['user_state']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="zip_code" placeholder="Zip" value="<?php echo $fetched_row['zip_code']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name=birth_date placeholder="BirthDate" value="<?php echo $fetched_row['birth_date']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="username" placeholder="username" value="<?php echo $fetched_row['username']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="password" placeholder="password" value="<?php echo $fetched_row['password']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="gender" placeholder="gender" value="<?php echo $fetched_row['gender']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="relation" placeholder="relation" value="<?php echo $fetched_row['relation']; ?>" required /></td>
</tr>
<tr> <tr>
<td>
<button type="submit" name="btn-update"><strong>UPDATE</strong></button>
</td>
</tr>
</table>
</form>
</div>
</div>
</center>
</body>
search.php:
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="body">
<div id="content">
<table align="center">
<tr>
<th colspan="5"><a href="add_data.php">add data here.</a></th>
</tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Address</th>
<th>City Name</th>
<th>State Name</th>
<th>Zip Code</th>
<th>Birth Date</th>
<th>username</th>
<th>password</th>
<th>gender</th>
<th>relationship</th>
<th colspan="2">Operations</th>
</tr>
<?php
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);
while($row=mysql_fetch_row($result_set))
{
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td><?php echo $row[4]; ?></td>
<td><?php echo $row[5]; ?></td>
<td><?php echo $row[6]; ?></td>
<td><?php echo $row[7]; ?></td>
<td><?php echo $row[8]; ?></td>
<td><?php echo $row[9]; ?></td>
<td><?php echo $row[10]; ?></td>
<td><?php echo $row[11]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</center>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.