我有输入只为数据库输入类名 数据库tbl_class
db_id db_class db_username db_pass
1 test
2 second test
第二个表单从这个
中读取数据库中的类信息<?php
$sql=mysqli_query($conn,"select * from tbl_class")or die(mysqli_error($conn));
echo"<table class='ol-md-12 table-bordered table-striped table-condensed cf table-bordered'>";
echo"<thead class='cf'>";
echo"<tr>";
echo"<th style='background:#f7ac01;font-size:13px;vertical-align: middle;text-align:center'rowspan='2' >ID</th>
<th style='background:#f7ac01;font-size:13px;vertical-align: middle;text-align:center'rowspan='2' >Class</th>
<th style='background:#f7ac01;font-size:13px;text-align:center;vertical-align: middle;text-align:center'rowspan='2' >Username</th>
<th style='background:#f7ac01;font-size:13px;text-align:center;vertical-align: middle;text-align:center'rowspan='2' >Password</th>
<th style='background:#f7ac01;font-size:13px;text-align:center;vertical-align: middle;text-align:center'rowspan='2' >Save</th>
<th style='background:#f7ac01;font-size:13px;text-align:center;vertical-align: middle;text-align:center'rowspan='2' >Delete</th>";
echo"</tr></thead><tbody>";
while($row=mysqli_fetch_array($sql)){
$id=$row['db_id'];
$class=$row['db_class'];
$user=$row['db_username'];
$pass=$row['db_pass'];
echo"<tr>";
echo"<form method='post' action='";?><?php $_PHP_SELF ?><?php echo "'>";
echo"<td data-title='Id'>"; echo $id;
echo "<input type='text' name='txt_id' value='$id'>";echo"</td>";
echo"<td data-title='Class'>";
echo "<input type='text' name='class' value='$class'>"; echo"</td>";
echo"<td data-title='Username'>";
echo "<input type='text' name='txt_username' value='$user'>"; echo"</td>";
echo"<td data-title='Password'>";
echo "<input type='text' name='txt_pass' value='$pass'>"; echo"</td>";
echo"<td data-title='Save'>";
echo "<input type='submit' name='saveclass' value='Save' >";echo"</td>";
echo"<td data-title='Delete'>";
echo "<a href='companies.php?d=$id'><img src='../img/delete.png'></a>";
echo"</td></form>";
}
echo"</tr>";
echo"</tbody></table>";
?>
之后,管理员可以设置用户名和密码,并使用此代码将其保存到数据库
if(isset($_POST['saveclass'])){
$id=$_POST['txt_id'];
$classe=strip_tags(mysqli_real_escape_string($conn,$_POST['class']));
$user=strip_tags(mysqli_real_escape_string($conn,$_POST['txt_username']));
$pass=strip_tags(mysqli_real_escape_string($conn,$_POST['txt_pass']));
$query=mysqli_query($conn,"select db_username from tbl_class where db_username='$user' AND db_username <>''")or die(mysqli_error($conn));
$count=mysqli_num_rows($query);echo $count;echo $id;echo $classe;
if($count==0){
$sql=mysqli_query($conn,"update tbl_class set db_class='$classe',db_username='$user',db_pass='$pass' where db_id='$id'")or die(mysqli_error($conn));
header("location:companies.php?msg=2");
}else{echo"This username exist please choose another one.";}
}
问题是他给我一条消息,信息是更新它但是当我查看数据库时看不到更新信息
答案 0 :(得分:1)
这里没有检查空格。如果您将用户名字段留空并提交表单,则每次都会提交表单。所以你的情况将是 -
if(isset($_POST['saveclass'])){
$id=$_POST['txt_id'];
$classe=strip_tags(mysqli_real_escape_string($conn,$_POST['class']));
$user=strip_tags(mysqli_real_escape_string($conn,$_POST['txt_username']));
$pass=strip_tags(mysqli_real_escape_string($conn,$_POST['txt_pass']));
$query=mysqli_query($conn,"select db_username from tbl_class where db_username='$user'")or die(mysqli_error($conn));
$count=mysqli_num_rows($query);echo $count;echo $id;echo $classe;
if($count==0){
$sql=mysqli_query($conn,"update tbl_class set db_class='$classe',db_username='$user',db_pass='$pass' where db_id='$id'")or die(mysqli_error($conn));
header("location:class.php?msg=2");
}else{echo"This username exist please choose another one.";}
}