php - mysql insert variables from foreach results -
cant insert results generated foreach loop mysql database. im provided xml file contains few of details me generate next generation of url's access detailed product xml files. im still in begging. here code:
<?php $xml=simplexml_load_file("http://www.website.com/categories/xml/list.xml") or die("error: cannot create object"); foreach ($xml->product $product) { $sql = "insert software (groupid,codeid) values ('$product->attributes()->groupid,$product->attributes()->codeid')"; if ($conn->query($sql) === true) { echo "info added successfully"; } else { echo "error inserting table: " . $conn->error; } } $conn->close(); ?>
here error message :
error creating table: column count doesn't match value count @ row 1
error creating table: column count doesn't match value count @ row 1
error creating table: column count doesn't match value count @ row 1
im not sure if syntax wrong or have somehow generate specific value each result.
thanks in advance answers.
you have 1 large string, should have two. try:
$sql = "insert software (groupid,codeid) values ('$product->attributes()->groupid','$product->attributes()->codeid')";
a better approach parameterized.
$sql = "insert software (groupid,codeid) values (?, ?)";
much easier read, , safer. need bind variables $product->attributes()->groupid
, $product->attributes()->codeid
.
here's rough example, (the i
s integers, if strings change s
s. 1 letter per variable):
$stmt = $conn->prepare($sql) $stmt->bind_param('ii', $product->attributes()->groupid, $product->attributes()->codeid); $stmt->execute();
you can read more here, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php.
Comments
Post a Comment