我有一个名为start_time和end_time的2行数据库,希望通过使用implode()从我的动态表单中插入值,但似乎无法插入后续输入字段中的值。
数据库
登录up.php
<div class="col-xs-4">
<input type="text" class="form-control" name="starttime[]" placeholder="12:00">
</div>
<div class="col-xs-2">
<div class="col-md-2"><h5>TO</h5></div>
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="endtime[]" placeholder="12:00">
</div>
登录up.php
$startTime = implode(",", $_POST['starttime']);
$endTime = implode(",", $_POST['endtime']);
if($uname=="") {
$error[] = "provide username !";
}
else if($upass=="") {
$error[] = "provide password !";
}
else if(strlen($upass) < 6){
$error[] = "Password must be atleast 6 characters";
}
else
{
try
{
$stmt = $user->runQuery("SELECT last_name FROM users WHERE last_name=:uname");
$stmt->execute(array(':uname'=>$uname));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($row['last_name']==$uname) {
$error[] = "Username already taken !";
}
else
{
if($user->register($startTime,$endTime)){
$user->redirect('sign-up.php?joined');
}
}
}
class.user.php
public function register($fName,$mName,$uname,$upass,$grdLvl,$subjects,$startTime,$endTime)
{
try
{
$new_password = password_hash($upass, PASSWORD_DEFAULT);
$stmt = $this->conn->prepare("INSERT INTO users(first_name,middle_name,last_name,user_pass,grade_lvl,subjects,start_time,end_time)
VALUES(:fName,:mName,:uname,:upass,:grdLvl,:subjects,:startTime,:endTime)");
$stmt->bindparam(":fName", $fName);
$stmt->bindparam(":mName", $mName);
$stmt->bindparam(":uname", $uname);
$stmt->bindparam(":upass", $new_password);
$stmt->bindparam(":grdLvl", $grdLvl);
$stmt->bindparam(":subjects", $subjects);
$stmt->bindparam(":startTime", $startTime);
$stmt->bindparam(":endTime", $endTime);
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
local.js
$("#add-entry").click(function(e){
console.log("called");
event.preventDefault();
$("#items").append('<div class="row">'
+'<div class="col-xs-4">'
+'<input type="text" class="form-control" name="starttime[]" placeholder="12:00">'
+'</div><div class="col-xs-2"><div class="col-md-2"><h5>TO</h5></div></div>'
+'<div class="col-xs-4">'
+'<input type="text" class="form-control" name="endtime[]" placeholder="12:00"></div>'
+'<div class="col-xs-1"><input type="button" value="delete" id="delete-entry"></div></div>');
});
答案 0 :(得分:0)
如果你破坏你的开始时间字段,你会得到一个字符串,其中包含所有开始时间连接 - 这看起来并不像你想要的那样。
相反,您应该遍历开始和结束时间的数组并自行处理每个条目
for ($i = 0; i < count($_POST['starttime']); $i++) {
$starttime = $_POST['starttime'];
$endtime = $_POST['endtime'];
// do something usefull and register user
}