如何在表php中使用复选框

时间:2017-02-07 11:08:56

标签: php

您好我想使用表格中的复选框来更新数据库中的数据,还可以查看数据库中的数据,就像我的数据库中有一个名为“p_estado”的字段,表示状态,状态为何时它在数据库中显示为1,当它不显示时,它不显示已检查,让我“检查”它。

代码:

<?php
$result = mysqli_query($link, "SELECT * FROM pedidos order by p_id desc")

or die(mysqli_error());

echo "{$_SESSION['login_username']}";
echo "<table border='20' cellpadding='20' width=80% height=30px  >";

echo "<tr> <th>ID</th> <th>Assunto</th> <th>Portal</th> <th>Destinado</th> <th>Estado</th> </tr>";

while($row = mysqli_fetch_array( $result )) {

echo "<tr>";

echo '<td height="50px" > ' . $row['p_id'] . '</td>';
echo '<td height="50px" >' . $row['p_titulo'] . '</td>';
echo '<td height="50px" >' . $row['p_portal'] . '</td>';
echo '<td height="50px" >' . $row['p_autor'] . '</td>';
echo '<td><center><input type="checkbox" name="name1" />&nbsp;</center></td>';
echo '<td><a href="p_editar.php?id=' . $row['p_id'] . '"><center><img src="../imagens/3.png" style="width:20px;height:20px;"></center></a></td>';
echo '<td><a href="p_delete.php?id=' . $row['p_id'] . '"><center><img src="../imagens/2.png" style="width:20px;height:20px;"></center></a></td>';
echo '<td><a href="p_viewall.php?id=' . $row['p_id'] . '"><center><img src="../imagens/7.png" style="width:20px;height:20px;"></center></a></td>';

echo "</tr>";

} 


echo "</table>";
echo "<p><b>Ver Todos</b> | <a href='a_view-paginated.php?page=1'>Ver por Página</a></p>";
?>

</center>
</font>
</body>

</html>

insert.php

<?php
include("../login/session.php");
        if(!empty($_FILES['userImage']['tmp_name']) 
     && file_exists($_FILES['userImage']['tmp_name'])) {
    $imgData= addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
}
        $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
        $portal = mysqli_real_escape_string($link, $_POST['field1']);
        $datacri = mysqli_real_escape_string($link, $_POST['field2']);
        $datacri = date('Y-m-d', strtotime(str_replace('-', '/', $datacri)));
        $datapub = mysqli_real_escape_string($link, $_POST['field3']);
        $datapub = date('Y-m-d', strtotime(str_replace('-', '/', $datapub)));
        $titulo = mysqli_real_escape_string($link, $_POST['field4']);
        $keyword = mysqli_real_escape_string($link, $_POST['field5']);
        $hashtags = mysqli_real_escape_string($link, $_POST['field6']);
        $pedido = mysqli_real_escape_string($link, $_POST['field7']);
        $autor = mysqli_real_escape_string($link, $_POST['field8']);
if (!$portal) { 
echo '<script type="text/javascript"> alert("Preencha todos os campos "); </script>';
header('Refresh: 0; URL=../forms/p_criar.php');
mysqli_close($link);
} 

$sql = "INSERT INTO pedidos (p_id, p_portal, p_datacri, p_datapub, p_titulo, p_keywords, p_hashtags, p_imageType, p_imageData, p_pedido, p_autor,p_nomeuti)
        VALUES('Null', '$portal', '$datacri', '$datapub', '$titulo', '$keyword', '$hashtags', '{$imageProperties['mime']}', '{$imgData}', '$pedido', '$autor', '{$_SESSION['login_username']}')";
if(mysqli_query($link, $sql)){
    echo '<script type="text/javascript"> alert("Portal Criado"); </script>';
    header('Refresh: 0; URL=../forms/p_criar.php');
    } else{
    header('Refresh: 0; URL=../forms/p_criar.php');
    echo "ERRO: Não foi possivel executar o comando $sql. " . mysqli_error($link);
    }
?>

2 个答案:

答案 0 :(得分:1)

将复选框替换为

echo '<td><center><input type="checkbox" name="name1" '.($row['p_estado'] == 1 ? 'checked' : '').' value='1'/>&nbsp;</center></td>';

尝试一下,它会起作用。

答案 1 :(得分:0)

您只需要检查当前行的字段值是否为1,如果是,则只需在CHECKED字段中添加input属性。

echo '<td><center><input type="checkbox" name="name1"';
if($row['p_estado'] == 1) echo ' CHECKED';
echo '/>&nbsp;</center></td>';