我被要求创建一个基于PHP和mySQL的时间表。
我创建了一个用于插入数据的页面和另一个用于显示数据和编辑/删除所显示数据的页面。
以下是显示数据并编辑/删除数据的页面:
<?php
while($info = mysql_fetch_array( $data )) {
?>
<tr>
<td> <input type="checkbox" name="job" id="<?php echo $info['job_code']?>" value="<?php echo $info['job_code']?>" /></td>
<td><?php echo $info['job_code'] ?></td>
<td><?php echo $info['job_desc'] ?> </td>
<td><?php echo $info['job_client'] ?> </td>
<td><?php echo $info['job_year'] ?> </td>
<td><?php echo $info['job_month']?> </td>
<td><?php echo $info['job_date']?> </td>
<td><?php echo $info['job_category']?> </td>
<td>EDIT</td>
<td>DELETE</td>
</tr>
<?php } ?>
这是删除过程
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$admin_data_query = mysql_query("SELECT * FROM jobs")or die(mysql_error());
$job_code_query = $_POST['job'];
$job_code_query_items = 'IN ( ';
foreach ($job_code_query as $key => $value) {
$job_code_query_items .= ($key > 0 ? ", '" . $value . "'" : "'" . $value . "'");
}
$job_code_query_items .= ' );';
while ($admin_data = mysql_fetch_array($admin_data_query)) {
if (isset($_POST[$admin_data['job_code']])) {
$admin_sql = "DELETE FROM jobs WHERE job_code = $job_code_query_items";
$admin_query = mysql_query($admin_sql) or die(mysql_error());
$this->messages[] = "Job deleted.";
} else {
$this->messages[] = "Failed to delete job.";
}
}
根据我的理解,第2页中的job []变量没有捕获第1页中的用户提交复选框,或者第一页没有发送数据,因为复选框值(也许我写错了)
编辑:我找到了答案,这里是正确的代码: 第1页
<td>
<input type="checkbox" name="<?php echo $info['job_code']?>"
id="<?php echo $info['job_code']?>"
value="<?php echo $info['job_code']?>"/>
</td>
<td><?php echo $info['job_code'] ?></td>
<td><?php echo $info['job_desc'] ?> </td>
<td><?php echo $info['job_client'] ?> </td>
<td><?php echo $info['job_year'] ?> </td>
<td><?php echo $info['job_month']?> </td>
<td><?php echo $info['job_date']?> </td>
<td><?php echo $info['job_category']?> </td>
处理页面(2)
while ($admin_data = mysqli_fetch_array($admin_data_query)) {
$job_code_query = $admin_data["job_code"];
if (isset($_POST[$admin_data["job_code"]])) {
$admin_sql = "DELETE FROM jobs WHERE job_code = '$job_code_query'";
$admin_query = mysqli_query($this->db_connection, $admin_sql) or die(mysqli_error($this->db_connection));
$this->messages[] = "Job deleted.";
} else {
$this->messages[] = "Failed to delete job.";
}
}
感谢所有试图帮助或阅读它的人!
答案 0 :(得分:0)
将$job_code_query = $_POST['job[]'];
更改为$job_code_query = $_POST['job'];
但这会导致另一个问题,因为$job_code_query
是一个数组,因此您无法将数组发送到查询$admin_sql ="DELETE FROM jobs WHERE job_code = '$job_code_query'";
你必须修复上面的定义,并在将数组添加到查询中之前将其转换为字符串
你的代码是:
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$admin_data_query = mysql_query("SELECT * FROM jobs")or die(mysql_error());
$job_code_query = $_POST['job'];
$job_code_query_items = 'IN ( ';
foreach ($job_code_query as $key => $value) {
$job_code_query_items .= ($key>0?", '".$value."'":"'".$value."'");
}
$job_code_query_items .= ' );';
while ($admin_data = mysql_fetch_array($admin_data_query)) {
if (isset($_POST['job_code'])) {
$admin_sql ="DELETE FROM jobs WHERE job_code $job_code_query_items";
$admin_query = mysql_query($admin_sql) or die(mysql_error());
$this->messages[] = "Job deleted.";
} else {
$this->messages[] = "Failed to delete job.";
}
}
答案 1 :(得分:0)
将第29行替换为
$job_code_query = $_POST['job'];
您还应该使用PDO或MySQLi而不是mysql_ *函数,这些函数现已弃用。
使用MySQLi连接,您必须使用$dbconnection->query($sqlquery)
而不是mysql_query