0

I have a table that I am trying to INSERT INTO three different columns (child1Name, dobChild1, ageChild1) using a prepared statement. I can insert the data into this table within phpmyadmin utilizing the SQL tab. I have checked my connection and the formatting of the prepared statement and all appears correct.

I make it through the connection and the stmt prepare error check no problem and for all intents and purposes make it through the mysqli_stmt_bind_param and the mysqli_stmt_execute($stmt). This is where things get interesting, although I make it through each step of the prepared statement the data will not be entered into the database.

I believe this is because of the date formatting but not 100% sure.

The question I am asking is if I am making it through all of the prepared statement why won't my data push to the table? Everything looks like it should work to me.

    include_once 'functions.inc.php';

    session_start();

    if (isset($_POST["submit"])) {
        $child1Name = $_POST["child1Name"];
        $dobChild1 = date('Y-m-d', strtotime($_POST['dobChild1']));
        $ageChild1 = $_POST["ageChild1"];

    $sql = "INSERT INTO children (child1Name, dobChild1, ageChild1) VALUES      (?,?,?);";

    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "SQL ERROR";
    } else {
        mysqli_stmt_bind_param($stmt, "ssi", 
        $child1Name, $dobChild1, $ageChild1);
        mysqli_stmt_execute($stmt);
    }

FORM

    <form action="includes/mf2.inc.php" method="POST">
        <div class="container text-center">
        <div id="oneChild" class="childInfo d-none">
            <div class="row">
            <div class="col-md-3"></div>
                <div class="col-md-2">
                    <label for="child1Name">
                        <span>1st Child Name:</span><br>
                        <input type="text" name="child1Name" class="child1Name" placeholder="John/Jane Doe"></input>
                    </label>
                </div>
                <div class="col-md-2">
                    <label for="DOB">
                        <span>Date of Birth</span><br>
                        <input type="date" name="dobChild1" class="dob"></input>
                    </label>
                </div>
                    <div class="col-md-2">
                        <label for="ageChild1">
                            <span>age</span><br>
                            <input type="number" name="ageChild1" class="ageChild1" placeholder="99"></input>
                        </label>
                    </div>
                </div>

        <div class="submitButton">
            <button type="submit" name="submit">Next</button>
        </div>

    </form>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    You are "making it through" the wrong way. Instead of adding those rather useless conditions, you should configure mysqli to report errors automatically. And it will readily tell you why insert failed – Your Common Sense Jul 27 '23 at 14:51
  • `although I make it through each step of the prepared statement`...how do you know? You only check for errors on some of them, not all. But as above, and as per the duplicate question, you can just enable proper mysqli error reporting and then you can find out what's really wrong. (Even on the statements where you did check the return value, just echoing "SQL ERROR" was never going to give you any information you could use to diagnose and fix the problem.) – ADyson Jul 27 '23 at 14:54
  • regarding the date, it's just a string and you add it just as any other string, absolutely nothing special – Your Common Sense Jul 27 '23 at 14:56
  • I wouldn't store `age` of user in DB. You have `dob` so just calculate the age when needed. Otherwise you're going to need something to update that. Also would suggest looking at https://en.wikipedia.org/wiki/First_normal_form I'm guessing this schema should be updated. – user3783243 Jul 27 '23 at 15:50
  • That's a good point User3783243 I apprecaite the heads up! Your Common Sense - Spot on thank you! Using your method I was able to determine my problem and this will help greatly in the future! You're all amazing and I appreciate your help! – Joshua Lee Jul 27 '23 at 16:27

0 Answers0