Somebody can help me, why isnt working this code ?

<form action="" method="post">
        Firstname:<input type="text" id ="changefirstnamebox" name="firstname " maxlength="20">
        Lastname: <input type="text" id ="changefirstnamebox" name="lastname" maxlength="20">
        <center>
             <input type="submit" id ="changeusernamebutton" name="changeusernamebutton" value="Change">
        </center>
</form>

            <?php
                    $con=mysqli_connect("localhost","root","","user_registration");
                    if(isset($_POST["changeusernamebutton"])){
                        echo "working";

                    if(!empty($_POST['firstname']) && !empty ($_POST['lastname'])) {
                        $newfirstname=mysqli_real_escape_string($con,$_POST['firstname']);
                        $newlastname=mysqli_real_escape_string($con,$_POST['lastname']);
                        echo $newfirstname;echo $newlastname;
                            $sql ="UPDATE login SET firstname = '$newfirstname' WHERE  ID=$dbid" or die ("cant update " . mysqli_error($con));
                            $result = mysqli_query($con,$sql) or die ("cant update" . mysqli_error($con));
                            if($result)
                                echo"working";
                            else{
                                echo"not working";
                            }
                    }
                    }

                    ?>

A problem is this:

$sql ="UPDATE login SET firstname = '$newfirstname' WHERE  ID=$dbid" or die ("cant update " . mysqli_error($con));

Do only:

$sql = "UPDATE login SET firstname = '$newfirstname' WHERE ID=$dbid";

Otherwise mysqli_query() will return false.

I guess the code doesnt enter in this condition

 if(!empty($_POST['firstname']) && !empty ($_POST['lastname'])) {  
// here
                        $newfirstname=mysqli_real_escape_string($con,$_POST['firstname']);
                        $newlastname=mysqli_real_escape_string($con,$_POST['lastname']);
                        echo $newfirstname;echo $newlastname;
                            $sql ="UPDATE login SET firstname = '$newfirstname' WHERE  ID=$dbid" or die ("cant update " . mysqli_error($con));
                            $result = mysqli_query($con,$sql) or die ("cant update" . mysqli_error($con));
                            if($result)
                                echo"working";
                            else{
                                echo"not working";
                            }

This is my full code.

<div id="usernamechangediv" class="popupusernamewindow">
            <div id="popup">
                <h2 id="changeusername">Change Your Name </h2>
                <a class="close" href="#">&times;</a> 
                      <div id="settingwrotefirstname">
                <?php
                $con=mysqli_connect("localhost","root","","user_registration");
                if (mysqli_connect_errno())
                  {
                  echo "Failed to connect to MySQL: " . mysqli_connect_error();
                  }else{
                    if(isset($_POST["changeusernamebutton"])){
                        echo "working";echo "<br/>";
                    $chname=mysqli_real_escape_string($con,$_POST['firstname']);















               // this condition not working   


                    if(!empty($_POST['firstname']) && !empty ($_POST['lastname'])) {
                            echo "we are in ";
                            echo "<br/>";
                    }











                //  me printing this   condition     






                    else{
                        echo "fields are  blank";echo "<br/>";
                    }
















                    }
                  }
                ?>

                      <form action="" method="post">Firstname:
                    <input type="text" id ="changefirstnamebox" name="firstname " maxlength="20">
                    Lastname:
                    <input type="text" id ="changefirstnamebox" name="lastname" maxlength="20">
                    </div>
                    <center><input type="submit" id ="changeusernamebutton" name="changeusernamebutton" value="Change"></center>
                    </form>
            </div>
        </div>

The query is still a problem, it is not correct to add an or die() statement to a query string that needs to be executed.

Regarding the IF condition fix the attribute name of the firstname input field in your form, at the moment there is an extra space, so change it from this:

<input type="text" id ="changefirstnamebox" name="firstname " maxlength="20">

To:

<input type="text" id ="changefirstnamebox" name="firstname" maxlength="20">

Otherwise you have to refer it as $_POST['firstname_'], the extra space is converted by PHP to an underscore.

thanks sir. its working...

You're welcome! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.