0

Ok so Im quite new to php so go easy xD

Im trying to add workers to a db through a form and its working fine as long as I enter a date in the date field. If I dont then nothing gets stored in the db. I have 2 fields that accept null ($unavaliable and $okompatible), the last is a string (varchar in db) the other is a javascript datepicker with yy-mm-dd and is date in db. what am I doing wrong?

<?php
//Hämta in databaskoppling (variabel $conn)
include 'dbh.inc.php';
//Lägg in formulärvärdena i egna variabler
$f_namn = $_POST['f_namn'];
$e_namn = $_POST['e_namn'];
$arbetsgrad = $_POST['arbetsgrad'];
$unavaliable = $_POST['unavaliable'];
$ledig_helg = $_POST['ledig_helg'];
$kan_oppna = $_POST['kan_oppna'];
$kan_stanga = $_POST['kan_stanga'];
$okompatibel = $_POST['okompatibel'];
$timlon = $_POST['timlon'];
//kontrollera om något obligatoriskt fält inte är ifyllt
if (empty($f_namn) || empty($e_namn) || empty($arbetsgrad) || empty($ledig_helg) || 
empty($kan_oppna) || empty($kan_stanga) || empty($timlon)) {
  header("Location ../schema.php/?Fält=ej_ifyllt");
} else {
  //Skapa SQL koden som lagrar alla fälten med platshållare (?)
  $sql = "INSERT INTO Medarbetare (f_namn, e_namn, arbetsgrad, unavaliable, ledig_helg, 
  kan_oppna, kan_stanga, okompatibel, timlon) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
  //Initierar ett prepared statment
  $stmt = mysqli_stmt_init($conn);
  //Kontrollerar om vårt Statement INTE skickades till och lagrades i databasen
  if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "SQL Error";
  } else {
    //Ersätter våra platshållare i databasen med den faktiskta informationen vi vill lagra
    mysqli_stmt_bind_param($stmt, "ssisssssd", $f_namn, $e_namn, $arbetsgrad, 
    $unavaliable, $ledig_helg, $kan_oppna, $kan_stanga, $okompatibel, $timlon);
    mysqli_stmt_execute($stmt);
  }
}
header("Location: ../schema.php?signup=success");
?>
  • To get errors out of PHP even in a LIVE environment add these 4 lines to the top of any `MYSQLI_` based script you want to debug `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. This will force any `MYSQLI_` errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Jul 21 '20 at 20:58
  • After any `header("Location .....");` you need an `exit;` as the header statement, sends a header to the browser, but it does not terminate the scripts execution – RiggsFolly Jul 21 '20 at 21:07

0 Answers0