gpt4 book ai didi

javascript - 从动态表单插入多个值

转载 作者:行者123 更新时间:2023-11-29 19:33:07 24 4
gpt4 key购买 nike

我有一个包含 2 行名为 start_time 和 end_time 的数据库,想要使用 implode() 从动态表单插入值,但似乎无法从后续输入字段插入值。

enter image description here

数据库

enter image description here

注册.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>

注册.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();
}
}

本地.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>');
});

最佳答案

如果您内爆开始时间字段,您会得到一个所有开始时间连接在一起的字符串 - 这似乎不是您想要的。

相反,您应该迭代开始和结束时间的数组并单独处理每个条目

for ($i = 0; i < count($_POST['starttime']); $i++) {
$starttime = $_POST['starttime'];
$endtime = $_POST['endtime'];

// do something usefull and register user
}

关于javascript - 从动态表单插入多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com