使用会话获取多个数组值

时间:2014-06-19 12:16:46

标签: 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>

2 个答案:

答案 0 :(得分:0)

我只是将此作为显示一些错误代码的答案 - 从循环中删除抓取数据,因为它没有做任何事情

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;
}

答案 1 :(得分:0)

我真的无法弄清楚你想要做什么,但也许这就是你想要的问题:

SELECT * FROM usermessages WHERE FIND_IN_SET('$sessionshow', toparent)

FIND_IN_SET(str, strlist)strlist中以逗号分隔的列表搜索等于str的元素,并返回列表中的位置;如果未找到,则会返回0,其计为false

我无法看到使用$select_query3的任何代码的任何目的。