Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions addAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,33 @@
echo "<font color='red'>Email field is empty.</font><br/>";
}

// Show link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<font color='red'>The email address '$email' is considered invalid.</font><br/>";

// Show link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// If all the fields are filled (not empty)

// Insert data into database
$result = mysqli_query($mysqli, "INSERT INTO users (`name`, `age`, `email`) VALUES ('$name', '$age', '$email')");

// Display success message
echo "<p><font color='green'>Data added successfully!</p>";
echo "<a href='index.php'>View Result</a>";
try {
// Insert data into database
$result = mysqli_query($mysqli, "INSERT INTO users (`name`, `age`, `email`) VALUES ('$name', '$age', '$email')");

// Kalau query berhasil, tampilkan success message
if ($result) {
echo "<p><font color='green'>Data added successfully!</font></p>";
echo "<a href='index.php'>View Result</a>";
}

} catch (mysqli_sql_exception $e) {
if ($e->getCode() == 1062) { // 1062 = Duplicate entry
echo "Email must be unique.";
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
}
}

}
}
?>
Expand Down
2 changes: 1 addition & 1 deletion database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`age` int(3) NOT NULL,
`email` varchar(100) NOT NULL,
`email` varchar(100) UNIQUE NOT NULL,
PRIMARY KEY (`id`)
);
32 changes: 26 additions & 6 deletions editAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,32 @@
if (empty($email)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}

// Show link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<font color='red'>The email address '$email' is considered invalid.</font><br/>";

// Show link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// Update the database table
$result = mysqli_query($mysqli, "UPDATE users SET `name` = '$name', `age` = '$age', `email` = '$email' WHERE `id` = $id");

// Display success message
echo "<p><font color='green'>Data updated successfully!</p>";
echo "<a href='index.php'>View Result</a>";

try {
// Update the database table
$result = mysqli_query($mysqli, "UPDATE users SET `name` = '$name', `age` = '$age', `email` = '$email' WHERE `id` = $id");

// Kalau query berhasil, tampilkan success message
if ($result) {
// Display success message
echo "<p><font color='green'>Data updated successfully!</p>";
echo "<a href='index.php'>View Result</a>";
}

} catch (mysqli_sql_exception $e) {
if ($e->getCode() == 1062) { // 1062 = Duplicate entry
echo "Email must be unique.";
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
}
}
}
}