gpt4 book ai didi

php - 使用Ajax+php在mysql表中同时添加多条记录

转载 作者:搜寻专家 更新时间:2023-10-31 22:13:59 24 4
gpt4 key购买 nike

我正在尝试使用 PHP 在 MySQL 数据库中一次插入多行。我想在 3 个不同的行中插入 a、b、c,但它以 a、b、c 的格式插入到 1 行中。

我的代码在下面,它没有给我任何错误,因为代码运行完美,但我得不到我想要的结果。

<?php
require_once('conn.php');
$name = $_POST['name'];
if($name)
{
foreach($name as $std_name)
{
$student[] = $std_name;
// implode($c); i.e a,b,c etc
}
}
$sql = "INSERT INTO `tbl_student`
(`student_name`)
VALUES
('".implode($student, ',')."');";
$res = mysql_query($sql);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var nFloor = "";

function removeField(nField){

nField.parentNode.parentNode.removeChild(nField.parentNode);
}

function insertField(){

var newFieldContainer = document.createElement('div');
var newFieldLabel = document.createElement('label');
newFieldLabel.innerHTML = "Student Name:&nbsp;&nbsp;&nbsp;";
var newField = document.createElement('input');
newField.type = "text";
newField.name = "vipInfo[]";
newFieldContainer.appendChild(newFieldLabel);
newFieldLabel.appendChild(newField);
var deleteBtn = document.createElement('input');
deleteBtn.type = "button";
deleteBtn.value = "Remove";
deleteBtn.style.marginLeft = "5px";
deleteBtn.onclick = function(){removeField(this)};
newFieldContainer.appendChild(deleteBtn);
document.forms[0].insertBefore(newFieldContainer,nFloor);
}

function init(){

var insertBtn = document.getElementById('newFieldBtn')
insertBtn.onclick = function()
{
insertField();
}
nFloor = insertBtn;
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>
</head>
<body>
<form action="" method="post">

<div class="field"><label>Student Name:&nbsp;&nbsp;&nbsp;<input type="text" name="name[]"></label></div>

<input type="button" id="newFieldBtn" value="New Field">
<input type="submit" name="submit" value="Submit">

</form>
</body>
</html>

最佳答案

提出这个问题

$sql = "INSERT INTO `tbl_student` 
(`student_name`)
VALUES
('".implode($student, ',')."');";
$res = mysql_query($sql);

foreach block 内不要使用 implode 或使用下面的查询作为

$sql = "INSERT INTO `tbl_student` 
(`student_name`)
VALUES
('". $std_name."');";

所以它会将每一个添加到不同的行

关于php - 使用Ajax+php在mysql表中同时添加多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9059938/

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