• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP if option 0 is choosen then set timestamp in col

220061

New Coder
Hello I'm kinda stuck here and I don't know what to do. I have this code where I want to (based on the choice that the user made) put the timestamp into my table

its goes like this if you choose option 1 then insert NULL into both the columns inlever_datum and uitleen_datum
if you choose option 0 then insert the current timestamp in Y-m-d into uitleen_datum and for inlever_datum put a day 7 days later (so current time + 7 days)
now I have this code
PHP:
<div class="container">
        <div class="form">
        <h2>Apparaten toevoegen</h2>
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
        <!--naam van het apparaat-->
        <label for="naam">Naam </label><br>
        <input type="text" name="naam" id="naam" required="required"> <br><br/><br/>
        <!--beschrijving van het product -->
        <label for="beschrijving">Beschrijving </label>
        <input type="text" name="beschrijving" id="beschrijving" required="required">
         <br><br/><br/>
        <!--beschikbaar dropdown-->
<!--this is where the option part is -->
        <label for="beschikbaar">Beschikbaar </label><br>
        <select name="beschikbaar" class="beschikbaar" value="true" required="required"'>
            <option hidden>--Select option--</option>
            <option value="1">Beschikbaar</option>
            <option value="0">Uitgeleend</option>
        </select>
        <!--submit button-->
        <br><br><input type="submit" value="Toevoegen" name="submit">
        <?php
        //link voor het stoppen van empty values
        //dit zou alles naar de database moeten sturen als je op de knop drukt
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
             date_default_timezone_set("America/New_York");
             switch($_POST['beschikbaar']){
                 case "1":
                     $date = date("Y/m/d"); //works
                     var_dump($date);
                     //empty table
                     $stmt = $conn->prepare("INSERT INTO apparatuur( inlever_datum, uitleen_datum ) VALUES ('%s', NULL)");
                     var_dump($stmt); // gives false??
                     $stmt->bind_param("i", $inlever_datum);
                     $stmt->execute();
                     break;
                case "0":
                     $date = date("Y/m/d");
                     var_dump($date);
                     // + 7 dagen
                     $stmt = $conn->prepare("INSERT INTO apparatuur( uitleen_datum ) VALUES ('%s', CURDATE())");
                     var_dump($stmt);
                     $stmt->bind_param("i", $uitleen_datum);
                     $stmt->execute();
                     break;
                 default:
                     echo "<p>U heeft geen optie gekozen</p>";
                 }
                 echo $stmt;
            //dit nog veranderen voor als optie 0 word aangeklikt
            //prepared statement
                $stmt = $conn->prepare("INSERT INTO apparatuur (naam, beschrijving,beschikbaar)
                VALUES (?,?,?)");
                $stmt->bind_param("ssi", $naam, $beschrijving,$beschikbaar);
                $stmt->execute();
                function_alert("Apparaat is toegevoegd");
                $conn->close();
        }
        ?>
        </form>
    </div>
    </div>
</body>
<footer>
<script>
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>
</footer>
</html>

what am I doing wrong here ???
 

220061

New Coder
You never said what's happening. BUY
$stmt = $conn->prepare("INSERT INTO apparatuur( uitleen_datum ) VALUES ('%s', CURDATE())");
has one column and two values.
just saw that but it still doesnt fix my errors :
Errors:
Notice: Query was empty[] in C:\xampp\htdocs\uitleensysteem\code\edit.php on line 151

Fatal error
: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\uitleensysteem\code\edit.php:152 Stack trace: #0 {main} thrown in C:\xampp\htdocs\uitleensysteem\code\edit.php on line 152


PHP:
<?php
        //link voor het stoppen van empty values
        //dit zou alles naar de database moeten sturen als je op de knop drukt
        
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
            date_default_timezone_set("America/New_York");
            $date = $_POST['beschikbaar'];
            switch($date){
                case "1":
                    $sql = $conn->prepare("INSERT INTO apparatuur( inlever_datum, uitleen_datum ) VALUES( NULL, NULL );");
                    $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
                    $stmt->bind_param('i', $inlever_datum);
                    $status = $stmt->execute();
                    break;
                case "0":
                    
                    $sql = $conn->prepare("INSERT INTO apparatuur( uitleen_datum ) VALUES( CURDATE() );");
                    $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
                    $stmt->bind_param('i', $uitlever_datum);
                    $status = $stmt->execute();
                    break;
                default:
                    echo "<p>U heeft geen optie gekozen</p>";
                }
 

220061

New Coder
did an update on it but right now the only problem that I have is that $stmt returns false. why is that?
PHP:
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
            date_default_timezone_set("America/New_York");
            $date = date("YYYY-MM-DD");
            switch($_POST['beschikbaar']){
                case "1":
                    $id = $_GET['id'];
                    //perfectly works
                    $sql = "UPDATE apparatuur SET inlever_datum=NULL, uitleen_datum=NULL WHERE id=$id";
                    $stmt = $conn->prepare($sql);
                    $stmt->bind_param('ssi', $inlever_datum, $uitleen_datum, $id);
                    $status = $stmt->execute();
                    break;
                case "0":
                    //doesn't work
                    $id = $_GET['id'];
                    $date = date("Y-m-d", strtotime($date));
                    $new_date = date('Y-M-d', strtotime($date. '+7 day'));
                    $sql = "UPDATE apparatuur SET inlever_datum = $new_date, uitleen_datum = $date WHERE id=$id";
                    $stmt = $conn->prepare($sql);
                    var_dump($stmt);// false???
                    $stmt->bind_param('iii', $inlever_datum, $uitleen_datum, $id);
                    $status = $stmt->execute();
                    break;
                default:
                    echo "<p>U heeft geen optie gekozen</p>";
                }
 

Top