0

my code is not inserting any data on my php, im using a form that will display values but my code in update is not working. please help, here is my code in php :

if (isset($_POST['update'])) {
    $landowner_id = $_POST['landowner_id'];
    $firstname = $_POST['firstname'];
    $middlename = $_POST['middlename'];
    $lastname = $_POST['lastname'];
    $municipality = $_POST['municipality'];
    $barangay = $_POST['barnagay'];
    $areacovered = $_POST['areacovered'];
    $sex = $_POST['sex'];

    mysqli_query($db, "UPDATE info SET firstname='$firstname', middlename='$middlename', lastnamename='$lastname', municipality='$municipality', barangay='$barangay', areacovered='$areacovered', sex='$sex' WHERE landowner_id=$landowner_id");
    $_SESSION['message'] = "Address updated!"; 

}

here is my html

<div class="form-wrapper">
                    <input type="number" id = "check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $landowner_id;?>"  required>
                </div>
                <div class="form-wrapper">
                    <input type="text" id = "check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $firstname;?>"  required>
                </div>
                <div class="form-wrapper">
                    <input type="text"  name="middlename" placeholder="Middle Name" class="input-field"  value="<?php echo $middlename;?>">
                </div>
                <div class="form-wrapper">
                    <input type="text"   name="lastname" placeholder="Last Name" class="input-field"  value="<?php echo $lastname;?>" required>
                </div>
                <div class="form-wrapper">
                    <input type="text"  name="municipality" placeholder="Municipality" class="input-field"   value="<?php echo $municipality;?>" required>
                </div>
                <div class="form-wrapper">
                    <input type="text"  name="barangay" placeholder="Barangay" class="input-field" value="<?php echo $barangay;?>" >
                </div>
                <div class="form-wrapper">
                    <input type="text" id = "check" name="areacovered" placeholder="Area Covered" class="input-field" value="<?php echo $areacovered;?>" required>
                </div>
                <div class="form-wrapper">
                    <input type="text" id = "check" name="sex" placeholder="Sex" class="input-field" value="<?php echo $sex;?>" required>
                    <br>
                    <button class="btn" type="submit" name="update"  >Update</button>
                </div>
1
  • The very first thing you would do is add in a debug statement like var_dump($_POST), which you can see if $_POST['update'] is working. So the question you would be asking is "Is the code getting in there..." Commented Apr 18, 2020 at 17:50

1 Answer 1

2

I don't see any "form" tag. Are you missing to wrap your "form-wrapper" into a tag? Something like this:

<form action="" method="post">
    <div class="form-wrapper">
        <input type="number" id="check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $landowner_id; ?>" required>
    </div>

    <!-- Other inputs -->

    <div class="form-wrapper">
        <input type="text" id="check" name="sex" placeholder="Sex" class="input-field" value="<?php echo $sex; ?>" required>
        <br>
        <button class="btn" type="submit" name="update">Update</button>
    </div>
</form>

Other important things to consider:

  • Never EVER send to the DB plain inputs coming from the outside without cleaning them! Otherwise, you will be open to SQL injection. Use prepare-statements to solve this issue.

  • Instead of mysqli_query I recommend you to use PDO. You can prepare statement super easy. Here you can see an example of usage: https://stackoverflow.com/a/60988740/3454593

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.