萌新询问 php连接了mysql数据库但不能读取表里的数据

只能录入数据到表里,不能查询我表里先建好的数据

并且录入数据之后,表里的名字是乱码
图片说明

图片说明

1.studentManage.php

<?php
header('Content-Type:text/html;charset=utf-8');
?>
<?php
    session_start();            //启动SESSION
?>
<html>
<head>
    <title>学生管理</title>
</head>
<body bgcolor="#FFFFCC">
<?php
    $XM = $_SESSION['XM'];
    $XB = $_SESSION['XB'];
    $CSSJ = $_SESSION['CSSJ'];
    $KCS = $_SESSION['KCS'];
    $StuName = $_SESSION['StuName'];
?>
<form method="post" action="studentAction.php" enctype="multipart/form-data">
    <table>
        <tr>
            <td>
                <table>
                    <tr>
                        <td>姓名:</td><td><input type="text" name="xm" value="<?php echo @$XM;?>"/></td>
                    </tr>
                    <tr>
                        <td>性别:</td>
                        <?php
                            if(@$XB == 1) {
                        ?>
                        <td>
                            <input type="radio" name="xb" value="1" checked="checked">男
                            <input type="radio" name="xb" value="0">女
                        </td>
                        <?php
                            }else {
                        ?>
                        <td>
                            <input type="radio" name="xb" value="1">男
                            <input type="radio" name="xb" value="0" checked="checked">女
                        </td>
                        <?php
                            }
                        ?>
                    </tr>
                    <tr>
                        <td>出生年月:</td><td><input type="text" name="cssj" value="<?php echo @$CSSJ;?>"/></td>
                    </tr>
                    <tr>
                        <td>照片:</td><td><input name="photo" type="file"></td>                     
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                        <!-- 使用img控件调用showpicture.php页面用于显示照片,studentname用于保存当前学生姓名值,time()函数用于产生一个时间戳,防止服务器读取缓存中的内容-->
                        <?php
                            //echo "<img src='showpicture.php?time=".time()."'>";
                            //if($row['ZP'])
                                echo "<img src='showpicture.php?studentname=$StuName&time=".time()."' width=90 height=120 />";
                                //echo "<img src='showpicture.php?time=".time()."' width=90 height=120 />";
                            //else
                                //echo "<div class=STYLE1>暂无照片</div>";
                        ?>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <input name="btn" type="submit" value="录入">
                            <input name="btn" type="submit" value="删除">
                            <input name="btn" type="submit" value="更新">
                            <input name="btn" type="submit" value="查询">
                        </td>
                    </tr>
                </table>
            </td>
            <td>
                <table>
                    <tr>
                        <td>已修课程<input type="text" name="kcs" size="6" value="<?php echo @$KCS;?>" disabled/></td>
                    </tr>
                    <tr>
                        <td align="left">
                        <?php
                            include "fun.php";
                            $cj_sql = "call CJ_PROC('$StuName')";       //执行存储过程
                            $result = $db->query(iconv('UTF-8', 'UTF-8', $cj_sql));
                            $xmcj_sql = "select * from XMCJ_VIEW";      //从XMCJ_VIEW表中查询出学生成绩信息
                            $cj_rs = $db->query($xmcj_sql);
                            //输出表格
                            echo "<table border=1>";
                            echo "<tr bgcolor=#CCCCC0>";
                            echo "<td>课程名</td><td align=center>成绩</td></tr>";
                            while(list($KCM, $CJ) = $cj_rs->fetch(PDO::FETCH_NUM)) {            //获取成绩结果集
                                $KC = iconv('UTF-8', 'UTF-8', $KCM);
                                echo "<tr><td>$KC&nbsp;</td><td align=center>$CJ</td></tr>";    //在表格中显示输出"课程名-成绩"信息
                            }
                            echo "</table>";
                        ?>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

2.studentAction.php

