gpt4 book ai didi

php - 如何使用PHP将动态文本框保存到数据库

转载 作者:行者123 更新时间:2023-11-27 22:36:54 24 4
gpt4 key购买 nike

我的 JS 脚本在用户单击“添加项目”按钮时动态添加三个文本框。这三个文本框在保存到数据库时具有不同的字段。但似乎在我的 PHP 代码中它没有从文本框中捕获任何值。在数据库中它只显示“Array”。请帮我!谢谢!

<script language="javascript">
row_no=0;
function addRow(tbl,row){
row_no++;
if (row_no<=20){
if (row_no<=20){
if (row_no>=10){
var textbox = row_no+'.)&nbsp;<input type="text" size = "50" maxlength= "50" name= "desk_user[]">';}
if (row_no<10){
var textbox = row_no+'. )&nbsp;<input type="text" size = "20" maxlength= "50" name= "desk_user[]">';}
var textbox2 = '<input type="text" size = "60" maxlength= "250" name= "desk_report[]">';
var textbox3 = '<input type="text" size = "60" maxlength= "250" name= "desk_action[]">';

var tbl = document.getElementById(tbl);
var rowIndex = document.getElementById(row).value;
var newRow = tbl.insertRow(row_no);
var newCell = newRow.insertCell(0);
newCell.innerHTML = textbox;
var newCell = newRow.insertCell(1);
newCell.innerHTML = textbox2;
var newCell = newRow.insertCell(2);
newCell.innerHTML = textbox3;
}
if (row_no>20){
alert ("Too Many Items. Limit of 20.");
}
}
}

</script>

我的 PHP 代码:

<?php

$con = mysql_connect ("localhost","root","nasi") or die
('cannot connect to database error: '.mysql_error());


if (isset($_POST['desk_user']) &&
isset($_POST['desk_report']) &&
isset($_POST['desk_action']))
{

$desk_user = $_POST['desk_user'];
$desk_report = $_POST['desk_report'];
$desk_action = $_POST['desk_action'];

if (!empty($desk_user)&& !empty($desk_report)&& !empty($desk_action)) {

mysql_select_db("csr", $con);
$sql = "INSERT INTO `desktoplaptop` (`desk_user`,`desk_report`,`desk_action`) VALUES ('$_POST[desk_user]','$_POST[desk_report]','$_POST[desk_action]') ";

if ($sql_run = mysql_query($sql)) {
echo 'ok.';
}
else {
echo '*Sorry, we couldn\'t register you at this time. Try again later.';
}
}

}

?>

最佳答案

您正在获取数组形式的值,因为您已将文本框名称定义为数组,

<input type="text" size = "20"  maxlength= "50" name= "desk_user[]">

像这样修改你的代码

 <?php

$con = mysql_connect ("localhost","root","nasi") or die
('cannot connect to database error: '.mysql_error());


if (isset($_POST['desk_user']) &&
isset($_POST['desk_report']) &&
isset($_POST['desk_action']))
{

$desk_user_arr = $_POST['desk_user'];//contains array value
$desk_report = $_POST['desk_report'];//contains array value
$desk_action = $_POST['desk_action'];//contains array value
foreach($desk_user_arr as $key=>$desk_user) { //Loop through arrays
if (!empty($desk_user) && !empty($desk_report[$key]) && !empty($desk_action[$key])) {
mysql_select_db("csr", $con);
$sql = "INSERT INTO `desktoplaptop` (`desk_user`,`desk_report`,`desk_action`) VALUES ('$desk_user','$desk_report[$key]','$desk_action[$key]') ";

if ($sql_run = mysql_query($sql)) {
echo 'ok.';
} else {
echo '*Sorry, we couldn\'t register you at this time. Try again later.';
}
}
}
}
?>

关于php - 如何使用PHP将动态文本框保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13448922/

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