0

I've made this code; but all i get are empty fields in the database. The connection with the db is working. I can print the XML file on my screen : object(SimpleXMLElement)#2 (3) { ["item"]=> string(4) "Tove" ["product_category_code"]=> string(4) "Jani" ["SKU"]=> string(8) "Reminder" } ToveJaniReminderwaarden

I don't get a message 'recoreds inserted' or 'no records inserted'. But the moment i put it in my query to insert it in the database; the fields stay empty. Why?

check the connection with the database echo the variables use AI (says the code is ok) insert with a textfield (gets inserted)

//check if xml exists -- good. 
  if (file_exists('note.xml')) {
      
 // If XML file exists then load the XML file --  good 
$xml_file = simplexml_load_file('note.xml');
 
// Display the content of XML file -- gets displayed 
     var_dump($xml_file);   
   } 
   else {
     exit('Fail to open the file');}

  $xml = simplexml_load_file("note.xml")
  or die("Error: Cannot create object");


// Assign values --> ?? 
foreach ($xml->children() as $row) {
  $item = $row->item;
  $product_category_code = $row->product_category_code;
  $SKU = $row->SKU;  


  //echo -> gets echo'ed
  echo $xml->item; 
  echo $xml->product_category_code;
  echo $xml->SKU; 

  //check again and print -- .yep.  
  echo ($item . $product_category_code . $SKU . "waarden"); 

  // SQL query to insert data into xml table
  $sql = $con->prepare("INSERT INTO Producten (product_category_code, SKU, item) VALUES (product_category_code, item , SKU)");
  $sql->bind_param("sss", $product_category_code, $SKU, $item);
  $sql->execute(); 

  if ($sql->affected_rows > 0) {
    $affectedRow ++;
  } else {
    $error_message = $con->error . "
";
  }
}

if ($affectedRow > 0) {
  $message = $affectedRow . " records inserted";
} else {
  $message = "No records inserted";
}
Erik
  • 1
  • 3
  • 1
    Change VALUES (product_category_code, item , SKU) to VALUES (?, ?, ?) – Jason K Apr 24 '23 at 13:34
  • https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement – ADyson Apr 24 '23 at 13:35
  • I think you haven't configured mysqli to report errors. Add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` somewhere on top of your code. – Álvaro González Apr 24 '23 at 13:37
  • Thanks; I already tried the (Values ?, ?, ?); Also did the myssqli_report.... Sorry i didn't mention it. – Erik Apr 24 '23 at 13:45
  • If you don't have error logging turned on in the script. Check the web server error log. – Jason K Apr 24 '23 at 13:50
  • done. No errors :-( – Erik Apr 24 '23 at 14:16
  • Change $error_message = $con->error to $error_message = $sql->error – Jason K Apr 24 '23 at 15:27
  • I got my data in the db! it worked after i changed this bit: $stmt->bind_param("sss", $xml->product_category_code, $xml->SKU, $xml->item); don't know why. but happy to get this far... – Erik Apr 25 '23 at 13:19

0 Answers0