ingerNorway Posted May 14, 2014 Share Posted May 14, 2014 Hi I wondering what i did wrong here:I wont to register 1 or 0 into database. Checkbox file: <?php include("db-tilkobling.php"); $sqlSetning="SELECT * FROM Kvarterbestilt ORDER BY Møtt;"; $sqlResultat=mysqli_query($db,$sqlSetning) or die ("Ikke mulig å hente data fra databasen"); $antallRader=mysqli_num_rows($sqlResultat); print("<input type='checkbox' name='møtt' value='$møtt'>"); ?>Register-møtt.php<?php include("start.html"); ?> <br/> <p>Registrer Møtt/Ikke møtt ved å fylle ut skjemaet under</p> <form method="post" action="" id="registrerAnsattSkjema" name="registrerAnsattSkjema"> <fieldset> <legend>Registrer møtt / ikke møttt</legend> <label for="personnummer">Personnummer</label> <?php include("listeboks-personnummer.php"); ?><br/> <label for="timenummer">Timenummer</label> <?php include("listeboks-timenummer.php"); ?><br/> Sjekk av denne boksen hvis pasient har møtt til time:<br/> <?php include("checkbox-møtt.php"); ?><br/> <input type="submit" name="registrerMottKnapp" id="registrerMottKnapp" value="Registrer møtt/ikke"> <input type="reset" name="nullstill" id="nullstill" value="nullstill"> </fieldset> </form><br/> <?php /* include("valider-mott.php");*/ $registrerMottKnapp=$_POST ["registrerMottKnapp"]; if ($registrerMottKnapp) { $personnummer=$_POST["Personnummer"]; $timenummer=$_POST["Timenummer"]; $møtt=$_POST["Møtt"]; } if ($møtt == '1') { $query = mysql_query("INSERT INTO Kvarterbestilt(Møtt) VALUES('1')"); } /* $lovligFornavn=validerFornavn ($fornavn); $lovligFornavn2=validerFornavn2 ($fornavn); $lovligFornavn3=validerFornavn3 ($fornavn); $lovligEtternavn=validerEtternavn ($etternavn); $lovligEtternavn2=validerEtternavn2 ($etternavn); $lovligEtternavn3=validerEtternavn3 ($etternavn); $lovligYrke=validerYrke ($yrke); $lovligYrke2=validerYrke2 ($yrke); $lovligYrke3=validerYrke3 ($yrke); if (!$lovligFornavn) { print("Fornavn er ikke fylt ut! <br/>"); } else if (!$lovligFornavn2) { print("Bare bokstaver og mellomrom er tillat; $fornavn<br/>"); } else if (!$lovligFornavn3) { print("Ingen tall tillat; $fornavn<br/>"); } else if (!$lovligEtternavn) { print("Etternavn er ikke fylt ut!<br/>"); } else if (!$lovligEtternavn2) { print("Bare bokstaver og mellomrom er tillat; $etternavn<br/>"); } else if (!$lovligEtternavn3) { print("Ingen tall tillat; $etternavn<br/>"); } else if (!$lovligYrke) { print("Yrke er ikke fylt ut!<br/>"); } else if (!$lovligYrke2) { print("Bare bokstaver og mellomrom er tillat; $yrke<br/>"); } else if (!$lovligYrke3) { print("Ingen tall tillat; $yrke<br/>"); } */ /* include("db-tilkobling.php"); $check=mysqli_query($db,"SELECT * FROM Kvarterbestilt WHERE Personnummer='$personnummer' AND Timenummer='$timenummer'"); $checkrows=mysqli_num_rows($check); if($checkrows>0){echo "Ansatt eksiterer fra før";} else { $sqlSetning="INSERT INTO Kvarterbestilt(Møtt) WHERE Personnummer='$personnummer' VALUES ('$møtt');"; mysqli_query ($db,$sqlSetning) or die ("Ikke mulig Ã¥ registrere i db"); print ("Det er registrert at pasienten har $møtt til timen $timenummer"); } } */ include("slutt.html"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/ Share on other sites More sharing options...
ingerNorway Posted May 14, 2014 Author Share Posted May 14, 2014 I can add,that when I run the code. NOTHING happens.... Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479431 Share on other sites More sharing options...
boompa Posted May 14, 2014 Share Posted May 14, 2014 (edited) $sqlSetning="INSERT INTO Kvarterbestilt(Møtt) WHERE Personnummer='$personnummer' VALUES ('$møtt');"; If you want to change the value of an existing record, you use an UPDATE query, not an INSERT: $sqlSetning="UPDATE Kvarterbestilt SET Møtt="'$møtt'" WHERE Personnummer='$personnummer'"; Edited May 14, 2014 by boompa Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479434 Share on other sites More sharing options...
ingerNorway Posted May 14, 2014 Author Share Posted May 14, 2014 $sqlSetning="INSERT INTO Kvarterbestilt(Møtt) WHERE Personnummer='$personnummer' VALUES ('$møtt');"; If you want to change the value of an existing record, you use an UPDATE query, not an INSERT: $sqlSetning="UPDATE Kvarterbestilt SET Møtt="'$møtt'" WHERE Personnummer='$personnummer'"; It's no data there in my database it's just say "NULL" Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479435 Share on other sites More sharing options...
cyberRobot Posted May 14, 2014 Share Posted May 14, 2014 Your checkbox name has a lower case "m": print("<input type='checkbox' name='møtt' value='$møtt'>"); But the code which processes the form submission uses and upper case "M": $møtt=$_POST["Møtt"]; Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479437 Share on other sites More sharing options...
ingerNorway Posted May 14, 2014 Author Share Posted May 14, 2014 Your checkbox name has a lower case "m": print("<input type='checkbox' name='møtt' value='$møtt'>"); But the code which processes the form submission uses and upper case "M": $møtt=$_POST["Møtt"]; That didn't make a diffrence.... Here you can see live view.. https://studhome.hive.no/inghor/web1000/innlevering3/vedlikehold/registrer-mott.php Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479439 Share on other sites More sharing options...
cyberRobot Posted May 14, 2014 Share Posted May 14, 2014 Have you tried modifying your PHP script to show all errors and warnings? To do that, you could add the following lines of code to the top of your script: <?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> You could also see if MySQL is throwing errors by using mysql_error() after each query. http://www.php.net/manual/en/function.mysql-error.php Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479443 Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 Are you aware that check box values are only posted if they are checked? If unchecked it will not post a zero value Use $mott = isset($_POST['mott'] ? $_POST['mott'] : 0; Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479445 Share on other sites More sharing options...
ingerNorway Posted May 14, 2014 Author Share Posted May 14, 2014 Have you tried modifying your PHP script to show all errors and warnings? To do that, you could add the following lines of code to the top of your script: <?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> You could also see if MySQL is throwing errors by using mysql_error() after each query. http://www.php.net/manual/en/function.mysql-error.php THANKS! Now I got this : Notice: Undefined index: registrerMottKnapp in/jail/www/inghor/web1000/innlevering3/vedlikehold/registrer-mott.php on line 33 But dont know what thats mean... Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479446 Share on other sites More sharing options...
cyberRobot Posted May 14, 2014 Share Posted May 14, 2014 (edited) THANKS! Now I got this : Notice: Undefined index: registrerMottKnapp in/jail/www/inghor/web1000/innlevering3/vedlikehold/registrer-mott.php on line 33 But dont know what thats mean... Since your form hasn't been submitted, the POST variables for "registrerMottKnapp" doesn't exist yet. To fix the error, you need to change this: $registrerMottKnapp=$_POST ["registrerMottKnapp"]; To something like this: $registrerMottKnapp = (isset($_POST['registrerMottKnapp'])) ? $_POST['registrerMottKnapp'] : false; Personally, I would eliminate the extra variable ($registrerMottKnapp) and just do this: /* include("valider-mott.php");*/ if (isset($_POST['registrerMottKnapp'])) { Edited May 14, 2014 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479448 Share on other sites More sharing options...
ingerNorway Posted May 14, 2014 Author Share Posted May 14, 2014 Since your form hasn't been submitted, the POST variables for "registrerMottKnapp" doesn't exist yet. To fix the error, you need to change this: $registrerMottKnapp=$_POST ["registrerMottKnapp"]; To something like this: $registrerMottKnapp = (isset($_POST['registrerMottKnapp'])) ? $_POST['registrerMottKnapp'] : false; Personally, I would eliminate the extra variable ($registrerMottKnapp) and just do this: /* include("valider-mott.php");*/ if (isset($_POST['registrerMottKnapp'])) { Thanks that solve that.. But now, i'm back to the start where nothing happends when I push submit... It just say "har ikke møtt" And thats what I told it to print when submit button is pushed and check box havn't been checked. Soooo, I think the problem might be with the checkbox??? Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479451 Share on other sites More sharing options...
cyberRobot Posted May 14, 2014 Share Posted May 14, 2014 Thanks that solve that.. But now, i'm back to the start where nothing happends when I push submit... It just say "har ikke møtt" And thats what I told it to print when submit button is pushed and check box havn't been checked. Soooo, I think the problem might be with the checkbox??? Have you looked to see what the checkbox value contains? Try using var_dump() after you read in the POST variable for "møtt": $møtt=$_POST['møtt']; var_dump($møtt); You might also need to check the source code (when viewing the page in your browser) for the form to see if the checkbox is being assigned the proper value. Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479453 Share on other sites More sharing options...
ingerNorway Posted May 14, 2014 Author Share Posted May 14, 2014 Have you looked to see what the checkbox value contains? Try using var_dump() after you read in the POST variable for "møtt": $møtt=$_POST['møtt']; var_dump($møtt); You might also need to check the source code (when viewing the page in your browser) for the form to see if the checkbox is being assigned the proper value. Nothing happend when I used var_dump...... This is what my source code says: <input type='hidden' name='m�tt' value='0' /><input type='checkbox' name='m�tt' value='1' />m�tt<br/> (Sorry and newbie at this!) THANKS ALOT ALOT for the help! Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479456 Share on other sites More sharing options...
cyberRobot Posted May 14, 2014 Share Posted May 14, 2014 Nothing happend when I used var_dump...... When the var_dump() code was added, did you check the box and submit the form? Something should have been displayed. <input type='hidden' name='m�tt' value='0' /><input type='checkbox' name='m�tt' value='1' />m�tt<br/> Is there a reason you have two form fields with the same name? Have you tried removing the hidden field to see if that's what's causing the issue? If you're trying to make sure that "møtt" always has a value, you could employ Barand's suggestion. $møtt = isset($_POST['møtt'] ? $_POST['møtt'] : 0; Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479460 Share on other sites More sharing options...
ingerNorway Posted May 15, 2014 Author Share Posted May 15, 2014 When the var_dump() code was added, did you check the box and submit the form? Something should have been displayed. Is there a reason you have two form fields with the same name? Have you tried removing the hidden field to see if that's what's causing the issue? If you're trying to make sure that "møtt" always has a value, you could employ Barand's suggestion. $møtt = isset($_POST['møtt'] ? $_POST['møtt'] : 0; I tried "var_dump($_POST); die();" At the start of my php and this is what i get. array(4) { ["personnummer"]=> string(11) "33333333333" ["timenummer"]=> string(1) "5" ["m�tt"]=> string(1) "1" ["registrerMottKnapp"]=> string(9) "Registrer" } Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479542 Share on other sites More sharing options...
Barand Posted May 15, 2014 Share Posted May 15, 2014 Can you re-post your current code (using a code box so we can see which bits are commented out). It's not easy to read the original. Also, what is the table structure for the table where you are inserting the mott value? Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479543 Share on other sites More sharing options...
ingerNorway Posted May 15, 2014 Author Share Posted May 15, 2014 <?php include("start.html"); error_reporting(E_ALL); ini_set('display_errors', 1); ?> <br/> <form method="post" action="" id="registrerAnsattSkjema" name="registrerAnsattSkjema"> <fieldset> <legend>Registrer møtt / ikke møttt</legend> <label for="personnummer">Personnummer</label> <?php include("listeboks-personnummer.php"); ?><br/> <label for="timenummer">Timenummer</label> <?php include("listeboks-timenummer.php"); ?><br/> Sjekk av denne boksen hvis pasient har møtt til time:<br/> <?php include("checkbox-mott.php"); ?><br/> <input type="submit" name="registrerMottKnapp" id="registrerMottKnapp" value="Registrer"> <input type="reset" name="nullstill" id="nullstill" value="nullstill"> </fieldset> </form><br/> <?php /* include("valider-mott.php");*/ $registrerMottKnapp = (isset($_POST['registrerMottKnapp'])) ? $_POST['registrerMottKnapp'] : false; if (isset($_POST['registrerMottKnapp']) && $mott) { $personnummer=$_POST["personnummer"]; $timenummer=$_POST["timenummer"]; $mott=$_POST["mott"]; print("har mott"); $sqlSetning="UPDATE Kvarterbestilt SET Møtt='$mott' WHERE Personummer='$personnummer' AND Timenummer='$timenummer';"; } else if ($registrerMottKnapp && !$mott) { print("har ikke mott"); $sqlSetning="UPDATE Kvarterbestilt SET Møtt='0' WHERE Personnummer='$personnummer' AND Timenummer='$timenummer';"; } /* $lovligFornavn=validerFornavn ($fornavn); $lovligFornavn2=validerFornavn2 ($fornavn); $lovligFornavn3=validerFornavn3 ($fornavn); $lovligEtternavn=validerEtternavn ($etternavn); $lovligEtternavn2=validerEtternavn2 ($etternavn); $lovligEtternavn3=validerEtternavn3 ($etternavn); $lovligYrke=validerYrke ($yrke); $lovligYrke2=validerYrke2 ($yrke); $lovligYrke3=validerYrke3 ($yrke); if (!$lovligFornavn) { print("Fornavn er ikke fylt ut! <br/>"); } else if (!$lovligFornavn2) { print("Bare bokstaver og mellomrom er tillat; $fornavn<br/>"); } else if (!$lovligFornavn3) { print("Ingen tall tillat; $fornavn<br/>"); } else if (!$lovligEtternavn) { print("Etternavn er ikke fylt ut!<br/>"); } else if (!$lovligEtternavn2) { print("Bare bokstaver og mellomrom er tillat; $etternavn<br/>"); } else if (!$lovligEtternavn3) { print("Ingen tall tillat; $etternavn<br/>"); } else if (!$lovligYrke) { print("Yrke er ikke fylt ut!<br/>"); } else if (!$lovligYrke2) { print("Bare bokstaver og mellomrom er tillat; $yrke<br/>"); } else if (!$lovligYrke3) { print("Ingen tall tillat; $yrke<br/>"); } */ /* include("db-tilkobling.php"); $check=mysqli_query($db,"SELECT * FROM Kvarterbestilt WHERE Personnummer='$personnummer' AND Timenummer='$timenummer'"); $checkrows=mysqli_num_rows($check); if($checkrows>0){echo "Ansatt eksiterer fra før";} else { $sqlSetning="INSERT INTO Kvarterbestilt(Møtt) WHERE Personnummer='$personnummer' VALUES ('$møtt');"; mysqli_query ($db,$sqlSetning) or die ("Ikke mulig Ã¥ registrere i db"); print ("Det er registrert at pasienten har $møtt til timen $timenummer"); } } */ include("slutt.html"); ?> <?php include("db-tilkobling.php"); $sqlResultat=mysqli_query($db,$sqlSetning) or die ("Ikke mulig å hente data fra databasen"); $antallRader=mysqli_num_rows($sqlResultat); /*print("<input type='checkbox' id='mott' name='mott[]' value='$mott'/> Mott");*/ print("<input type='hidden' name='mott' value='0' />"); print("<input type='checkbox' name='mott' value='1' />mott"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479546 Share on other sites More sharing options...
Barand Posted May 15, 2014 Share Posted May 15, 2014 (edited) if (isset($_POST['registrerMottKnapp']) && $mott) As far as I can see, $mott has not yet been defined does not exist. Also in this code section else if ($registrerMottKnapp && !$mott) { print("har ikke mott"); $sqlSetning="UPDATE Kvarterbestilt SET Møtt='0' WHERE Personnummer='$personnummer' AND Timenummer='$timenummer';"; } $personnummer and $timenummer are not defined. edit : put error_reporting(-1); at start of your php code. Edited May 15, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479547 Share on other sites More sharing options...
Solution Barand Posted May 15, 2014 Solution Share Posted May 15, 2014 (edited) try $db = new mysqli(HOST,USERNAME,PASSWORD,'test'); // use your credentials if (isset($_POST['registrerMottKnapp'])) { $personnummer = intval($_POST["personnummer"]); $timenummer = intval($_POST["timenummer"]); $mott = intval($_POST['mott']); echo $mott ? "har mott" : "har ikke mott"; $sqlSetning = "UPDATE Kvarterbestilt SET Møtt=$mott WHERE Personnummer=$personnummer AND Timenummer=$timenummer"; mysqli_query($db, $sqlSetning); } Edited May 15, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/288480-checkbox-10-into-database/#findComment-1479550 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.