gpt4 book ai didi

php - 更新查询在某些情况下不起作用

转载 作者:行者123 更新时间:2023-11-28 23:26:30 26 4
gpt4 key购买 nike

我有一个输入,只输入一个类名到数据库数据库 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.";}

}

问题是他给我一条消息说信息正在更新但是当我查看数据库时看不到更新信息

enter image description here

最佳答案

这里没有检查空格。如果您将用户名字段留空并提交表单,那么每次都会提交表单。所以你的情况将是 -

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.";}

}

关于php - 更新查询在某些情况下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39159282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com