Jump to content

PHP array checkbox help xD storing in database


rmariano02

Recommended Posts

when i try to run this code no error comes out but the values is not storing in the database please help T_T

 

Interface(client.php)

<form action="reserve.php" method="post">

Date: <select name='month'>

 

<?php

$montharray = array ("01","02","03","04",

"05","06","07","08","09"

,"10","11","12");

foreach($montharray as $month){

echo "<option value=$month>$month</option>";

}

?>

</select>

 

<select name='day'>

<?php

for($i=1; $i<=31; $i++){

echo "<option value=$i>$i</option>";

}

?>

</select>

 

<select name='year'>

<?php

for($i=date(Y); $i>=1935; $i--){

echo "<option value=$i>$i</option>";

}

?>

</select>

<br/>

Time: <select name="time">

<option value="10:00"> 10:00 am</option>

<option value="11:00"> 11:00 am</option>s

<option value="12:00"> 12:00 pm</option>

<option value="1:00"> 1:00 pm</option>

<option value="2:00"> 2:00 pm</option>

<option value="3:00"> 3:00 pm</option>

<option value="4:00"> 4:00 pm</option>

<option value="5:00"> 5:00 pm</option>

<option value="6:00"> 6:00 pm</option>

<option value="7:00"> 7:00 pm</option>

<option value="8:00"> 8:00 pm</option>

 

</select>

<br/>

Service:<br/>

<input name="0" value="check" type="checkbox"> Facial</input><br/>

<input name="1" value="check" type="checkbox"> Diamond Microderma</input><br/>

<input name="2" value="check" type="checkbox"> Peeling Procedure</input><br/>

<input name="3" value="check" type="checkbox"> Pimple Treatment</input><br/>

<input name="4" value="check" type="checkbox"> Pimple Injection</input><br/>

<input name="5" value="check" type="checkbox"> Acne Surgery</input><br/>

<input name="6" value="check" type="checkbox"> Cautery</input><br/>

<input name="7" value="check" type="checkbox"> Acne-prone skin microneeding</input><br/>

<input name="8" value="check" type="checkbox"> Advanced stem cell microneeding therapy</input><br/>

<input name="9" value="check" type="checkbox"> Needle-less stem cell infusion</input><br/>

<input name="10" value="check" type="checkbox"> Needle-less mesolift</input><br/>

<input name="11" value="check" type="checkbox"> Botox</input><br/>

<input name="12" value="check" type="checkbox"> Bio hair growth stem cell infusion</input><br/>

<input name="13" value="check" type="checkbox"> RF Figure countour</input><br/>

<input name="14" value="check" type="checkbox"> Slimming Massage</input><br/>

<input name="15" value="check" type="checkbox"> PT Metabolic Accelerator</input><br/>

<input name="16" value="check" type="checkbox"> Mesotherapy</input><br/>

<input name="17" value="check" type="checkbox"> Ultrasonic Liposuction</input><br/>

<input name="18" value="check" type="checkbox"> Filler</input><br/>

<input name="19" value="check" type="checkbox"> Hair Removal</input><br/>

<input name="20" value="check" type="checkbox"> Photorejuvenation</input><br/>

<input name="21" value="check" type="checkbox"> Hyperpigmentation</input><br/>

<input name="22" value="check" type="checkbox"> Acnecontrol</input><br/><br/>

<input name="reserve" type="submit" onclick="show_alert()" value="Make Resevation">

</form>

 

Validation(reserve.php)

<?php

 

include('chkconnection.php');

session_start();

$user = $_SESSION['loggedInUser'];

 

for($x=0;$x<=23;$x++){

$chk[$x] = 'uncheck';

}

 

if(isset($_POST["reserve"])){

for($x=0;$x<=23;$x++){

if(isset($_POST["$x"])){

$chk[$x] = $_POST["$x"];

}

}

 

$date = $_POST["year"].$_POST["month"].$_POST["day"];

$time = $_POST["time"];

 

mysql_query("INSERT INTO ff_tblreserve (Facial,

Diamond_Microderma,

PeelingsProcedure,

Pimple_Treatment,

Pimple_Injection,

Acne_Surgery,

Acne_prone_skin_microneeding,

Advanced_stem_cell_microneeding_therapy,

Needle_less_stem_cell_infusion,

Needle_less_mesolift,

Botox,

Bio_hair_growth_stem_cell_infusion,

RF_Figure_countour,

Slimming_Massage,

PT_Metabolic_Accelerator,

Mesotherapy,

Ultrasonic_Liposuction,

Filler,

Hair_Removal,

Photorejuvenation,

Hyperpigmentation,

Acnecontrol,

time,

date,

id)

VALUES

('$chk[1]','$chk[2]','$chk[3]','$chk[4]','$chk[5]','$chk[6]','$chk[7]','$chk[8]','$chk[9]','$chk[10]','$chk[11]',

'$chk[12]','$chk[13]','$'chk[14]','$chk[15]','$chk[16]','$chk[17]','$chk[18]','$chk[19]','$chk[20]','$chk[21]',

'$chk[22]','$chk[23]','$time','$date',$user)");}

 

?>

 

Link to comment
Share on other sites

Your query is probably failing and you don't see it because you have no error handling. The reason it is probably failing is that checkboxes that aren't checked do not pass a value. So, you have some values in the query that are missing. Don't give your fields names such as 1, 2, 3. Give them names that correspond to their field names. That makes it much easier to debug/fix code.

 

Also, don't store the value "check". That is a string value that you would have to constantly compare against a string. Use 1 (true) and 0 (false). Those values are logically handled as Boolean values.

 

Here are some changes to make:

 

1. Change the checkboxes to something like this

<input name="service[]" value="Facial" type="checkbox"> Facial</input>
<input name="service[]" value="Diamond_Microderma" type="checkbox"> Diamond Microderma</input><br/>

I forget if you should have quotes around the indexes in the HTML field names, but I don't think so.

 

2. In your processing of the input, be sure to generate a COMPLETE list of values for all services so you have an appropriate value for the query

//Create an array of ALL services
$services = array('Facial', 'Diamond_Microderma', 'PeelingsProcedure', ....);
//use above array to create new array using services as key and default value of 0
$svc = array_fill_keys($services, 0);
//Loop through each POST service and updates the values as needed
foreach($_POST['services'] as $checkedService)
{
    $svc[$checkedService] = 1;
}

 

Lastly change your query to use the new values and add some error handling

$query = "INSERT INTO ff_tblreserve
              (Facial, Diamond_Microderma, PeelingsProcedure, Pimple_Treatment, Pimple_Injection, Acne_Surgery,
               Acne_prone_skin_microneeding, Advanced_stem_cell_microneeding_therapy, Needle_less_stem_cell_infusion,
               Needle_less_mesolift, Botox, Bio_hair_growth_stem_cell_infusion, RF_Figure_countour, Slimming_Massage,
               PT_Metabolic_Accelerator, Mesotherapy, Ultrasonic_Liposuction, Filler, Hair_Removal, Photorejuvenation,
               Hyperpigmentation, Acnecontrol, time, date, id)
          VALUES
              ('$svc['Facial']','$svc['Diamond_Microderma']','$svc['PeelingsProcedure']', . . .";

$result = mysql_query($query) or die ("Query:<br>\n{$query}\n<br>Error:<br>\n" . mysql_error());

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.