为什么不能插入 求大神

来源:2-7 用户管理之添加用户

嘚瑟c

2016-12-02 18:02

添加页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>adduser</title>   

</head>

<body>

<h2>添加用户</h2>

<form action="doAction.php?act=addUser" method='post'>

<table border='1' cellpadding='0' cellspacing='0' bgcolor='#ABCDEF' width='80%'>

<tr>

<td>用户名</td>

<td><input type="text" name="username" id="" placeholder='请输入用户名' required='required' /></td>

</tr>

<tr>

<td>密码</td>

<td><input type="password" name="password" id="" placeholder='请输入密码' required='required' /></td>

</tr>

<tr>

<td>年龄</td>

<td><input type="number" name="age" id="" min='1' max='100' placeholder='请输入年龄' required='required' /></td>

</tr>

<tr>

<td colspan='2'><input type="submit" value="添加用户  " /></td>

</tr>



</table>

</form>

</body>

</html>


doAction页面

<?php

header('content-type:text/html;charset=utf-8');

//接收页面

$mysqli=new mysqli('localhost','root','123456','zcc');

 if($mysqli->connect_errno){

     die('CONNECT ERROR:'.$mysqli->connect_error);

 }

 $mysqli->set_charset('utf-8');

 $username=$_POST['username'];

 $username=$mysqli->escape_string($username);//将用户名中的特殊符号进行转移

 $password=md5($_POST['password']);//选择md5加密

 $age=$_POST['age'];

$act=$_GET['act'];

//根据不同的操作完成不同功能

switch ($act){

    case 'addUser':

        //echo '添加用户操作';

       $sql="INSERT user(username,password,age) VALUES('{$username}','{$password}','{$age}');";

        $res=$mysqli->query($sql);

        if($res) {

            $insert_id=$mysqli->insert_id;

            echo "<script type='text/javascript'>

               alert('添加成功,网站的第{$insert_id}位用户');

                location.href='Userlist.php';

                </script>";

               

        }else {

            echo "<script type='text/javascript'>

           alert('添加失败,重新添加');

            location.href='addUser.php';

            </script>";

        }

        break;

}

似乎不能接受post过来的数据啊?

写回答 关注

4回答

  • 慕粉3531781
    2017-05-24 21:21:49

    我的也是这样的问题  我把md5去掉之后就好了  你试试看是不是这个问题

  • 台农一号
    2016-12-03 22:32:31

     我换我的数据库你的代码在我的电脑上测试完全正常。

  • 嘚瑟c
    2016-12-03 21:58:46

    对着呢  可能是我的数据库问题吧

  • 台农一号
    2016-12-03 14:50:12

    在我的电脑测试时可以传过来代码没有问题http://img.mukewang.com/58426ac30001fdad07090702.jpg是不是跳转页面写错了  检查一下location.href=  的地址

    嘚瑟c

    打印post过来的数据都可以 但是sql语句里面得不到值

    2016-12-03 22:00:40

    共 2 条回复 >

Duang~MySQLi扩展库来袭

本教程从面向对象和面向过程两个方面为你开启MySQLi学习之旅

28651 学习 · 194 问题

查看课程

相似问题