刚刚在 webslessons 上尝试了您源代码中的代码,它在第一次尝试时就起作用了。 (老实说,第二次尝试,我只需要将文件 jquery.tabledit.min.js 添加到目录中。)
现在关于您的问题。 Tabledit 本身不适用于两个标识符。您可以做的是将两个标识符转储到一列中。为此,我将代码从 webslessons 更改。因此,我使用了一种手工序列化这两个标识符,只需将它们一个接一个地用分号分隔即可。
$id_ser = $row[0].";".$row[1];
我使用这个“序列化”的 id 作为显示表的第一列的值。 (php 函数 serialize() 的序列化不起作用,因为该函数生成了引号,tabledit 的 INPUT_POST 不能很好地传输这些引号。)在文件“action.php”中,必须“反序列化” $id_ser 回来了。这是使用正则表达式实现的:
$id_array = preg_split("/;/",$id_ser);
我将在此处添加更改后的代码。确保还采用文件“action.php”中的“编辑”和“删除”操作代码。希望有用。
index.php:
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$query = "SELECT * FROM tbl_2_ids";
$result = mysqli_query($connect, $query);
?>
<html>
<head>
<title>Live Table Data Edit Delete using Tabledit Plugin in PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="jquery.tabledit.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h3 align="center">Live Table Data Edit Delete using Tabledit Plugin in PHP</h3><br />
<table id="editable_table" class="table table-bordered table-striped">
<thead>
<tr>
<th>ID1;ID2</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($result))
{
$id_ser = $row[0].";".$row[1]; # extract first and second entry of the row.
echo '
<tr>
<td>'.$id_ser.'</td>
<td>'.$row["last_name"].'</td>
</tr>
';
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
action.php:
<?php
//action.php
$connect = mysqli_connect('localhost', 'root', '', 'testing');
$input = filter_input_array(INPUT_POST);
$id_ser = $input["id_ser"];
$id_array = preg_split("/;/",$id_ser);
if($input["action"] === 'edit')
{
$last_name = mysqli_real_escape_string($connect, $input["last_name"]);
$query = "
UPDATE tbl_2_ids
SET last_name = '".$last_name."'
WHERE id0 = '".$id_array[0]."'
AND id1 = '".$id_array[1]."'
";
mysqli_query($connect, $query);
}
if($input["action"] === 'delete')
{
$query = "
DELETE FROM tbl_2_ids
WHERE id0 = '".$id_array[0]."'
AND id1 = '".$id_array[1]."'
";
mysqli_query($connect, $query);
}
echo json_encode($input);
?>
为了使用表“tbl_2_ids”创建数据库“testing”,您可以使用以下批处理文件“database_mysql.batch”。
对于mysql,通过命令执行批处理文件:
mysql -u root -p < database_mysql.batch
database_mysql.batch:
--
-- Database: `testing`
--
-- --------------------------------------------------------
CREATE DATABASE IF NOT EXISTS testing;
USE testing;
--
-- Table structure for table `tbl_2ids`
--
CREATE TABLE IF NOT EXISTS `tbl_2_ids` (
`id0` int(11) NOT NULL,
`id1` int(11) NOT NULL,
`last_name` varchar(250) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
DELETE FROM tbl_2_ids;
--
-- Dumping data for table `tbl_user`
--
INSERT INTO `tbl_2_ids` (`id0`, `id1`, `last_name`) VALUES
(2, 20, 'Smith'),
(3, 21, 'Ferrari'),
(4, 22, 'Mitten'),
(5, 23, 'Noyes'),
(6, 24, 'William'),
(7, 25, 'Hise'),
(8, 26, 'Aguinaldo'),
(9, 27, 'Goad'),
(10, 28, 'Simons'),
(11, 29, 'Huber'),
(12, 30, 'Soliz'),
(13, 31, 'Dismuke'),
(14, 32, 'Thomas');
如果有密码,您必须在命令行中提供密码。同样在“index.php”和“action.php”文件中,如果您配置了root密码,则必须在文件中为root而不是“”提供身份验证。