Displaying and Fetching Database Records in PHP
تبليغيرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.
I’m trying to create a program that accepts user input, but my problem is some of the data is missing when I insert them inside their corresponding field.
here is my HTML code:
<!———————–ADD MODAL——————–>
<div class=”modal fade” id=”studentaddmodal” tabindex=”-1″ role=”dialog” aria-labelledby=”exampleModalLabel”
aria-hidden=”true”>
<div class=”modal-dialog” role=”document”>
<div class=”modal-content”>
<div class=”modal-header”>
<h5 class=”modal-title” id=”exampleModalLabel”>ADD ADMIN</h5>
</div>
<form action=”insertcode_admin.php” method=”POST”>
<div class=”modal-body”>
<div class=”form-group”>
<div class = “row”>
<div class = “col mb-12″>
<label>First Name</label>
<input type=”text” name=”fname” id=”fname” class=”form-control” placeholder=”Firstname” required>
</div>
<div class = “col mb-12″>
<label>Lastname</label>
<input type=”text” name=”lname” id=”lname” class=”form-control” placeholder=”Lastname” required>
</div>
</div>
</div>
<div class=”form-group”>
<label>Birthdate</label>
<input type=”date” name=”bday” id=”bday” class=”form-control” max=”2004-07-10″ required>
</div>
<div class=”form-group”>
<label>Email</label>
<input type=”email” name=”email” id=”email” class=”form-control” placeholder=”example@email.com” required>
</div>
<div class=”form-group”>
<label>Address</label>
<input type=”text” name=”address” id=”address” class=”form-control” placeholder=”City, Country” required>
</div>
<div class=”form-group”>
<label>Password</label>
<input type=”password” name=”pass” id=”pass” class=”form-control” placeholder=”8 characters minimun” minlength=”8″ required>
</div>
<div class=”form-group”>
<label>Confirm Password</label>
<input type=”password” name=”cpass” id=”cpass” class=”form-control” placeholder=”8 characters minimun” minlength=”8″ required>
</div>
</div>
<div class=”modal-footer”>
<button type=”button” class=”btn btn-secondary” data-dismiss=”modal”>Cancel</button>
<button type=”submit” name=”insertdata” class=”btn btn-primary”>Add Admin</button>
</div>
</form>
</div>
</div>
</div>
<!—————–TABLE———————>
<div class=”container”>
<div class=”input-group mb-3″>
<input type=”text” name=”search” class=”form-control” placeholder=”Search Products”>
<button class=”btn btn-success” style=”width: 15vh;” type=”submit” id=”button-addon2″>Search</button>
</div>
<div class=”card”>
<div class=”card-body”>
<?php
$connection = mysqli_connect(“localhost”,”root”,””);
$db = mysqli_select_db($connection, ‘orginal_burger’);
$query = “SELECT * FROM admin”;
$query_run = mysqli_query($connection, $query);
?>
<table id=”datatableid” class=”table table-hover”>
<thead>
<tr>
<th scope=”col”>AID</th>
<th scope=”col”>Firstname</th>
<th scope=”col”>Lastname</th>
<th scope=”col”>Birthdate</th>
<th scope=”col”>Email</th>
<th scope=”col”>Address</th>
<th scope=”col”>Date Created</th>
<th scope=”col”>Action</th>
</tr>
</thead>
<?php
if($query_run)
{
foreach($query_run as $row)
{
?>
<tbody>
<tr>
<td> <?php echo $row[‘aid’]; ?> </td>
<td> <?php echo $row[‘firstname’]; ?> </td>
<td> <?php echo $row[‘lastname’]; ?> </td>
<td> <?php echo $row[‘birthdate’]; ?> </td>
<td> <?php echo $row[’email’]; ?> </td>
<td> <?php echo $row[‘address’]; ?> </td>
<td> <?php echo $row[‘date_created’]; ?> </td>
<td>
<button type=”button” class=”btn btn-success editbtn”><i class=’bx bx-edit-alt’ ></i></button>
<button type=”button” class=”btn btn-danger deletebtn”><i class=’bx bx-trash’ ></i></button>
</td>
</tr>
</tbody>
<?php
}
}
else
{
echo “No Record Found”;
}
?>
</table>
</div>
</div>
<div class=”card-body”>
<p style=”text-align:right;”><button type=”button” class=”btn btn-primary” data-toggle=”modal” data-target=”#studentaddmodal”>Add Admin</button></p>
</div>
</div>
</section>
here is my PHP code:
<?PHP
$server = “localhost:3306”;
$user = “root”;
$pass = “”;
$conn = mysqli_connect($server, $user, $pass);
mysqli_select_db($conn, “orginal_burger”);
if (isset($_POST[‘insertdata’])) {
$fname = $_POST[‘firstname’];
$lname = $_POST[‘lastname’];
$bday = $_POST[‘birthdate’];
$email = $_POST[’email’];
$address = $_POST[‘address’];
$pass = md5($_POST[‘password’]);
$cpass = md5($_POST[‘cpassword’]);
if ($pass == $cpass) {
$sql = “SELECT * FROM admin WHERE email =’$email'”;
$result = mysqli_query($conn, $sql);
if (!$result->num_rows > 0) {
$sql = “INSERT INTO `admin`(`firstname`, `lastname`, `birthdate`, `email`, `address`, `password`)
VALUES (‘$fname’,’$lname’,’$bday’,’$email’,’$address’,’$pass’)”;
$result = mysqli_query($conn, $sql);
if ($result) {
echo “<script>alert(‘Admin Record Added Successfully’)</script>”;
header(‘Location: Admin_Admin.php’);
} else {
echo “<script>alert(‘Something went wrong. Please try again!’)</script>”;
}
} else {
echo “<script>alert(‘Email already taken. Please try agin!’)</script>”;
}
} else {
echo “<script>alert(‘Password do not matched. Please try again!’)</script>”;
}
}
?>
When I’m trying to insert records inside the table the records in firstname, lastname and birthdate is missing. How do i fix this? thank you in advance!
أضف إجابة