Joshua
Joshua

Reputation: 1

Php not inserting data

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>

Upvotes: 0

Views: 38

Answers (1)

Chemaclass
Chemaclass

Reputation: 2011

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

Upvotes: 2

Related Questions