Jump to content

What are the additional codes to write to capture multiple values?


genzedu777

Recommended Posts

Hi guys,

 

I have an intention to allow user to select multiple district, however even though when they select more than 2 options in the district, it turns out my database is only able to capture the last value.

 

For instance, I chose ID=1 ID=2, however the return value to my database will be only ID=2.

 

Is there something which I have missed out? Your advice is greatly appreciated. Thank you

 

<?php

$district = $_POST['district'];

 

if (isset($_POST['submit'])) { 

$dbc = mysqli_connect('localhost', '***', '***', '***')

    or die(mysqli_error());

 

$query2 = "INSERT INTO tutor_preferred_dlocation (district_id) VALUES ('$district')";

 

$result2 = mysqli_query($dbc, $query2)

or die(mysqli_error());

}

 

 

<input name="district" type="checkbox" id="1" value="1">

<label for="yishun">Yishun</label>

 

<input name="district" type="checkbox" id="2" value="2">

<label for="angMoKio">Ang Mo Kio</label>

 

<input name="district" type="checkbox" id="3" value="3">

<label for="yioChuKang">Yio Chu Kang</label>

 

?>

This assumes you want to create a table row for each selected value.

 

<?php
if (isset($_POST['submit'])) {
    $dbc = mysqli_connect('localhost', '***', '***', '***') or die(mysqli_error());
    unset($_POST['submit']);
    $num_fields = count($_POST);
    $i = 0;
    while($i <= $num_fields) {
        $i++;
        if(!empty($_POST['district_'.$i])) {
            $sql = 
            "INSERT INTO tutor_preferred_dlocation (district_id) VALUES ('".$_POST['district_'.$i]."')";
            if(!$query = mysql_query($sql)) {
                echo "Error on line ".__LINE__.". ".mysql_error();
                exit;
            }
            echo "ID <b>".$_POST['district_'.$i]."</b> successfully inserted. <br />\n";
        }
    }
} else {
?>
<input name="district_1" type="checkbox" id="yishun" value="1">
<label for="yishun">Yishun</label>
<input name="district_2" type="checkbox" id="angMoKio" value="2">
<label for="angMoKio">Ang Mo Kio</label>
<input name="district_3" type="checkbox" id="yioChuKang" value="3">
<label for="yioChuKang">Yio Chu Kang</label>
<?php
}
?>

Untested.

I prefer arrays:

 

if (isset($_POST['submit'])) { 
   $districts = $_POST['district'];
   $dbc = mysqli_connect('localhost', '***', '***', '***') or die(mysqli_error());

   foreach($districts as $district) {
      $query2 = "INSERT INTO tutor_preferred_dlocation (district_id) VALUES ('$district')";
      $result2 = mysqli_query($dbc, $query2) or die(mysqli_error());
   }
}

 

 

<input name="district[]" type="checkbox" id="1" value="1">
<label for="yishun">Yishun</label>
    
<input name="district[]" type="checkbox" id="2" value="2">
<label for="angMoKio">Ang Mo Kio</label>
    
<input name="district[]" type="checkbox" id="3" value="3">
<label for="yioChuKang">Yio Chu Kang</label>

 

Hi AbraCadaver,

 

I received an error, not too sure if I have done it correctly.

 

It gave out an errror for the line which is highlighted in yellow.

Warning: Invalid argument supplied for foreach() in D:\inetpub\vhosts\abc.com\httpdocs\report.php on line 128

 

<?php

$name = $_POST['name'];

$email = $_POST['email'];

    $password = $_POST['password'];

$zone = $_POST['zone'];

$districts = $_POST['district'];

$dob = $_POST['dob'];

$category = $_POST['category'];

$comments = $_POST['comment'];

 

if (isset($_POST['submit'])) { 

  $dbc = mysqli_connect('localhost', '***', '***', '***')

    or die(mysqli_error());

 

$query = "INSERT INTO practice_user (name, email, password, dob, category, comments) VALUES ('$name', '$email', '$password', '$dob', '$category', '$comments')";

 

$results1 = mysqli_query($dbc, $query)

or die(mysqli_error());

 

foreach($districts as $district) {

$query2 = "INSERT INTO tutor_preferred_dlocation (district_id) VALUES ('$district')";

$results2 = mysqli_query($dbc, $query2)

or die(mysqli_error());

}

echo 'Thanks for submitting the form.<br />';

 

  mysqli_close($dbc);

 

  }

 

 

?>

Archived

This topic is now archived and is closed to further replies.

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