Update mysql from array xml

456
May 01, 2017, at 03:50 AM

been struggling with this, I am trying to update with 0 the rows that is not in the array I get from the xml.

$sus = array();
foreach( $xml->property as $node ) {
$sus[] = $node->suid;
}
$A = "'".implode("','",$sus)."'";
echo $A;
$sth = $dbh->prepare("UPDATE tabla SET
alta = 0
WHERE suid NOT IN ($A)");
$sth->execute($sus);

When I echo $A it prints it out correctly like this: '60','62','65','73','74','79','83','90','112','124' However it does not do the update, whats wrong? Thanks in advance

Answer 1

You should start by escaping your XML values to avoid SQL injection:

$escapedValues = str_repeat('?,', count($sus) - 1) . '?';
$sth = $db->prepare("UPDATE tabla SET alta = 0 WHERE suid NOT IN ($escapedValues)"
$sth->execute($sus);
Rent Charter Buses Company
READ ALSO
JCombo Box with mySQL Database

JCombo Box with mySQL Database

I have a JCombo Box with 2 values (Fanta, Cola) and a JTextfieldIf i write a number in the JTextfield and choose for example Fanta it should add the value in the MYSQL database in the colummn Fanta, if i choose Cola it should add it in Cola

283
Uploading and displaying images through API

Uploading and displaying images through API

On my site I use uploadsim (http://uploads

329
Get id of latest inserted row in JDBC with preparedstatemtnt

Get id of latest inserted row in JDBC with preparedstatemtnt

I have followed DAO pattern for my project and made separate file for handeling DB connectionI have found answer in other posts/questions but they had used Statemtnt and I have used PreparedStatemtnt

375