I’m having trouble running this php code with my database in phpmyadmin in cpanel

  html, mysql, php, phpmyadmin, sql

I’m having trouble running this code in phpmyadmin. It is not storing anything in the RESERVATIONS table. I’ve spent hours trying to figure this out. I tried putting the queries in manually in phpmyadmin and that was successful. However, running it in the php file is another story. The table is called RESERVATIONS and it has columns:

image of table RESERVATIONS columns

<?php
    require 'dbconfig/config.php';
?>

<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body style="background-color:#bdc3c7">

    <div id="main-wrapper">
        <center>
            <h2>Registration Form</h2>
            
        
        <form class="GuestForm">
    <label for="fname">First Name:</label>
    <input type="text" name="fname" id="fname" required>
    <hr>
    <label for="lname">Last Name:</label>
    <input type="text" name="lname" id="lname" required>
    <hr>
    <label for="phoneNum">GuestPhone:</label>
    <input type="text" name="phoneNum" id="phoneNum" required>
    <hr>
    <label for="emailaddr">GuestEmail:</label>
    <input type="text" name="emailaddr" id="emailaddr" required>
    <hr>
    <label for="dateNtime">DateTime:</label>
    <input type="text" name="dateNtime" id="dateNtime" required>
    <hr>
    <label for="partySize">Party Size:</label>
    <input type="number" name="partySize" id="partySize" min="0" max="10" required>
        <input name="submit_btn" type="submit" id="signup_btn" value="Create Reservation"/><br>
</form>
        <?php
            if(isset($_POST['submit_btn']))
            {
                echo '<script type="text/javascript"> alert("Submit button clicked") </script>';
                $fname = $_POST['fname'];
                $lname = $_POST['lname'];
                $phoneNum = $_POST['phoneNum'];
                $emailaddr = $_POST['emailaddr'];
                $dateNtime = $_POST['dateNtime'];
                $partySize = $_POST['partySize'];
                
                
                    $query = "SELECT dateNtime from RESERVATIONS WHERE dateNtime = '$dateNtime'";
                    $query_run = mysqli_query($con,$query);
                    //$length = mysql_field_len(username, 0);
                    
                    if(mysqli_num_rows($query_run)>0) 
                    {
                        // There is already a reservation for that particular date and time
                        echo '<script type="text/javascript"> alert("There is already a reservation for that particular date and time") </script>';
                    }
                    else{
                        $result = mysqli_query($conn, 'SELECT SUM(table_size) AS value_sum FROM DININGTABLES WHERE occupied = 0'); 
                        $row = mysqli_fetch_assoc($result); 
                        $sum = $row['value_sum'];
                        echo $sum;
                        
                        
                        $query = "INSERT into RESERVATIONS(reservation_id, registered_user, username, fname,lname,phoneNum, emailaddr, dateNtime, partySize) values('NULL', 'NULL', 'NULL','$fname','$lname', '$phoneNum', '$emailaddr', '$dateNtime', '$partySize')";
                        $query_run = mysqli_query($con,$query);
                        
                        if($query_run)
                        {
                            echo '<script type="text/javascript"> alert("Reservation created but not really") </script>';
                        }
                        else
                        {
                            echo '<script type="text/javascript"> alert("Error!") </script>';
                        }
                        
                
                
                
                
            }
            }
        ?>
        </center>
    </div>
</body>
</html>

This isn’t meant to be a public thing, just for a school project. I’m not concerned about security since it won’t be deployed

Source: Ask PHP

LEAVE A COMMENT