0

I'm trying to insert specific value to a MySQL table but whenever I do so, an error occurred and cant be inserted. As I traced it I noticed that the queries can only be inserted if all entities are given or all entities will be declared. How can I resolve this using PHP code?

if (isset($_POST['submit'])) {
    $rec_qty=$_POST['rec_qty'];
    $id=$_POST['item_id'];

    for($i=0; $i < count($_POST['rec_qty']);$i++) {
        $sql = mysqli_query($conn,"INSERT INTO itemlist (quantity) values('".$_POST['rec_qty'][$i]."') ") or die ("Error description: " . mysqli_error($conn)); 

        $update = "$conn,UPDATE itemlist set quantity='$quantity' where item_id='$id'";

        if($sql) {
            header("Location: received_item.php?attempt=success");
        } else {        
            header("Location: received_item.php?attempt=empty");
        }
    }

/**Error description: Field 'item_name' doesn't have a default value**/
zajonc
  • 1,935
  • 5
  • 20
  • 25

1 Answers1

2

The item_name field in your table is declared as not null without a default value. One approach would be to supply it in your insert statement in the same way you provide the quantity.

Another approach would be to change the table's definition to either allow nulls:

ALTER TABLE itemlist MODIFY COLUMN item_name VARCHAR(10) NULL;

or to have a default value:

ALTER TABLE itemlist ALTER item_name SET DEFAULT 'somename';
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • indeed it was declared as not null because there is a relationship between 2 tables. im trying to pass same field but only quantity will be alter.. for example im ordering 500doz and I only receive 450 so 450 will be the new value for my quantity. – MACKIE MACKO May 23 '16 at 06:27