<?php
header('Content-Type:text/html;charset=utf-8');
?>
<?php
    include "fun.php";
    include "studentManage.php";
    $StudentName = @$_POST['xm'];                   //姓名
    $Sex = @$_POST['xb'];                           //性别
    $Birthday = @$_POST['cssj'];                    //生日
    $tmp_file = @$_FILES["photo"]["tmp_name"];      //文件被上传后在服务端储存的临时文件
    $handle = @fopen($tmp_file,'rb');               //打开文件
    $Picture = @base64_encode(fread($handle, filesize($tmp_file))); //读取上传的照片变量并编码

    $s_sql = "select XM, KCS from XS where XM ='$StudentName'";     //查找姓名、已修课程数信息
    $s_result = $db->query(iconv('GB2312', 'UTF-8', $s_sql));

    /**以下为各学生管理操作按钮的代码*/
    //录入功能
    if(@$_POST["btn"] == '录入') {                    //单击"录入"按钮
        if($s_result->rowCount() != 0)              //要录入的学生姓名已经存在时提示
            echo "<script>alert('该学生已经存在!');location.href='studentManage.php';</script>";
        else {
            if(!$tmp_file) {                        //没有上传照片的情况
                $insert_sql = "insert into XS values('$StudentName', $Sex, '$Birthday', 0, NULL, NULL)";
            }else {
                $insert_sql = "insert into XS values('$StudentName', $Sex, '$Birthday', 0, NULL, '$Picture')";
            }
            $insert_result = $db->query(iconv('GB2312', 'UTF-8', $insert_sql));
            if($insert_result->rowCount() != 0) {
                $_SESSION['StuName'] = $StudentName;
                echo "<script>alert('添加成功!');location.href='studentManage.php';</script>";
            }else
                echo "<script>alert('添加失败,请检查输入信息!');location.href='studentManage.php';</script>";
        }
    }

    //删除功能
    if(@$_POST["btn"] == '删除') {                    //单击"删除"按钮
        if($s_result->rowCount() == 0)              //要删除的学生姓名不存在时提示
            echo "<script>alert('该学生不存在!');location.href='studentManage.php';</script>";
        else {                                      //处理姓名存在的情况
            list($XM, $KCS) = $s_result->fetch(PDO::FETCH_NUM);
            if($KCS != 0)                           //学生有修课记录时提示
                echo "<script>alert('该生有修课记录,不能删!');location.href='studentManage.php';</script>";
            else {                                  //删除操作
                $del_sql = "delete from XS where XM ='$StudentName'";
                $del_affected = $db->exec(iconv('GB2312', 'UTF-8', $del_sql));
                if($del_affected) {
                    $_SESSION['StuName'] = 0;
                    echo "<script>alert('删除成功!');location.href='studentManage.php';</script>";
                }
            }
        }
    }

    //更新功能
    if(@$_POST["btn"] == '更新'){                     //单击"更新"按钮
        $_SESSION['StuName'] = $StudentName;        //将用户输入的姓名用SESSION保存
        if(!$tmp_file)                              //若没有上传文件则不更新照片列
            $update_sql = "update XS set XB =$Sex, CSSJ ='$Birthday' where XM ='$StudentName'";
        else
            $update_sql = "update XS set XB =$Sex, CSSJ ='$Birthday', ZP='$Picture' where XM ='$StudentName'";
        $update_affected = $db->exec(iconv('GB2312', 'UTF-8', $update_sql));
        if($update_affected)
            echo "<script>alert('更新成功!');location.href='studentManage.php';</script>";
        else
            echo "<script>alert('更新失败,请检查输入信息!');location.href='studentManage.php';</script>";
    }

    //查询功能
    if(@$_POST["btn"] == '查询') {                    //单击"查询"按钮
        $_SESSION['StuName'] = $StudentName;        //将姓名传给其他页面
        $sql = "select XM, XB, CSSJ, KCS from XS where XM ='$StudentName'"; //查找姓名对应的学生信息
        $result = $db->query(iconv('GB2312', 'UTF-8', $sql));
        if($result->rowCount() == 0)                                                                        //判断该学生是否存在
            echo "<script>alert('该学生不存在!');location.href='studentManage.php';</script>";
        else {
            list($XM, $XB, $CSSJ, $KCS) = $result->fetch(PDO::FETCH_NUM);
            $_SESSION['XM'] = iconv('UTF-8', 'GB2312', $XM);
            $_SESSION['XB'] = $XB;
            $_SESSION['CSSJ'] = $CSSJ;
            $_SESSION['KCS'] = $KCS;
            echo "<script>location.href='studentManage.php';</script>";
        }
    }
?>

可以贴一部分代码出来看下 操作sql的部分

乱码的问题 加一行

mysql__query('set names utf8');

读取数据 遍历一下就可以了