Kin Kin - 6 months ago 10
PHP Question

Mysql insert doent work when adding text

I have the following INSERT Statement but it doesn't insert a record in the table. It works when i add only the CALC_STOCK_NO field but not when i add Description field to the insert statement.

Here sample Description value:

DSSY68678/787-15.5 14 328 I3 TL 8-8-6.01 ABC


$itemid = $data2['fields']['CALC STOCK NO'];
$pdesc = $data2['fields']['Item Description'];

mysqli_query($con,
"INSERT INTO 600XXX
(CALC_STOCK_NO, pdesc) VALUES
($itemid, $pdesc)"
);


here is what my table looks like:

enter image description here

Answer

You are not writing the correct field name for the description field. You placed pdesc instead of Description_for_Purchases in your sql statement. You also need to have apostrophes before and after your string values, which is the case for your second field. To correct these problems:

change this code:

mysqli_query($con,
    "INSERT INTO  600XXX  
    (CALC_STOCK_NO, pdesc) VALUES
    ($itemid, $pdesc)"
);

to this code:

mysqli_query($con,
    "INSERT INTO  600XXX  
    (CALC_STOCK_NO, Description_for_Purchases) VALUES
    ({$itemid}, '{$pdesc}')"
);

I am used to adding {} as well when I insert values into strings directly. You don't need to do it to work in this case, though since there are situations in which it is needed, I like to remain consistent and do it everywhere :). Let me know if that worked for you.