<?php
//case 17 用户信息编辑

/*
 * 建表SQL语句
create table emp_userinfo(
    id int unsigned auto_increment primary key,  --主键
    nick_name varchar(10) not null,  --昵称
    gender enum('男性','女性','中性') not null,  --性别
    email varchar(40) not null, -- 邮箱
    qq varchar(20) not null, --
    url varchar(200) not null, -- 个人主页
    city varchar(10) not null, -- 城市
    skill text not null, -- 语言技能
    description text not null  -- 自我介绍
)charset=utf8;
 *
 *insert into emp_userinfo values(1,'小明','男性','test@123.com','12345678','http://xx.com','北京','JavaScript,PHP','虾米大王');
 *
 */
//连接数据库
$connect = mysqli_connect('localhost','root','mysql123','test');
if(!$connect)
{
    die('数据库连接失败!').mysqli_error($connect);
}
//设置字符集
mysqli_query($connect,"set names utf8");
//数组
$city = array('北京','兰州');
$skill = array('HTML','JavaScript','PHP','C++');
$id = 1;
//查询语句
$sql = "select * from emp_userinfo where id=$id";
//取数据
$result = mysqli_query($connect,$sql);
$data = mysqli_fetch_assoc($result);
$data['skill'] = explode(',',$data['skill']);
?>

<html>
<head>
    <title>用户信息编辑</title>
</head>
<body>
    <form method="post">
        <p>昵称:<input type="text" name="nick_name" id="nick_name" value="<?= $data['nick_name'];?>"></p>
        <p>
            性别:
            <input type="radio" name="gender" value="男性" id="gender_1" <?php if($data['gender'] == '男性') echo "checked='checked'";?>>
            <label for="gender_1">男性</label>
            <input type="radio" name="gender" value="女性" id="gender_2" <?php if($data['gender'] == '女性') echo "checked='checked'";?>>
            <label for="gender_2">女性</label>
            <input type="radio" name="gender" value="中性" id="gender_3" <?php if($data['gender'] == '中性') echo "checked='checked'";?>>
            <label for="gender_3">中性</label>
        </p>
        <p>邮箱:<input type="text" name="email" id="email" value="<?= $data['email'];?>"></p>
        <p>QQ号:<input type="text" name="qq" id="qq" value="<?= $data['qq'];?>"></p>
        <p>个人主页:<input type="text" name="url" id="url" value="<?= $data['url'];?>"></p>
        <p>所在城市:
            <select name="city" id="city">
                <option value="未选择">未选择</option>
                <?php
                //绑定数据
                foreach ($city as $val)
                {
                    if($data['city'] == $val)
                    {
                        echo "<option value='$val' selected='selected'>$val</option>";
                    }
                    else
                    {
                        echo "<option value='$val'>$val</option>";
                    }
                }
                ?>
            </select>
        </p>
        <p>语言技能:
            <?php foreach ($skill as $val):?>
            <input type="checkbox" name="skill[]" value="<?= $val;?>" id="<?= $val;?>" <?php if(in_array($val,$data['skill'])) echo "checked='checked'";?>>
            <label for="<?= $val;?>"><?= $val;?></label>
            <?php endforeach;?>
        </p>
        <p>自我介绍:<textarea name="description" rows="10" cols="30"><?= $data['description'];?></textarea></p>
        <p><input type="submit" name="submit" value="保存资料"><input type="reset" value="重新填写"></p>
    </form>
</body>
</html>

<?php
//保存
if(isset($_POST['submit']))
{
    $fields = array('nick_name','gender','email','qq','url','city','skill','description');
    foreach ($fields as $val)
    {
        $save_data[$val] = isset($_POST[$val])?$_POST[$val]:'';
    }

    if($save_data['gender'] != '男性' && $save_data['gender'] !='女性' && $save_data['gender'] != '中性')
    {
        die('保存失败:未选择性别。');
    }

    if($save_data['city'] !='未选择' && !in_array($save_data['city'],$city))
    {
        die('保存失败:城市不在列表中。');
    }

    if(is_array($save_data['skill']))
    {
        $save_data['skill'] = array_intersect($skill,$save_data['skill']);
        $save_data['skill'] = implode(',',$save_data['skill']);
    }
    else
    {
        $save_data['skill'] = '';
    }

    $sql = "update emp_userinfo set ";
    foreach ($save_data as $key=>$val)
    {
        $sql .= "$key='".mysqli_real_escape_string($connect,$val)."',";
    }
    //删除尾部多余的逗号
    $sql = rtrim($sql,',')." where id='$id'";
    //echo $sql;
    $rst = mysqli_query($connect,$sql);
    if ($rst > 0)
    {
        echo "<script>alert('保存成功!');location.href='t207.php';</script>";
    }



}
?>