Jump to content

Checkbox-1/0-into database


ingerNorway
Go to solution Solved by Barand,

Recommended Posts

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");
?>
 
  
 

 

Link to comment
Share on other sites

$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 by boompa
Link to comment
Share on other sites

$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"

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

 

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.

 

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...

Link to comment
Share on other sites

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 by cyberRobot
Link to comment
Share on other sites

 

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???

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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;
Link to comment
Share on other sites

 

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" }

Link to comment
Share on other sites


<?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");
?>
Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

  • Solution

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 by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.