我试图使用PHP从select下拉列表中插入多个值,但它';s仅取最后选定的值
我尝试过使用内爆和foreach函数,但它仍然使用最后选择的值 这是php插入代码我试图使用PHP从select下拉列表中插入多个值,但它';s仅取最后选定的值,php,Php,我尝试过使用内爆和foreach函数,但它仍然使用最后选择的值 这是php插入代码 <?php if (isset($_POST['register'])) { $research = $_POST['research']; $reg = $_POST['RegNo']; $topic = $_POST['topic']; $year = $_POST['year']; $staff = $_POST['staff']; foreach
<?php
if (isset($_POST['register'])) {
$research = $_POST['research'];
$reg = $_POST['RegNo'];
$topic = $_POST['topic'];
$year = $_POST['year'];
$staff = $_POST['staff'];
foreach ($staff as $choice) {
require 'connectdb.php';
$statement = $conn->prepare('INSERT INTO research ( Name, Topic, Year, RegNo, staffID) VALUES (?,?,?,?,?);');
$statement->bind_param('sssss', $researchh, $topicc, $yearr, $regg, $stafff);
$researchh = $research;
$topicc = $topic;
$yearr = $year;
$regg = $reg;
$stafff = $choice;
}
if ($statement->execute() == false) {
if ($conn->errno === 1062) {
header('Location:?exists');
}
} else {
header('Location:?newstudentTopic');
}
}
?>
<?php
if (isset($_POST['register'])) {
require 'connectdb.php';
$research = $_POST['research'];
$reg = $_POST['RegNo'];
$topic = $_POST['topic'];
$year = $_POST['year'];
$staff = $_POST['staff'];
$isSuccess = 1;
foreach ($staff as $choice) {
$statement = $conn->prepare('INSERT INTO research ( Name, Topic, Year, RegNo, staffID) VALUES (?,?,?,?,?);');
$statement->bind_param('sssss', $researchh, $topicc, $yearr, $regg, $stafff);
$researchh = $research;
$topicc = $topic;
$yearr = $year;
$regg = $reg;
$stafff = $choice;
if ($statement->execute() == false) {
$isSuccess = 0;
}
}
if (!$isSuccess) {
header('Location:?exists');
}
else {
header('Location:?newstudentTopic');
}
}
?>
这是表单字段:
<div class="col-md-6">
<label for="gender">Supervior</label>
<select class="form-control selectpicker border border-info" id="staff" required name="staff[]"
data-live-search="true" multiple data-max-options="2" multiple="multiple">
<option value=""> Allocate Supervisor</option>
<?php
require 'connectdb.php';
$statement = $conn->prepare('SELECT staffid,fname,lname from staff');
$statement->execute();
$statement->bind_result($staffid, $fname, $lname);
$statement->store_result();
while ($statement->fetch()) {
?>
<option value="<?php echo $staffid ?>"><?php echo $fname . ' ' . $lname ?></option>
<?php
}
$statement->close();
$conn->close();
?>
</select>
</div>
监管者维度
分配主管
将您的$statement->execute()
放在您的foreach
循环中,您就可以开始了:)
你可以这样做:
// needs to be included only once
require 'connectdb.php';
// you can use this multiple times
$statement = $conn->prepare('INSERT INTO research ( Name, Topic, Year, RegNo, staffID) VALUES (?,?,?,?,?);');
$success = true;
foreach ($staff as $choice) {
$researchh = $research;
$topicc = $topic;
$yearr = $year;
$regg = $reg;
$stafff = $choice;
$statement->bind_param('sssss', $researchh, $topicc, $yearr, $regg, $stafff);
$success = $statement->execute() && $success;
// Do this if you want to stop inserting after one fails
// if($success === false) {
// break;
// }
}
if ($success) {
header('Location:?exists');
} else {
header('Location:?newstudentTopic');
}
exit; // thx @halfer :)
如果只想在没有一个执行失败的情况下插入,请查看事务:execute命令在循环之外,也不要在循环中包含“connectdb.php”或使用require_一次。您应该
准备sql一次,然后执行多次~这是准备语句风格之美的一部分
您应该测试所需字段在POST数组中是否可用-下面我创建了一个要测试的字段数组~第一个测试确定是否有任何所需字段为空,第二个测试确定是否有人试图将其他参数注入POST数组
您应该将require
数据库连接包含一次,而不是包含在循环中
最后,只需循环遍历staff
数组,并为每个迭代执行查询
<?php
try{
$results=[];
$fields=['research','RegNo','topic','year','staff','register'];
foreach( $fields as $field ) {
if( empty( $_POST[ $field ] ) ) {
throw new Exception( sprintf( 'Missing field "%s"', $field ) );
}
}
foreach( $_POST as $field ){
if( !in_array( $field, $fields ) ) {
throw new Exception( sprintf('Unknown field "%s"', $field ) );
}
}
/* include the db connection once, NOT in the loop */
require 'connectdb.php';
$research=$_POST['research'];
$reg=$_POST['RegNo'];
$topic=$_POST['topic'];
$year=$_POST['year'];
/* this is a multiple field - array */
$arr_staff=$_POST['staff'];
/* Create the sql statement once - NOT in the loop */
$sql='INSERT INTO research ( `Name`, `Topic`, `Year`, `RegNo`, `staffID`) VALUES (?,?,?,?,?);'
$stmt=$conn->prepare( $sql );
if( !$stmt )throw new Exception('Failed to prepare SQL');
$stmt->bind_param('sssss', $research, $topic, $year, $reg, $staff );
foreach( $arr_staff as $staff ){
$results[]=$stmt->execute();
}
exit( header( 'Location: ?newstudentTopic' ) );
} catch( Exception $e ){
exit( $e->getMessage() );
}
?>
您的代码中存在多个小故障。我已经对它进行了一些优化,并修复了与您的需求相关的问题。请让我知道它是否有效
我尝试过使用内爆和foreach函数,但它仍然使用最后选择的值
这是php插入代码
<?php
if (isset($_POST['register'])) {
$research = $_POST['research'];
$reg = $_POST['RegNo'];
$topic = $_POST['topic'];
$year = $_POST['year'];
$staff = $_POST['staff'];
foreach ($staff as $choice) {
require 'connectdb.php';
$statement = $conn->prepare('INSERT INTO research ( Name, Topic, Year, RegNo, staffID) VALUES (?,?,?,?,?);');
$statement->bind_param('sssss', $researchh, $topicc, $yearr, $regg, $stafff);
$researchh = $research;
$topicc = $topic;
$yearr = $year;
$regg = $reg;
$stafff = $choice;
}
if ($statement->execute() == false) {
if ($conn->errno === 1062) {
header('Location:?exists');
}
} else {
header('Location:?newstudentTopic');
}
}
?>
<?php
if (isset($_POST['register'])) {
require 'connectdb.php';
$research = $_POST['research'];
$reg = $_POST['RegNo'];
$topic = $_POST['topic'];
$year = $_POST['year'];
$staff = $_POST['staff'];
$isSuccess = 1;
foreach ($staff as $choice) {
$statement = $conn->prepare('INSERT INTO research ( Name, Topic, Year, RegNo, staffID) VALUES (?,?,?,?,?);');
$statement->bind_param('sssss', $researchh, $topicc, $yearr, $regg, $stafff);
$researchh = $research;
$topicc = $topic;
$yearr = $year;
$regg = $reg;
$stafff = $choice;
if ($statement->execute() == false) {
$isSuccess = 0;
}
}
if (!$isSuccess) {
header('Location:?exists');
}
else {
header('Location:?newstudentTopic');
}
}
?>
此外,header()
Location语句后面应该紧跟着一个exit,这样脚本就不会试图在之后继续执行。