0

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

                    ?>
2
Contributors
7
Replies
10
Views
2 Years
Discussion Span
Last Post by cereal
0

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.

0

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

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>

Edited by phoenix254

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.