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

标签 php mysql

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

相关文章:

php - 如何使用 PHP 和 MySQL 将父子(邻接)表转换为嵌套集?

mysql - 错误!服务器退出而不更新 PID 文件 (/usr/local/mysql/data/Mac.local.pid)

php - 从 Joomla 3.2+ 中的另一个数据库访问数据

php - 使用 composer 在自定义目录中安装包

php - 如何一次编辑数据库中的多个值

php - 从数字格式中删除逗号而不删除 PHP 中的小数点

javascript - 关于jquery ajax,php文件已运行但成功功能不起作用

php - 如何正确使用MySQLI查询和获取?

mysql - 如何在 MySQL 中将 IF 语句与 ALTER 语句结合使用?

php - htaccess 到 Nginx 重写 - Ajax/Jquery 不起作用