課程
/后端開發
/PHP
/Duang~MySQLi擴展庫來襲
表格怎么插不進數據
2017-04-09
源自:Duang~MySQLi擴展庫來襲 2-7
正在回答
你的 INSERT 后面少寫 INTO
你把$password的那個md5去掉試試看?? 就是不用md5來加密,直接插入數據
求大神幫忙解答,是數據庫的問題嗎
userlist代碼:
<?php
$mysqli= new mysqli("localhost","root","","bb");
if($mysqli->errno){
? ? die('Connect Error:'.$mysqli->error);
}else{
? ? $mysqli->set_charset('UTF8');
}
$sql="SELECT id,username,age FROM user";
$mysqli_result=$mysqli->query($sql);
if($mysqli_result && $mysqli_result->num_rows>0)
{
? ? while($row=$mysqli_result->fetch_assoc())
? ? {
? ? ? ? $rows[]=$row;
? ? }
//print_r($rows)
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登錄成功</title>
</head>
<body>
<h2>用戶列表-<a href='adduser.php'>添加用戶</a></h2>
? ? ? <table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>
? ? ? ? ? <tr>
? ? ? ? ? ? ?<td>編號</td>
? ? ? ? ? ? ?<td>用戶名</td>
? ? ? ? ? ? ?<td>年齡</td>
? ? ? ? ? ? ?<td>操作</td>
? ? ? ? ? </tr>
? ? ? ? ? <?php $i=1;foreach ($rows as $row):?>
? ? ? ? ? ? ?<td><?php echo $i;?></td>
? ? ? ? ? ? ?<td><?php echo $row['username']?></td>
? ? ? ? ? ? ?<td><?php echo $row['age']?></td>
? ? ? ? ? ? ?<td><a href="edituser.php">更新</a>|<a href="doaction.php">刪除</a></td>
? ? ? ? ? <?php $i++; endforeach;?>
? ? ? ? ?
? ? ? </table>
</body>
</html>
addUser代碼:
/*<!DOCTYPE html>
<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>
? ? ? ? ? ? <td>密碼</td>
? ? ? ? ? ? <td><input type="password" name="password" id="" min='1' max='125' placeholder='請輸入密碼' required="required"/></td>
? ? ? ? ? ? <td>年齡</td>
? ? ? ? ? ? <td><input type="number" name="age" id="" placeholder='請輸入合法年齡' required="required"/></td>
? ? ? ? ? ??
? ? ? ? ? ? <td colspan='2'><input type="submit" value="添加用戶"/></td>
? ? </table>
? ??
</form>
*/
//這是addUser.php頁面代碼
? ? <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
? ? <title>添加用戶</title>
<form action="doAction.php?act=addUser" method="post">
<table cellpadding="0" cellspacing="0" bgcolor="#ABCDEF" width="45%" border="1">
? ? <tr>
? ? ? ? <td>用戶名</td>
? ? ? ? <td><input type="text" name="username" placeholder="請輸入用戶名" required="required"></td>
? ? </tr>
? ? ? ? <td>密碼</td>
? ? ? ? <td><input type="password" name="password" placeholder="請輸入密碼" required="required"></td>
? ? ? ? <td>年齡</td>
? ? ? ? <td><input type="text" name="age" min="1" max="125" placeholder="請輸入年齡"></td>
? ? ? ? <td colspan="2"><input type="submit" name="" value="提交"></td>
</table>
doAction代碼:
//接收頁面
header('Content-Type" content="text/html; charset=utf-8');
$mysqli->set_charset('utf8');
$username=$_POST['username'];
$username=$mysqli->escape_string($username);
$password=md5($_POST['password']);
$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{
? ? ? ? ? ? alert('添加失敗,重新添加');
? ? ? ? ? ? location.href='addUser.php';
? ? ? ? }
? ? ? ? break;
舉報
本教程從面向對象和面向過程兩個方面為你開啟MySQLi學習之旅
3 回答代碼 沒錯 怎么不能插入表??
2 回答說一下關于數據不能插入到數據庫中的問題
1 回答在if條件中輸入$res,不是等于第二次進行數據插入了嗎
6 回答為什么創建表格不成功 顯示bool false
1 回答如果資金不足,數據庫為負數了怎辦?沒有說怎么處理?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2020-03-15
你的 INSERT 后面少寫 INTO
2017-05-24
你把$password的那個md5去掉試試看?? 就是不用md5來加密,直接插入數據
2017-04-09
求大神幫忙解答,是數據庫的問題嗎
2017-04-09
userlist代碼:
<?php
$mysqli= new mysqli("localhost","root","","bb");
if($mysqli->errno){
? ? die('Connect Error:'.$mysqli->error);
}else{
? ? $mysqli->set_charset('UTF8');
}
$sql="SELECT id,username,age FROM user";
$mysqli_result=$mysqli->query($sql);
if($mysqli_result && $mysqli_result->num_rows>0)
{
? ? while($row=$mysqli_result->fetch_assoc())
? ? {
? ? ? ? $rows[]=$row;
? ? }
}
//print_r($rows)
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登錄成功</title>
</head>
<body>
<h2>用戶列表-<a href='adduser.php'>添加用戶</a></h2>
? ? ? <table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>
? ? ? ? ? <tr>
? ? ? ? ? ? ?<td>編號</td>
? ? ? ? ? ? ?<td>用戶名</td>
? ? ? ? ? ? ?<td>年齡</td>
? ? ? ? ? ? ?<td>操作</td>
? ? ? ? ? </tr>
? ? ? ? ? <?php $i=1;foreach ($rows as $row):?>
? ? ? ? ? <tr>
? ? ? ? ? ? ?<td><?php echo $i;?></td>
? ? ? ? ? ? ?<td><?php echo $row['username']?></td>
? ? ? ? ? ? ?<td><?php echo $row['age']?></td>
? ? ? ? ? ? ?<td><a href="edituser.php">更新</a>|<a href="doaction.php">刪除</a></td>
? ? ? ? ? </tr>
? ? ? ? ? <?php $i++; endforeach;?>
? ? ? ? ?
? ? ? </table>
</body>
</html>
addUser代碼:
/*<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登錄成功</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="" min='1' max='125' placeholder='請輸入密碼' required="required"/></td>
? ? ? ? </tr>
? ? ? ? <tr>
? ? ? ? ? ? <td>年齡</td>
? ? ? ? ? ? <td><input type="number" name="age" id="" placeholder='請輸入合法年齡' required="required"/></td>
? ? ? ? </tr>
? ? ? ? <tr>
? ? ? ? ? ??
? ? ? ? ? ? <td colspan='2'><input type="submit" value="添加用戶"/></td>
? ? ? ? </tr>
? ? </table>
? ??
</form>
</body>
</html>
*/
//這是addUser.php頁面代碼
<!DOCTYPE html>
<html>
<head>
? ? <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
? ? <title>添加用戶</title>
</head>
<body>
<h2>添加用戶</h2>
<form action="doAction.php?act=addUser" method="post">
<table cellpadding="0" cellspacing="0" bgcolor="#ABCDEF" width="45%" border="1">
? ? <tr>
? ? ? ? <td>用戶名</td>
? ? ? ? <td><input type="text" name="username" placeholder="請輸入用戶名" required="required"></td>
? ? </tr>
? ? <tr>
? ? ? ? <td>密碼</td>
? ? ? ? <td><input type="password" name="password" placeholder="請輸入密碼" required="required"></td>
? ? </tr>
? ? <tr>
? ? ? ? <td>年齡</td>
? ? ? ? <td><input type="text" name="age" min="1" max="125" placeholder="請輸入年齡"></td>
? ? </tr>
? ? <tr>
? ? ? ? <td colspan="2"><input type="submit" name="" value="提交"></td>
? ? </tr>
</table>
</form>
</body>
</html>
doAction代碼:
<?php
//接收頁面
header('Content-Type" content="text/html; charset=utf-8');
$mysqli= new mysqli("localhost","root","","bb");
if($mysqli->errno){
? ? die('Connect Error:'.$mysqli->error);
}
$mysqli->set_charset('utf8');
$username=$_POST['username'];
$username=$mysqli->escape_string($username);
$password=md5($_POST['password']);
$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;
}