php - 如何使用php将一列的单行中的多个值添加到mysql中

标签 php html mysql twitter-bootstrap

我是人力资源部,是开发部的新人。我开始使用 PHP 和 Mysql 编写人力资源管理程序。我想在一列的一行中添加至少 5 名员工的姓名。我使用 bootstrap 在单个字段中获取多个值。但是当我尝试插入值时,只插入了最后一个值。

<td>Distribute</td><td><input type="text" class="form-control"  name="distribute[]"></td>

HTML代码:

<tr><td>Distribute</td><td><input type="text" class="form-control"  name="distribute[]"></td></tr>

PHP 插入 mysql

$civil_id= $_POST['civil_id'];

    $name= $_POST['name']; 
    $card_type = $_POST['card_type']; 
    $count = $_POST['count']; 
    $amt=$card_type * $count;
    $avail_bal=$_POST['balance']- $amt; 
    $issue_year= $_POST['issue_year']; 
    $issue_month= $_POST['issue_month']; 
    $issue_date= $_POST['issue_date']; 
    $distribute= $_POST['distribute'];

$sql ="insert into group_phone
(civil_id,
name,
card_type,
count,
amt,
avail_bal,issue_year,issue_month,issue_date,distribute)values('$civil_id',
'$name',
'$card_type',
'$count',
'$amt',
'$avail_bal','$issue_year','$issue_month','$issue_date','$distribute')";

HTML

<script>
      $(document).ready(function() {
        $(".select2_single").select2({
          placeholder: "Select Vehicle Plate Number",
          allowClear: true
        });
        $(".select2_group").select2({});
        $(".select2_multiple").select2({
          maximumSelectionLength: 10,
          placeholder: "With Max Selection limit 10",
          allowClear: true
        });
      });
    </script>
<form class="form-horizontal form-label-left" action="new_rechargecard.php" method="POST" ">
                    <table id="datatable" class="table table-striped table-bordered">
                      


                      <tbody>
					  
					   <tr>
	          <td width='100'>Employee Name: </td><td><input type="text" class="form-control" placeholder="0" name="name" value="<?php echo "$name";?>" ></td> 
    
</tr>
                       
                      
					   
					   
                          
    <tr>
	
	<td>Available Balance</td><td><input type="text" class="form-control" placeholder="0" name="balance" value=<?php echo $balance;?> > </td></tr> 
     <tr><td>Civil id</td><td><input type="text" class="form-control" placeholder="Civil ID Required" name="civil_id" value=<?php echo $pass_name;?> > </td></tr>
	 <!--<tr><td>Name</td><td><input type="text" class="form-control" name="name" value="<?php echo $name;?>" > </td></tr>-->
	 
	  
	 <tr><td width='250'>Card Type (1 KD or 2.5 KD or 5 KD)</td><td><input type="text" class="form-control"   name="card_type"></td></tr>
	 <tr><td width='200'>Card Count</td><td><input type="text" class="form-control"  name="count"></td></tr>
	 <tr><td>Issue Year</td><td><input type="text" class="form-control"  name="issue_year" value="<?php echo date('Y'); ?>"></td></tr>
	  <tr><td>For the Month of</td><td><input type="text" class="form-control"  name="issue_month" value="<?php echo date('M'); ?>"></td></tr>
	 <tr><td>Issue date</td><td><input type="text" class="form-control"  name="issue_date"></td></tr>
	 <tr><td>Distribute</td><td><select name="distribute" class="select2_multiple form-control" tabindex="-1"  multiple="multiple">
	 <option></option>
     <option>Richard Marcus</option>     
     <option>Rowlant S Peter</option> 
     <option>David.K.Rumpell</option> 
     <option>John Mathew</option>  
   	 </select>
	 </td></tr>
	 
	
	 	
     <tr><td></td><td><input type="submit" class="btn btn-round btn-danger" value="Update" name="submit"></td></tr>
	 </tbody>
     </table>
	 </form>

最佳答案

像这样更改你的代码

<?php

/* First create mysql connection */

$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "userlist";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

/* End */

    $civil_id= $_POST['civil_id'];

    $name= $_POST['name']; 
    $card_type = $_POST['card_type']; 
    $count = $_POST['count']; 
    $amt=$card_type * $count;
    $avail_bal=$_POST['balance']- $amt; 
    $issue_year= $_POST['issue_year']; 
    $issue_month= $_POST['issue_month']; 
    $issue_date= $_POST['issue_date']; 
    $distribute= $_POST['distribute'];

$sql ="insert into group_phone
(civil_id,
name,
card_type,
count,
amt,
avail_bal,issue_year,issue_month,issue_date,distribute)values('".$civil_id."',
'".$name."',
'".$card_type."',
'".$count."',
'".$amt."',
'".$avail_bal."','".$issue_year."','".$issue_month."','".$issue_date."','".$distribute."')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

?>

MySQL table structure

-- 表userlist

的表结构
CREATE TABLE IF NOT EXISTS `userlist` (
  `civil_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `card_type` varchar(100) NOT NULL,
  `count` int(11) NOT NULL,
  `amt` int(11) NOT NULL,
  `avail_bal` int(11) NOT NULL,
  `issue_year` int(11) NOT NULL,
  `issue_month` varchar(50) NOT NULL,
  `issue_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `distribute` varchar(200) NOT NULL,
  PRIMARY KEY (`civil_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Working Query (MySQL)

INSERT INTO `userlist` (`civil_id`, `name`, `card_type`, `count`, `amt`, `avail_bal`, `issue_year`, `issue_month`, `issue_date`, `distribute`) VALUES
(1, 'aman,suresh,mohan', 'POST', 10, 500, 50, 2016, 'March', '2017-02-01 06:46:16', 'Airtel,Idea,Vodafone');

关于php - 如何使用php将一列的单行中的多个值添加到mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972623/

相关文章:

mysql - 找到多列索引的最佳顺序

c# - 在 C# 中用 Regex 替换两个或多个空格

html - 设置express以启用静态css和js文件的使用

php - LEFT JOIN SQL 语句对性能的影响

php - 地理编码 - 获取位置

javascript - 删除用于复制 + 粘贴的富文本格式? (跨浏览器)

mysql - 跨数据库连接花费的时间比预期要长

mysql - 按计数和另一个标准抓取行 MySQL

php - 如何从数据库请求中获取多行

php - synology php ftp_ssl_connect - 调用未定义的函数