我还在学习服务器端代码,这就是为什么我有这个问题。到目前为止,我有一个只能插入一行的php脚本。这里是:如何编写我的php脚本来将多行插入到数据库中?
<?php
$response = array();
if(isset($_POST['team_full_name']) && isset($_POST['team_short_name'] && isset($_POST['league']))
{
$team_fn = $_POST['team_full_name'];
$team_sn = $_POST['team_short_name'];
$league = $_POST['league'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("INSERT INTO teamData(team_full_name, team_short_name, league)
VALUES('$team_fn', '$team_sn' '$league')");
if($result)
{
$response["success"] = 1;
$response["message"] = "Team Data successfully added.";
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Error occurred!";
echo json_encode($response);
}
}
else
{
$response["success"] = 0;
$response["message"] = "Required field(s) is missing.";
echo json_encode($response);
}
?>
但在这种情况下,我将张贴整个数组持有多行的JSON字符串。我如何设置php脚本来处理多行?
我正在转换成JSON字符串类是:
public class TeamData
{
String mFullName;
String mShortName;
String mLeague;
....
}
很抱歉,如果我忘记了一些重要的信息,我完全新的PHP和Web服务器的工作。我可以编辑帖子,如果你说明我需要添加什么来使帖子更清晰。因为你把你的查询长度为几十千字节
INSERT INTO teamData(team_full_name, team_short_name, league) VALUES
('$val', '$val', '$val'),
('$val', '$val', '$val'),
('$val', '$val', '$val'),
('$val', '$val', '$val'),
('$val', '$val', '$val')
,只要你想你可以把尽可能多的行的价值值,只要:
PHP中的'mysql_ *'函数已被弃用,不应使用。请阅读[为什么不应该在PHP中使用mysql_ *函数?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)了解有关为什么和用什么来代替它们。 –