update,
This commit is contained in:
46
_resources/it114105/itp4513/Lab04/Lab04_1.php
Normal file
46
_resources/it114105/itp4513/Lab04/Lab04_1.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Lab04 Task1</title>
|
||||
</head>
|
||||
<body>
|
||||
<?php
|
||||
$servername = "localhost";
|
||||
$username = "root";
|
||||
$password = "";
|
||||
$db = "lab04";
|
||||
|
||||
// Create connection
|
||||
$conn = new mysqli($servername, $username, $password, $db);
|
||||
|
||||
// Check connection
|
||||
if ($conn->connect_error) {
|
||||
die("Connection failed: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
$sql = "SELECT * FROM customers";
|
||||
$result = $conn->query($sql);
|
||||
?>
|
||||
<table border="1">
|
||||
<tr>
|
||||
<th>Cust ID</th>
|
||||
<th>Cust Name</th>
|
||||
<th>Cust Pswd</th>
|
||||
<th>Cust Gender</th>
|
||||
</tr>
|
||||
<?php
|
||||
while($row = $result->fetch_assoc()) {
|
||||
echo "<tr>";
|
||||
echo "<td>$row[custID]</td>";
|
||||
echo "<td>$row[custName]</td>";
|
||||
echo "<td>$row[custPswd]</td>";
|
||||
echo "<td>$row[custGender]</td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
?>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
54
_resources/it114105/itp4513/Lab04/Lab04_2.php
Normal file
54
_resources/it114105/itp4513/Lab04/Lab04_2.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Lab04 Task1</title>
|
||||
</head>
|
||||
<body>
|
||||
<?php
|
||||
$servername = "localhost";
|
||||
$username = "root";
|
||||
$password = "";
|
||||
$db = "lab04";
|
||||
|
||||
// Create connection
|
||||
$conn = new mysqli($servername, $username, $password, $db);
|
||||
|
||||
// Check connection
|
||||
if ($conn->connect_error) {
|
||||
die("Connection failed: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
$sql = "SELECT * FROM customers";
|
||||
$result = $conn->query($sql);
|
||||
?>
|
||||
<table border="1">
|
||||
<tr>
|
||||
<th>Cust ID</th>
|
||||
<th>Cust Name</th>
|
||||
<th>Cust Pswd</th>
|
||||
<th>Cust Gender</th>
|
||||
</tr>
|
||||
<?php
|
||||
while($row = $result->fetch_assoc()) {
|
||||
echo "<tr>";
|
||||
echo "<td>$row[custID]</td>";
|
||||
echo "<td><input type='text' value='$row[custName]'</td>";
|
||||
echo "<td><input type='text' value='$row[custPswd]'</td>";
|
||||
echo "<td>";
|
||||
if($row["custGender"] == 'M'){
|
||||
echo "<input type='radio' name='custGender-$row[custID]' value='M' checked/> Male";
|
||||
echo "<input type='radio' name='custGender-$row[custID]' value='F'/> Female";
|
||||
}else{
|
||||
echo "<input type='radio' name='custGender-$row[custID]' value='M'/> Male";
|
||||
echo "<input type='radio' name='custGender-$row[custID]' value='F' checked/> Female";
|
||||
}
|
||||
echo "</td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
?>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
62
_resources/it114105/itp4513/Lab04/Lab04_3.php
Normal file
62
_resources/it114105/itp4513/Lab04/Lab04_3.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Lab04 Task 3</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<form method="post">
|
||||
<h1>Add a new customer</h1>
|
||||
<p>Customer ID <input type="text" name="custID" value="" /></p>
|
||||
<p>Customer Name <input type="text" name="custName" value=""></p>
|
||||
<p>Password <input type="password" name="custPwd" value="" /></p>
|
||||
<p>Gender <input type="radio" name="gender" value="M" checked>Male <input type="radio" name="gender" value="F">Female</p>
|
||||
<p><input type="submit" value="Submit"><input type="reset" value="Reset"></p>
|
||||
</form>
|
||||
<?php
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
|
||||
extract($_POST);
|
||||
$hostname = "localhost";
|
||||
$username = "root";
|
||||
$pwd = "";
|
||||
$db = "lab04";
|
||||
|
||||
$conn = new mysqli($hostname, $username, $pwd, $db);
|
||||
if ($conn->connect_error) {
|
||||
die("Connection Failed: " . $conn->connect_error);
|
||||
}
|
||||
|
||||
$sql = "SELECT COUNT(*) AS total FROM customers WHERE custID = ?";
|
||||
$stmnt = $conn->prepare($sql);
|
||||
$stmnt->bind_param('s', $custID);
|
||||
$stmnt->execute();
|
||||
|
||||
$result = $stmnt->get_result();
|
||||
$result = $result->fetch_assoc();
|
||||
if($result['total'] > 0){
|
||||
echo "<h1>Record already exist!</h1>";
|
||||
$conn->close();
|
||||
}else{
|
||||
$sql = "INSERT INTO customers VALUES(?,?,?,?)";
|
||||
$stmnt = $conn->prepare($sql);
|
||||
$stmnt->bind_param('ssss', $custID, $custName, $custPwd, $gender);
|
||||
$stmnt->execute();
|
||||
|
||||
if($stmnt->affected_rows == 1){
|
||||
echo "<h1>A record is added successfully</h1>";
|
||||
}else{
|
||||
echo "Unknow Error";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
||||
</body>
|
||||
|
||||
</html>
|
Reference in New Issue
Block a user