0

Hello guys i want to insert data into my database table upon form submission but the following errors are appearing

Warning: Undefined array key "id" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 3
Warning: Undefined array key "week" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 4
Warning: Undefined array key "description" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 5
Warning: Undefined array key "quantity" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 6
Warning: Undefined array key "unit_price" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 7
Warning: Undefined array key "requested_by" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 8
Warning: Undefined array key "receieved_from" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 9
Warning: Undefined array key "ordered_from" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 10 Warning: Undefined array key "order_number" in C:\Users\Lawson\Downloads\HTML and CSS\insert.php on line 11

Error executing query Php code

<?php
include("connect.php")
$id = $_POST['id'];
$week = $_POST['week'];
$description = $_POST['description'];
$quantity = $_POST['quantity'];
$unit_price = $_POST['unit_price'];
$requested_by = $_POST['requested_by'];
$receieved = $_POST['receieved_from'];
$ordered_from = $_POST['ordered_from'];
$order_number = $_POST['order_number'];
$sql2 = ("INSERT INTO Goods_Receieved
            (idGoods_Received,Week,Description,
            Quantity,Unit_Price,Requested_By,Date,
            Receieved_From,Ordered_From,Order_Number) 
        VALUES ('$id', '$week', '$description', 
            '$quantity','$unit_price','$requested_by','$receieved',
            '$ordered_from','$order_number')");
$connANDsql = sqlsrv_query($conn,$sql2);
if($connANDsql == false){
    echo"Error executing query";
}else{
    echo "Data Succesfully saved";
}
sqlsrv_close($conn);
?>

Html code

<h1>Goods Received Form</h1> <br>
        
<form id="goodsreceived_form" method="Post" action="insert.php">
    
    <label for="ID" class="grlabel"><b>ID</b></label>
    <input id="id" type="number" name="ID"  required><br>

    <label for ="week" class="grlabel"><b>Week</b></label>
    <input id="week" type="number" name="uname" required><br>

    <label for="description" class="grlabel"><b>Description</b></label>
    <input id="description"type="text" name="description" required><br>

    <label for="quantity" class="grlabel"><b>Quantity</b></label>
    <input id="quantity"type="number" name="quantity" required> <br>

    <label for="unit_price" class="grlabel"><b>Unit Price</b></label>
    <input id="unit_price" type="text" name="unit_price" required><br>

    <label for ="requested_by" class="grlabel"><b>Requested By</b></label>
    <input id="requested_by"type="text" name="requested_by" required><br>

    <label for="date" class="grlabel"><b>Date</b></label>
    <input id="date" type="date" name="date" required><br>

    <label for="received_from" class="grlabel"><b>Received From</b></label>
    <input id="receieved_from" type="text" name="received_from" required><br>

    <label for="order_number" class="grlabel"><b>Order Number</b></label>
        <input id="order_number" type="text" name="order_number" required><br>

    <label for="ordered_from" class="grlabel"><b>Ordered From</b></label>
            
    <button id="submit" type="submit">Save</button>
        </div>
    </div> 
</form>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Lawson
  • 1
  • You have `id="week"` but `name="uname"` – 001 Jan 30 '23 at 13:52
  • And `name="ID"` and `$id = $_POST['id'];` Case ? – RiggsFolly Jan 30 '23 at 13:54
  • Your script is open to [SQL Injection Attack](http://stackoverflow.com/questions/60174). Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187) You should always use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's instead of concatenating user provided values into the query. Never trust ANY user input! This will also remove the unescaped character issue like a `'` in a text string. – RiggsFolly Jan 30 '23 at 13:55
  • Good code indentation and layoyt would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jan 30 '23 at 13:55

0 Answers0