-1

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

Webeng
  • 7,050
  • 4
  • 31
  • 59
Kin
  • 145
  • 1
  • 11

1 Answers1

0

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.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • i forgot to edit on my question `pdesc` is similar to `Description_for_Purchases`. still didnt work – Kin May 22 '16 at 01:12
  • @I modified my answer, I added apostrophes to the second value being inserted. What about now? – Webeng May 22 '16 at 01:13
  • ya that works. Thank you – Kin May 22 '16 at 01:16
  • @Kin When you are preforming a query, you are literally sending a string to the MySQL database. The string you were sending looked like this: `INSERT INTO table (column1, column2) VALUES (1, a)`. But MySQL needs you to have apostrophes before and after a value being inserted to recognize it as a string. So in your case, it was confused because to MySQL, `a` isn't a string, but `'a'` is. So what you were having is an error regarding the type of values being inserted. – Webeng May 22 '16 at 01:19
  • @Kin I just noticed that `CALC_STOCK_NO` was also set to be a string with `VARCHAR`... that is a bit weird that it is working without the apostrophes around `$itemid` – Webeng May 22 '16 at 01:20
  • Thanks @webeng one more thing when on the `Item id` field. Inserts `23455` but not `23455R` whay is that? the insert automatically ignore the last charactor `R` and adds 23455 – Kin May 22 '16 at 01:20
  • @Kin try changing `{$itemid}` to `'{$itemid}'`, and it should be for the exact same reason stated before. The database can't tell `23455R` is a string without the apostrophes. – Webeng May 22 '16 at 01:21
  • That also work Thanks alot! – Kin May 22 '16 at 01:29