mohamad mohamad
mohamad mohamad

Reputation: 631

Update query not working in some cases

i have an input to enter a class name to database only database tbl_class

db_id  db_class    db_username db_pass
1      test
2     second test

a second form read the class info from database like this

<?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>"; 
?>

after that the admin can set a username and password and save that to database using this code

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

    }

the problem is he give me a message that information is update it but when i look to database a don't see the update info

enter image description here

Upvotes: 1

Views: 63

Answers (1)

Hemant Jaiswal
Hemant Jaiswal

Reputation: 89

Here there is no check for blank space. If you left username field to be blank and submit form so every time form will be submitted. So your condition will be -

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

}

Upvotes: 1

Related Questions