課程
/后端開發(fā)
/PHP
/Duang~MySQLi擴(kuò)展庫來襲
請(qǐng)問各位大神,addUser.php"添加數(shù)據(jù)失敗,請(qǐng)重新添加”是什么原因?
2016-11-03
源自:Duang~MySQLi擴(kuò)展庫來襲 2-7
正在回答
數(shù)據(jù)庫字段的問題,導(dǎo)致插不進(jìn)數(shù)據(jù)。
問題已經(jīng)解決,謝謝,是mysql phpmyadmin數(shù)據(jù)庫的問題!
你先把if那一段都注釋掉
然后打印一下$sql(黏貼到數(shù)據(jù)庫里執(zhí)行一下)和$res看看是不是sql語句出錯(cuò)了
看出問題所在了嗎?
user_list.php
<?php
$mysqli=new mysqli('localhost','root','123456','admin');
if($mysqli->connect_errno){
die('CONNECT ERROR:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$sql="SELECT id,username,age FROM db";
$mysqli_result=$mysqli->query($sql);
if($mysqli_result && $mysqli_result->num_rows>0){
while($row=$mysqli_result->fetch_assoc()){
$rows[]=$row;
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
</head>
<body>
<h2>用戶列表-<a href="addUser.php">添加用戶</a></h2>
<table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#ABCDEF">
? ? <tr>
? ? ? ? <td>編號(hào)</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?id=<?php echo $row['id'];?>">更新</a><a href="do_Action.php?act=delUser&id=<?php echo $row['id'];?>">刪除</a></td>
? ? ? ? ?<?php $i++;endforeach;?>
? ? </table>
</body>
</html>
do_Action.php
header('content-type:text/html;charset=utf-8');
//接收頁面
die($mysqli->connect_error);
$username=$_POST['username'];
$username=$mysqli->escape_string($username);//escape_string轉(zhuǎn)義特殊字符///的SQL語句中使用字符串
$password=md5($_POST['password']);
$age=$_POST['age'];
$act=$_GET['act'];
$id=$_GET['id'];
//根據(jù)不同操作完成不同功能
switch($act){
case 'addUser':
//echo '添加用戶的操作';
$sql="INSERT db(username,password,age) VALUES('{$username}','{$password}','{$age}')";
$res=$mysqli->query($sql);
if($res){
$insert_id=$mysqli->insert_id;
echo "<script type='text/javascript'>
alert('添加成功,網(wǎng)站的第{$insert_id}位用戶');
location.href='user_list.php';
</script>";
}else{
alert('添加失敗,重新添加');
location.href='addUser.php';
break;
case 'delUser':
//echo '刪除記錄'.$id;
$sql="DELETE FROM db WHERE id=".$id;
$mes='刪除成功';
$mes='刪除失敗';
$url='user_list.php';
alert('{$mes}');
location.href='{$url}';
addUser.php
<h2>添加用戶</h2>
<form action="do_Action.php?act=addUser" method='post'>
<table border="1" cellpadding="0" cellspacing="0" bgcolor="#ABCDEF" width="80%">
? ? ? ? <td>用戶名</td>
? ? ? ? ? ? <td><input type="text" name="username" id="" placeholder="請(qǐng)輸入合法用戶名" required="required"/></td>
? ? ? ? </tr>
? ? ? ? <tr>
? ? ? ? <td>密碼</td>
? ? ? ? ? ? <td><input type="password" name="password" id="" placeholder="請(qǐng)輸入密碼" required="required"/></td>
? ? ? ? <td>年齡</td>
? ? ? ? ? ? <td><input type="number" name="age" id="" min='1' max='125' placeholder="請(qǐng)輸入合法年齡" required="required"/></td>
? ? ? ? <td colspan='2'><input type="submit" value="添加用戶"/></td>
</form>
請(qǐng)附上代碼具體分析
舉報(bào)
本教程從面向?qū)ο蠛兔嫦蜻^程兩個(gè)方面為你開啟MySQLi學(xué)習(xí)之旅
3 回答老師,addUser.php:數(shù)據(jù)“添加失敗,重新添加”是什么原因?
1 回答跟源碼一模一樣就是不知道哪里錯(cuò)了總是報(bào)添加失敗
1 回答跳轉(zhuǎn)以后顯示的條數(shù)是未添加的條數(shù)
3 回答添加成功,頁面不跳轉(zhuǎn),空白頁面;刪除失敗,提取不到ID
2 回答哪里錯(cuò)了呢,為什么轉(zhuǎn)賬失敗QUEEN還是加兩百
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-11-10
數(shù)據(jù)庫字段的問題,導(dǎo)致插不進(jìn)數(shù)據(jù)。
2016-11-10
問題已經(jīng)解決,謝謝,是mysql phpmyadmin數(shù)據(jù)庫的問題!
2016-11-10
你先把if那一段都注釋掉
然后打印一下$sql(黏貼到數(shù)據(jù)庫里執(zhí)行一下)和$res看看是不是sql語句出錯(cuò)了
2016-11-04
看出問題所在了嗎?
2016-11-04
user_list.php
<?php
$mysqli=new mysqli('localhost','root','123456','admin');
if($mysqli->connect_errno){
die('CONNECT ERROR:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$sql="SELECT id,username,age FROM db";
$mysqli_result=$mysqli->query($sql);
if($mysqli_result && $mysqli_result->num_rows>0){
while($row=$mysqli_result->fetch_assoc()){
$rows[]=$row;
}
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
</head>
<body>
<h2>用戶列表-<a href="addUser.php">添加用戶</a></h2>
<table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#ABCDEF">
? ? <tr>
? ? ? ? <td>編號(hào)</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?id=<?php echo $row['id'];?>">更新</a><a href="do_Action.php?act=delUser&id=<?php echo $row['id'];?>">刪除</a></td>
? ? ? ? ?</tr>
? ? ? ? ?<?php $i++;endforeach;?>
? ? </table>
</body>
</html>
do_Action.php
<?php
header('content-type:text/html;charset=utf-8');
//接收頁面
$mysqli=new mysqli('localhost','root','123456','admin');
if($mysqli->connect_errno){
die($mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$username=$_POST['username'];
$username=$mysqli->escape_string($username);//escape_string轉(zhuǎn)義特殊字符///的SQL語句中使用字符串
$password=md5($_POST['password']);
$age=$_POST['age'];
$act=$_GET['act'];
$id=$_GET['id'];
//根據(jù)不同操作完成不同功能
switch($act){
case 'addUser':
//echo '添加用戶的操作';
$sql="INSERT db(username,password,age) VALUES('{$username}','{$password}','{$age}')";
$res=$mysqli->query($sql);
if($res){
$insert_id=$mysqli->insert_id;
echo "<script type='text/javascript'>
alert('添加成功,網(wǎng)站的第{$insert_id}位用戶');
location.href='user_list.php';
</script>";
}else{
echo "<script type='text/javascript'>
alert('添加失敗,重新添加');
location.href='addUser.php';
</script>";
}
break;
case 'delUser':
//echo '刪除記錄'.$id;
$sql="DELETE FROM db WHERE id=".$id;
$res=$mysqli->query($sql);
if($res){
$mes='刪除成功';
}else{
$mes='刪除失敗';
}
$url='user_list.php';
echo "<script type='text/javascript'>
alert('{$mes}');
location.href='{$url}';
</script>";
break;
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
</head>
<body>
</body>
</html>
addUser.php
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
</head>
<body>
<h2>添加用戶</h2>
<form action="do_Action.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="請(qǐng)輸入合法用戶名" required="required"/></td>
? ? ? ? </tr>
? ? ? ? <tr>
? ? ? ? <td>密碼</td>
? ? ? ? ? ? <td><input type="password" name="password" id="" placeholder="請(qǐng)輸入密碼" required="required"/></td>
? ? ? ? </tr>
? ? ? ? <tr>
? ? ? ? <td>年齡</td>
? ? ? ? ? ? <td><input type="number" name="age" id="" min='1' max='125' placeholder="請(qǐng)輸入合法年齡" required="required"/></td>
? ? ? ? </tr>
? ? ? ? <tr>
? ? ? ? <td colspan='2'><input type="submit" value="添加用戶"/></td>
? ? ? ? </tr>
? ? </table>
</form>
</body>
</html>
2016-11-03
請(qǐng)附上代碼具體分析