0

I want to insert data from website but my website still can't insert to database mysql. whats wrong with my php code?

this is the html file

<form class="sign-in-up-form" action="book.inc.php" method="POST">
                    <p class="section-title">Booking Form</p>
                    <input class="input-field" type="text" id="name" name="UserName" placeholder="Full name" required>
                    
                    <input class="input-field" type="tel" id="contact" name="UserContact" placeholder="Contact Number" required>
                    
                    <input class="input-field" type="email" id="email" name="UserEmail" placeholder="Email" required>
                    
                    <input id="datetime-book" class="input-field book-field" type="datetime-local" id="date" name="aptDateTime" required>
                    
                    <select class="input-field book-field" name="artistName" id="selArtist" required>
                        <option id="select-placeholder" value="">Please choose an artist</option>
                        <option value="Aurelius">Aurelius</option>
                        <option value="Crocks">Crocks</option>
                    </select>

                    <textarea id="textarea-tattoo-idea" name="aptComment" class="input-field" placeholder="Describe your tattoo idea here..."></textarea>

                    <input class="submit-btn" type="submit" name="submit" value="book">
                </form>

this is the php file

<?php
session_start();
// connect to database
$con = mysqli_connect("localhost","root","","yellowstudios");

// to collect data
if(isset($_POST['submit'])) {

    $name = $_POST['UserName'];
    $contact = $_POST['UserContact'];
    $email = $_POST['UserEmail'];
    $date = $_POST['aptDateTime'];
    $artist = $_POST['artistName'];
    $text = $_POST['aptText'];
    $stat = $_POST['aptStat'];

    // will insert to DB
    $query = "INSERT INTO UserRecord (UserName, UserContact, UserEmail, aptDateTime, artistName, aptText, aptStat) VALUES ($name, $contact, $email, $date, $artist, $text, $stat)";
    $query_run = mysqli_query($con, $query);

    if($query_run)
    {
        // will execute if booking is successful, and will go to next page
        $_SESSION['status'] = "Appointment inserted successfully!";
        header("Location: book.output.php");
    }
    else
    {
        // will execute if booking is not successful, and will go to next page
        $_SESSION['status'] = "Appointment not inserted!";
        header("Location: book.output.php");
    }
}


I looked for typos and everything, but it won't insert data to database. I also double checked everything.

trish
  • 1
  • 1
  • What's `, ?` supposed to do in your query? That's a placeholder that should be replaced by something – brombeer Dec 29 '22 at 11:01
  • sorry i forgot to update the php file. can you see the updated one – trish Dec 29 '22 at 11:07
  • Hm, you had (correct) quotes in the first query but not in this. Take a look at prepared statements – brombeer Dec 29 '22 at 11:08
  • You should use prepared statements and parameters for a) reliability and b) security. This is a serious problem. See the duplicate in the blue box above, for details and instructions. Wherever you were taught to build queries the way you're doing now has done you a serious disservice - don't use that learning resource again. – ADyson Dec 29 '22 at 11:09
  • Also you should bring your error handling into the 21st century - see https://phpdelusions.net/mysqli/error_reporting for a good tutorial on that. – ADyson Dec 29 '22 at 11:10

0 Answers0