php - 使用 session 获取多个数组值

标签 php mysql database

插入很好,但是如果我从数据库中选择这个值,所有值都很好,但是单个值获取数组问题,我不知道如何解决这个任务。请尽快更新此代码。

这是插入代码......

<?php 
if(isset($_POST['sendmessage'])){
    $entermessage = $_POST['teachermessage'];
    $checkbox_user = $_POST['usernameallcheckbx'];
    $arr = implode(',',$checkbox_user);
    //$arr2 = explode(',',$arr);

    $insert = mysql_query("INSERT into usermessages(fromteacher,toparent,messages) VALUES('".$_SESSION['username']."','$arr','$entermessage')");
    if($insert == 1){
        echo "<h1>successful</h1>";
    }
    else{
        echo mysql_error();
    }
}
?>

<form method="post">
    <div style="float:left; width:450px;"><br/><br/>Message: <br/>
    <textarea style="width:400px; height:300px;" name="teachermessage"></textarea><br/>
    <input type="submit" value="Send" name="sendmessage" /></div>
    <div style="float:left; with:200px; padding-top:55px;">
    <table>
    <tr>    
    <?php 
    $select_query1 = mysql_query("SELECT * FROM register_user WHERE teacher='$teachername'");
while($chckbx=mysql_fetch_array($select_query1))
    {
        ?>
        <td><?php echo "<input type='checkbox' name='usernameallcheckbx[]' value=". $chckbx['userid']." />"; ?></td>
        </tr>
        <tr>
        <td><?php echo $chckbx['parent_fname']." ".$chckbx['parent_lname']; ?></td>
        </tr>
        <?php
    }
?>

</table>
</div>
</form>

这是选择代码......

<h2>Messages</h2>

<?php 
$select_query3 = mysql_query("SELECT * FROM usermessages");
$fetch= mysql_fetch_array($select_query3);
$data=$fetch['toparent'];
$arr=explode(',',$data);
//$userids=explode(',',$data);
$sessionshow=$_SESSION['userid'];
$userids=in_array("$sessionshow",$arr);

$select_query2 = mysql_query("SELECT * FROM usermessages WHERE toparent in ('$userids')='".$_SESSION['userid']."'"); 


?>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <th>Teacher Name</th>
    <th>Message</th>
    </tr>

    <?php
    while($fetch_name2=mysql_fetch_array($select_query2))
        {   
            echo "<tr>";
            echo "<td>".$fetch_data=$fetch_name2['fromteacher']."</td>";
            echo "<td>".$fetch_data=$fetch_name2['messages']."</td>";
            echo "</tr>";
        }
?>                                   
</table>

最佳答案

我只是把这个作为答案来显示一些不正确的代码 - 从循环中删除获取数据,因为它不执行任何操作

while($fetch_name2=mysql_fetch_array($select_query2))
{   
    echo "<tr>";
    echo "<td>".$fetch_name2['fromteacher']."</td>";
    echo "<td>".$fetch_name2['messages']."</td>";
    echo "</tr>";
}

更好的是使用 HEREDOC:

while($fetch_name2=mysql_fetch_array($select_query2))
{   
    echo <<<EOF
<tr>
   <td>{$fetch_name2['fromteacher']}</td>
   <td>{$fetch_name2['messages']}</td>
</tr>
EOF;
}

关于php - 使用 session 获取多个数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24306427/

相关文章:

php - 检查外部文件的大小,php

mysql - 在 docker mysql 容器中启用日志记录

mysql - 如果经常使用它们,我应该将宽 mysql 表上的列移到左侧吗?

简单连接到 MySQL 时 PHP 超时 - 在 win2k8 64 位 IIS 7/FastCGI php 上 - 新安装

php - Yii ActiveRecord - 有没有办法只更新脏属性?

mysql - 在 Perl 中实现跨线程/进程队列

php - 将 csv 导入 mySQL 数据库时出现西里尔字母编码问题

php - 只允许页面指定页码

PHP,如何以某种格式获取当前日期

mysql - 为什么以下查询会复制表数据?