You are on page 1of 3

//view.

php

<?php
include "config.php";
/*$db_host = "localhost";
$db_username = "root";
$db_passwd = "";
$conn= mysqli_connect($db_host, $db_username, $db_passwd) or die ("Could not
connect!\n");

echo "Connection established.\n";


$db_name = "db_sales";

mysqli_select_db($conn,$db_name) or die ("Could not select the database


$dbname!\n". mysql_error());

echo "Database $db_name is selected.\n";


*/

$result = mysqli_query( $conn,"SELECT * FROM customer" );


$num_rows = mysqli_num_rows( $result );
echo "There are currently $num_rows rows in the table<P>";
echo "<table border=1>\n";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>\n";
echo "\t<td>".$row['customer_id']."</td>\n";
echo "\t<td>".$row['fname']."</td>\n";
echo "\t<td>".$row['lname']."</td>\n";
echo "\t<td>".$row['addressline']."</td>\n";
echo "\t<td><a href='update.php?cust_id=" .$row['customer_id']."'>
view</a></td>\n";
echo "\t<td><a href='delete.php?cust_id=". $row['customer_id']."'>
delete</a></td>\n";
echo "</tr>\n";
}

echo "</table>\n";
mysqli_free_result($result);
mysqli_close( $conn );
?>
</body>
</html>

//update.php

<?php

include "config.php";

?>
<form method="GET" action="<?php echo "save.php"; ?>">

<pre>
Customer ID: <input type="text" name="customer_id" value="<?php echo
$_GET['cust_id'];?>">
</pre>

<?php
$result = mysqli_query( $conn,"SELECT * FROM customer where customer_id = ".
$_GET['cust_id']);
//$num_rows = mysqli_num_rows( $result );
//echo "There are currently $num_rows rows in the table<P>";
echo "<table border=1>\n";
while ($row = mysqli_fetch_array($result)) { ?>
<pre>
Title: <input type="text" name="title" value="<?php echo $row['title'];?
>"</br>
First Name: <input type="text" name="fname" value="<?php echo $row['fname'];?
>"></br>
Last Name: <input type="text" name="lname" value="<?php echo $row['lname'];?
>"></br>
Address: <input type="text" name="address" value="<?php echo
$row['addressline'];?>"></br>
Town: <input type="text" name="town" value="<?php echo $row['town'];?
>"</br>
Phone: <input type="text" name="phone" value="<?php echo $row['phone'];?
>"</br>
Zipcode: <input type="text" name="zip" value="<?php echo $row['zipcode'];?
>"</br>
<input type="submit" formaction="save.php" name="Submit" value="save"></br>
<input type="submit" formaction="edit.php" name="Submit" value="update">
</pre>
<?php }

echo "</table>\n";
echo "</form>";
mysqli_free_result($result);
mysqli_close( $conn );
?>
</body>
</html>

//save.php

<?php

include "config.php";

if ($_GET['Submit'] == "save"){
$title = $_GET['title'];
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$address = $_GET['address'];
$town = $_GET['town'];
$phone = $_GET['phone'];
$zip = $_GET['zip'];
echo $title;
$sql = "INSERT INTO CUSTOMER (title,fname,lname,addressline,town,zipcode,phone)
values ('$title','$fname','$lname','$address','$town','$phone','$zip');";
echo $sql;
$result = mysqli_query( $conn,$sql);
if ($result) {
echo "customer saved";

}
}
?>

//edit.php

<?php

include "config.php";

if ($_GET['Submit'] == "update"){
$customer_id = $_GET['customer_id'];
$title = $_GET['title'];
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$address = $_GET['address'];
$town = $_GET['town'];
$phone = $_GET['phone'];
$zip = $_GET['zip'];
echo $title;
$sql = "UPDATE CUSTOMER SET title = '$title',fname = '$fname',lname =
'$lname',addressline = '$address',town = '$town',zipcode = '$zip',phone = '$phone'
WHERE customer_id = " . $customer_id ;
echo $sql;
$result = mysqli_query( $conn,$sql);
if ($result) {

echo "customer updated";


//header("location: order.php");
}

?>

//delete.php

<?php

include "config.php";

$sql = "DELETE FROM CUSTOMER WHERE customer_id = ". $_GET['cust_id'];


echo $sql;
$result = mysqli_query( $conn,$sql);
if ($result) {

echo "customer deleted";

?>

You might also like