<?php // require_once('./include/header.php'); ?>
<div class="container">
<?php
if(isset($_POST['submit'])) {
$user_name = trim($_POST['full_names']);
$user_email = trim($_POST['contact_no']);
if(empty($user_name) || empty($user_email)) {
$error = true;
} else {
$dbh = mysqli_connect('localhost', 'root', '', 'projectone');
//connect to MySQL server
if (!$dbh)
die("Unable to connect to MySQL: ");
//if connection failed output error message
if (!mysqli_select_db($dbh,'projectone'))
die("Unable to select database: ");
//if selection fails output error message
$sql_stmt = "INSERT INTO my_personal_contacts (full_names,gender,contact_no,email,city,country)";
$sql_stmt .= " VALUES ($_POST[full_names], $_POST[gender], $_POST[contact_no], $_POST[email], $_POST[city], $_POST[country])";
//echo $sql_stmt;die;
$result = mysqli_query($dbh, $sql_stmt); //execute SQL statement
if (!$result)
echo "error";
//die("Adding record failed: " . mysqli_error());
//output error message if query execution failed echo "Poseidon has been successfully added to your contacts list";
mysqli_close($dbh); //close the database connection
}
}
?>
<form class="py-4" action="index.php" method="POST">
<div class="row">
<div class="col">
<input type="text" name="full_names" class="form-control" placeholder="full_names">
</div>
<div class="col">
<input type="text" name="gender" class="form-control" placeholder="Username">
</div>
<div class="col">
<input type="text" name="contact_no" class="form-control" placeholder="Username">
</div>
<div class="col">
<input type="text" name="email" class="form-control" placeholder="Username">
</div>
<div class="col">
<input type="text" name="city" class="form-control" placeholder="Username">
</div>
<div class="col">
<input type="text" name="country" class="form-control" placeholder="Email Address">
</div>
<div class="col">
<input type="submit" name="submit" class="form-control btn btn-secondary" value="Add New User">
<?php echo isset($error) ? "<p>name i gender obiyzatelno</p>": ''; ?>
</div>
</div>
</form>
<h2>All Users</h2>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Username</th>
<th scope="col">Email</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<?php
if(isset($_POST['delete'])) {
$user_id = $_POST['val'];
$sql = 'DELETE FROM users WHERE user_id = ?';
$stmt = mysqli_stmt_init($link);
if(!mysqli_stmt_prepare($stmt, $sql)) {
die("Failed");
} else {
mysqli_stmt_bind_param($stmt, 'i', $user_id);
mysqli_stmt_execute($stmt);
header("Location: http://localhost/projectone/index.php");
}
}
?>
</div>
<?php require_once('./include/footer.php'); ?>