dongmi5607 2017-02-07 11:08
浏览 30
已采纳

如何在表php中使用复选框

Hi I wanted to use a checkbox in a table to update data from my database and also see data from my database, Like i have a field on my database that is called "p_estado" that means status and when the status is at 1 in the database it shows checked and when it's not it doesn't show checked and let me "check" it.

Code:

<?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条回答 默认 最新

  • dongnaopa6200 2017-02-07 11:19
    关注

    replace your checkbox line with,

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

    Give it a try, it will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建