2

I am currently doing a project in adding values using database but I seem to have a problem. I am sure that my query is correct since I tried adding it manually in mysql. Only some of the fields seem to be able to get what I input. I get the error

"Error: INSERT INTO inventory (itemCode, dateReceived, typeOfFabric, details, unitOfMeasurement, amount, assignedOrderUse, section, row) VALUES ('', '', '', 'White', '', '5', '', 'C', 'C')"

    <?php

$host = "localhost";
$user = "root";
$pass = "";
$db = "gracydb";

if (isset($_POST['addInventory']))
{
    if(isset($_POST['itemCode'])){ $itemcode = $_POST['itemCode']; } 
    if(isset($_POST['dateReceived'])){ $inventoryDateReceived = $_POST['dateReceived']; } 
    if(isset($_POST['typeOfFabric'])){ $fabric = $_POST['typeOfFabric']; }
    if(isset($_POST['details'])){ $details = $_POST['details']; } 
    if(isset($_POST['unitOfMeasurement'])){ $measurement = $_POST['unitOfMeasurement']; }
    if(isset($_POST['amount'])){ $amount = $_POST['amount']; } 
    if(isset($_POST['assignedOrderUse'])){ $order = $_POST['assignedOrderUse']; } 
    if(isset($_POST['section'])){ $section = $_POST['section']; }
    if(isset($_POST['row'])){ $row = $_POST['row']; }

    $conn = mysql_connect($host, $user, $pass);
    $db_selected = mysql_select_db($db, $conn);

    $sql = "INSERT INTO inventory (itemCode, dateReceived, typeOfFabric, details, unitOfMeasurement, amount, assignedOrderUse, section, row)
    VALUES ('$itemcode', '$datereceived', '$fabric', '$details', '$measurement', '$amount', '$order', '$section', '$row')";

    if (mysql_query($sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysql_error($conn);
    }

    mysql_close($conn);
    //header ('Location: .php');
}

?>

<form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method = "POST">
Item Code: <input type = "text" name = "itemcode"><br>
Date Received: <input type = "date" name = "inventoryDateReceived"><br>
Type of Fabric: <input type = "text" name = "fabric"><br>
Unit of Measurement: 
<select name = "measurement">
<option value = "Grams">Grams</option>
<option value = "Kilograms">Kilograms</option>
</select><br>
Amount: <input type = "number" name = "amount"><br>
Assigned Order/Use: <input type = "text" name = "order"><br>
Section: <input type = "text" name = "section"><br>
Row: <input type = "text" name = "row"><br>
<input type = "submit" value = "submit" name = "addInventory">
</form>
3
  • set allow null in table Commented Feb 22, 2016 at 7:34
  • Check that you can insert into itemCode, dateReceived, typeOfFabric, unitOfMeasurement, assignedOrderUse empty values Commented Feb 22, 2016 at 7:35
  • So what is the actual error? You show us the SQL, but not the error itself. Commented Feb 22, 2016 at 7:56

2 Answers 2

1

These indexes not matched with your input form names:

$_POST['itemCode']
$_POST['dateReceived']
$_POST['typeOfFabric']

These should be:

$_POST['itemcode']
$_POST['inventoryDateReceived']
$_POST['fabric']

Check your form inputs:

<input type = "text" name = "itemcode">
<input type = "date" name = "inventoryDateReceived">
<input type = "text" name = "fabric">
Sign up to request clarification or add additional context in comments.

Comments

1

I don't see any sense in this part of the code:

if(isset($_POST['itemCode'])){ $itemcode = $_POST['itemCode']; } 
if(isset($_POST['dateReceived'])){ $inventoryDateReceived = $_POST['dateReceived']; } 
if(isset($_POST['typeOfFabric'])){ $fabric = $_POST['typeOfFabric']; }
if(isset($_POST['details'])){ $details = $_POST['details']; } 
if(isset($_POST['unitOfMeasurement'])){ $measurement = $_POST['unitOfMeasurement']; }
if(isset($_POST['amount'])){ $amount = $_POST['amount']; } 
if(isset($_POST['assignedOrderUse'])){ $order = $_POST['assignedOrderUse']; } 
if(isset($_POST['section'])){ $section = $_POST['section']; }
if(isset($_POST['row'])){ $row = $_POST['row']; }

Your are just setting values (if isset) to new variables - but if they not exists you will still use undefined variables. Also there is no escaping to prevent sql-injections and validation of the given values!

I think you will get this error because of a missing variable.

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.