所以这是我的HTML
<form method="post">
<div class="form-group">
<label for="Firstname">Firstname</label>
<input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Enter firstname">
</div>
<div class="form-group">
<label for="Lastname">Lastname</label>
<input type="text" class="form-control" id="Lastname" name="lastname" placeholder="Enter lastname">
</div>
<div class="form-group">
<label for="Contact">Contact</label>
<input type="number" class="form-control" id="Contact" name="contact" placeholder="Enter contact number">
</div>
<div class="form-group">
<label for="Email">Email</label>
<input type="email" class="form-control" id="Email" name="email" placeholder="Enter email address">
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<div class="form-group">
<label for="Username">Username</label>
<input type="text" class="form-control" id="Username" name="username" placeholder="Enter email">
</div>
<div class="form-group">
<div class="form-group">
<label for="Password">Password</label>
<input type="password" class="form-control" id="Password" name="password" placeholder="Enter email">
</div>
<div class="form-group">
<div class="form-group">
<label for="CPassword">Confirm Password</label>
<input type="password" class="form-control" id="CPassword" name="cpassword" placeholder="Enter email">
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-12">
<table class="table">
<tbody id="newlinehere">
<tr>
<th>Day</th>
<th>Start</th>
<th>End</th>
<th></th>
</tr>
<tr id="countme">
<td>
<div class="form-group">
<select class="form-control" name="day[]">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
</div>
</td>
<td>
<div class="bootstrap-timepicker"><div class="bootstrap-timepicker-widget dropdown-menu"><table><tbody><tr><td><a href="#" data-action="incrementHour"><i class="glyphicon glyphicon-chevron-up"></i></a></td><td class="separator"> </td><td><a href="#" data-action="incrementMinute"><i class="glyphicon glyphicon-chevron-up"></i></a></td><td class="separator"> </td><td class="meridian-column"><a href="#" data-action="toggleMeridian"><i class="glyphicon glyphicon-chevron-up"></i></a></td></tr><tr><td><span class="bootstrap-timepicker-hour">12</span></td> <td class="separator">:</td><td><span class="bootstrap-timepicker-minute">15</span></td> <td class="separator"> </td><td><span class="bootstrap-timepicker-meridian">AM</span></td></tr><tr><td><a href="#" data-action="decrementHour"><i class="glyphicon glyphicon-chevron-down"></i></a></td><td class="separator"></td><td><a href="#" data-action="decrementMinute"><i class="glyphicon glyphicon-chevron-down"></i></a></td><td class="separator"> </td><td><a href="#" data-action="toggleMeridian"><i class="glyphicon glyphicon-chevron-down"></i></a></td></tr></tbody></table></div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control timepicker" name="start[]">
<div class="input-group-addon">
<i class="fa fa-clock-o"></i>
</div>
</div>
</div>
</div>
</td>
<td>
<div class="bootstrap-timepicker"><div class="bootstrap-timepicker-widget dropdown-menu"><table><tbody><tr><td><a href="#" data-action="incrementHour"><i class="glyphicon glyphicon-chevron-up"></i></a></td><td class="separator"> </td><td><a href="#" data-action="incrementMinute"><i class="glyphicon glyphicon-chevron-up"></i></a></td><td class="separator"> </td><td class="meridian-column"><a href="#" data-action="toggleMeridian"><i class="glyphicon glyphicon-chevron-up"></i></a></td></tr><tr><td><span class="bootstrap-timepicker-hour">12</span></td> <td class="separator">:</td><td><span class="bootstrap-timepicker-minute">15</span></td> <td class="separator"> </td><td><span class="bootstrap-timepicker-meridian">AM</span></td></tr><tr><td><a href="#" data-action="decrementHour"><i class="glyphicon glyphicon-chevron-down"></i></a></td><td class="separator"></td><td><a href="#" data-action="decrementMinute"><i class="glyphicon glyphicon-chevron-down"></i></a></td><td class="separator"> </td><td><a href="#" data-action="toggleMeridian"><i class="glyphicon glyphicon-chevron-down"></i></a></td></tr></tbody></table></div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control timepicker" name="end[]">
<div class="input-group-addon">
<i class="fa fa-clock-o"></i>
</div>
</div>
</div>
</div>
</td>
<td>
<!--<button type="button" class="btn removeline">x</button>-->
</td>
</tr>
</tbody></table>
</div>
<div class="col-lg-12">
<button type="button" class="btn btn-primary" id="newline">New Line</button>
<button type="submit" class="btn btn-success" name="testsave">Save changes</button>
</form>
这是我的PHP代码
<?php
if(isset($_POST["testsave"])){
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$contact=$_POST['contact'];
$email=$_POST['email'];
$username=$_POST['username'];
$password=$_POST['password'];
$cpassword=$_POST['cpassword'];
$day=$_POST['day'];
$start=$_POST['start'];
$end=$_POST['end'];
$number = count($_POST["day"]);
$sql = "SELECT coalesce(MAX(cashier_id), 0) AS maxx FROM tbl_cashiers";
$result = $DBcon->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$cashier_id=$row["maxx"];
}
$cashier_id++;
} else {
$cashier_id=1;
}
if($number > 0){
foreach( $day as $key => $n ) {
$dday=$day[$key];
$sstart=$start[$key];
$eend=$end[$key];
$sql = "INSERT INTO tbl_shifts (cashier_id, shift_day, shift_start,shift_end)
VALUES ('$cashier_id','$dday','$sstart','$eend')";
if ($DBcon->query($sql) === TRUE) {
}
}
/* $sql2 = "INSERT INTO tbl_cashiers (username, password, firstname,lastname, contact, email)
VALUES ('$username','$password','$firstname','$lastname','$contact','$email')";
if ($DBcon->query($sql2) === TRUE) {
$last_id = $DBcon->insert_id;
} */
}
}
?>
注意评论的代码。我想将这些数据保存到两个表中,但这样只保存DAY,START和END值的第一个数组。 我想为什么把它放在foreach循环的末尾会停止循环。请赐教。
PS:评论的代码实际上没有在这里评论,我只是强调它。
答案 0 :(得分:0)
我不明白你的问题,插入查询几天不起作用?
在没有预备语句的情况下小心SQL注入。
你可以使用像这样的帮手:
<?php
class DB
{
protected static $instance = null;
protected function __construct() {}
protected function __clone() {}
public static function instance($context = 'stream')
{
//DB Dispatch
if (!isset(self::$instance[$context]) || self::$instance[$context] === null)
{
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => FALSE,
);
switch ($context)
{
case 'stream':
$dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
self::$instance[$context] = new PDO($dsn, DB_USER, DB_PASS, $opt);
break;
default:
die('DB Context invalid');
break;
}
}
return self::$instance[$context];
}
public static function __callStatic($method, $args)
{
return call_user_func_array(array(self::instance(), $method), $args);
}
public static function query($sql, $args = [], $context = 'stream')
{
$stmt = self::instance($context)->prepare($sql);
$stmt->execute($args);
return $stmt;
}
public static function getLastInsertedId($context)
{
return self::instance($context)->lastInsertId();
}
public static function insert($table, $keyValue, $context)
{
$fieldString = '';
$valueString = '';
$i = 1;
foreach ($keyValue as $key => $currKeyValue)
{
$fieldString .= $key;
$valueString .= ':'.$key;
if($i != count($keyValue))
{
$fieldString .= ', ';
$valueString .= ', ';
}
$i++;
}
$query = 'INSERT INTO `'.$table.'` ('.$fieldString.') VALUES('.$valueString.')';
DB::query($query, $keyValue, $context);
}
public static function update($table, $keyValue, $idtoUpdate, $context = 'stream', $fieldIdToUpdate = 'id')
{
$updateString = '';
$i = 1;
foreach ($keyValue as $key => $currKeyValue)
{
$updateString .= $key.'=:'.$key;
if($i != count($keyValue))
$updateString .= ', ';
$i++;
}
$query = 'UPDATE `'.$table.'` SET '.$updateString.' WHERE '.$fieldIdToUpdate.'=:id';
$keyValue['id'] = $idtoUpdate;
DB::query($query, $keyValue, $context);
}
public static function select($table, $where = false, $whereParams = array(), $selectFields = '*', $context = 'stream', $fetchAll = false)
{
$selectQuery = 'SELECT '.$selectFields.' FROM '.$table;
if($where)
$selectQuery .= ' WHERE '.$where;
$return = DB::query($selectQuery, $whereParams, $context);
if(!$fetchAll)
return $return;
else
return $return->fetchAll();
}
/**
*@param string $table : Table used for the search
*@param string $where : String containing the "WHERE" part of the query
*@param array $whereParams : Parameters to bind
*@param string $context : DB context (for multiple DB)
*/
public static function checkIfExist($table, $where = false, $whereParams = array(), $context = 'stream')
{
if(empty(DB::select($table, $where, $whereParams, '*', $context, true)))
return false;
else
return true;
}
public static function getLatestId()
{
return self::instance()->lastInsertId();
}
}
?>