gpt4 book ai didi

php - 动态输入表单和带数组的 php 代码

转载 作者:行者123 更新时间:2023-11-29 21:34:49 25 4
gpt4 key购买 nike

我有一个带有动态输入的表单。我应该如何将输入中的数组添加到 php,然后添加到 sql?

<?
if(isset($_POST['add'])){
$name = sf($_POST['name']); //first action start
$sec_name = sf($_POST['sec_name']);
mysql_query("INSERT INTO tb_job (name, sec_name) VALUES ('$name', '$sec_name')");
$lid = mysql_insert_id();// then goes second action and here is missing code for array.
mysql_query("INSERT INTO tb_job_skills (id_job, pre_job, date_start, date_end) VALUES ('$lid', '$job', '$date_start', '$date_end')");
}
?>



<div class="form-field">
<label for="name">First Name <span class="require"> *</span></label>
<input class="form-control" name="name" required="" type="text">
</div>
<div class="form-field">
<label for="sec_name">Second Name <span class="require"> *</span></label>
<input class="form-control" name="sec_name" required="" type="text">
</div>
<div class="form-field">
<label for="pre_job">Previous Job <span class="require"> *</span></label>
<input class="form-control" name="pre_job[]" required="" type="text">
</div>
<div class="form-field">
<label for="date_start">First Name <span class="require"> *</span></label>
<input class="form-control" name="date_start[]" required="" type="text">
</div>
<div class="form-field">
<label for="date_end">First Name <span class="require"> *</span></label>
<input class="form-control" name="date_end[]" required="" type="text">
</div>

pre_job[],date_start[],date_end[] - 动态添加输入(因为用户可以有一些以前的工作)

那么,问题是如何从这些输入中获取数据并将其添加到第二个操作中的 tb_job_skills 中?

最佳答案

您可以循环运行表单中的每个数据。您还必须注意 mysql 已经是 deprecated ,所以建议至少使用mysqli prepared statement :

if(isset($_POST["add"])){

/* START OF INSERTING NAME AND SEC_NAME */
$stmt = $con->prepare("INSERT INTO tb_job (name, sec_name) VALUES (?, ?)"); /* PREPARE YOUR QUERY */
$stmt->bind_param("ss", $_POST["name"], $_POST["sec_name"]); /* BIND THIS DATA TO YOUR QUERY */
$stmt->execute(); /* EXECUTE THE QUERY */
$lid = $stmt->insert_id; /* GET THE ID OF THE INSERTED NAME */
$stmt->close(); /* CLOSE THE PREPARED QUERY */

/* START OF LOOP; CHECK EACH JOB INPUT OF USER */
for($x = 0; $x < count($_POST["pre_job"]); $x++){

if(!empty($_POST["pre_job"][$x])){ /* CHECK IF PRE_JOB[N] IS NOT EMPTY */
/* START INSERTING THE DYNAMIC INPUT JOB(S) */
$stmt = $con->prepare("INSERT INTO tb_job_skills (id_job, pre_job, date_start, date_end) VALUES (?, ?, ?, ?)");
$stmt->bind_param("isss", $lid, $_POST["pre_job"][$x], $_POST["date_start"][$x], $_POST["date_end"][$x]);
$stmt->execute();
$stmt->close();
} /* END OF IF; CHECKING IF THE INPUT IS NOT EMPTY */

} /* END OF FOR LOOP */

} /* END OF ISSET */

另请注意,您必须在 mysqli 中建立连接才能使上述功能正常工作:

/* JUST REPLACE NECESSARY DATA */
$con = new mysqli("Host", "Username", "Password", "Database");

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

关于php - 动态输入表单和带数组的 php 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35004447/

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