0

I want it to only register a "skap" if the field is empty. If the field already has been set, then I want it to skip everything, and give an error, "You have already registered a 'skap'"

It does not give me any errors, and I'm quite a noob with PHP and SQL, so I come seeking for help, I've tried googling, but I cant really find any answers.

        // If query successfull, check if there are more then 1 row, if true, then run queries.
        if ($resultTestQuery1->num_rows > 0) {
            while ($rowtest = mysqli_fetch_assoc($resultTestQuery1));
                if ($rowtest['Skap'] === "") {
                    mysqli_query($connect_DB, "UPDATE Elever SET Skap = '$inputSkap_check' WHERE ElevNr = '$inputElevnr_check' ");
                    mysqli_query($connect_DB, "UPDATE testskap SET Status='Aktiv', Eier_Fornavn='$inputFornavn_check', Eier_Etternavn='$inputEtternavn_check', Eier_Telefon='$inputTelefon_check' WHERE Skap_Nummer='$inputSkap_check'");
                    echo "Bestilt skap " . $inputSkap_check . " for " . $inputFornavn_check . " " . $inputEtternavn_check . " med elev nummer: " . $inputElevnr_check;      
                } else {
                    echo "Bruker har allerede registrert skap";
                }
        } else {
            echo "Finner ingen bruker med navn " . $inputFornavn_check . " " . $inputEtternavn_check . " med elev nummer: " . $inputElevnr_check;
        }
NIDOIT
  • 45
  • 6
  • Try `$rowtest['Skap'] == ""` (instead of === - see http://stackoverflow.com/questions/2063480/the-3-different-equals). Or maybe the Skap field can be null? In that case try `$rowtest['Skap'] == "" || is_null($rowtest['Skap'])` – ADyson Jul 05 '16 at 12:25
  • I've tried :/ I get no errors, but the field in my database just changes. Which is why im wondering if there is a different method to do this. It just always runs the if's true statement. – NIDOIT Jul 05 '16 at 12:51
  • have you tried putting `echo $rowtest['Skap'];` just before the if statement, then you can see what PHP thinks is in that field? – ADyson Jul 05 '16 at 13:05
  • For simple implementation you can set skap field as unique (with allow null values), and insert without prechecking of existing values. after you can see number of affected rows (inserted/updated) if it's greater than zero means insert/update was successful – George G Jul 05 '16 at 13:07
  • Yeah, but its kinda not unique. What this means is that if the value is set for that one "person" that "person" can not get another locker, nor change it. – NIDOIT Jul 05 '16 at 13:22

0 Answers0