简体   繁体   中英

MySQL Multiple Values in Field

I have a MySQL table set up like this:

-applications (table) --positionsApplied (column)

On the front-end application you may check off multiple positions that will then be sent to the database.

How do I handle multiple values for one field?

Here is my PHP:

    <?php
include 'vbs.php';

if (!$connect) {
    die('Could not connect: ' . mysql_error());
} else {
    echo 'Connected.';
}

$date = date('m-d-Y');
$time = getTime();

//personal
$name = explode(" ", $_POST['name']);
$address = $_POST['address'];
$phone = $_POST['phone'];
$email = $_POST['email'];

//academic
$major = $_POST['major'];
$gradDate = $_POST['gradDate'];
$relevantWork = $_POST['relevantWork'];

//tell us more
$previousExp = $_POST['previousExp'];
$creativeStrengths = $_POST['creativeStrengths'];
$positions = $_POST['position'];
$interests = $_POST['interest'];
$skills = $_POST['skills'];
$workStudy = $_POST['work-study'];

$stmt = $connect->prepare("INSERT INTO `applications` (dateSubmitted, timeSubmitted, firstName,lastName,address,phone,email,major,gradDate,work,
previousExperience,creativeStrengths,interests,skills,workStudy) VALUES ('".$date."', '".$time."', '".$name[0]."', '".$name[1]."','".$address."',
'".$phone."', '".$email."', '".$major."', '".$gradDate."', '".$relevantWork."', '".$previousExp."', '".$creativeStrengths."', '".$positions."',
'".$interests."', '".$skills."', '".$workStudy."')");

if (!$stmt) {
     echo "Prepare failed: (" . $connect->errno . ") " . $connect->error;
}

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

function getTime() {
    date_default_timezone_set('America/New_York');
    $currenttime = date('h:i:s:u');
    list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
    return "$hrs:$mins";
}

?>

And my HTML check boxes for job positions:

<input name="position" value="Project Manager" type="checkbox" /> Project Manager<br><input name="position" value="Content Developer" type="checkbox" /> Content Developer<br><input name="position" value="Graphic Designer" type="checkbox" /> Graphic Designer<br>
                                                    <input name="position" value="Digital Media Creator & Storyteller" type="checkbox" /> Digital Media Creator & Storyteller<br><input name="position" value="Programmer" type="checkbox" /> Programmer<br><input name="position" value="Server Engineer/Technician" type="checkbox" /> Server Engineer/Technician<br>
                                                <input name="position" value="User Experience Designer" type="checkbox" /> User Experience Designer<br>

You could use <input name="position[]" multiple> then fill the input with <option> retrieved from your select statement. You could then access the multiple input as an array in your PHP. Make sure the name of your input have the [] at then end else it won't be considered as an array by PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM