I have this script that read parsed xml (external):
<?php
//mysql connection
$con2 = mysql_connect("localhost","username","password");
if (!$con2)
{
die('Could not connect: ' . mysql_error());
}
$selectdb = mysql_select_db("test_db", $con2);
if (!$selectdb)
{
die('Database not used: ; ' . mysql_error());
}
//simplexml load xml file
$jackpots = simplexml_load_file('https://www.123.com/xmldata.xml');
//loop through parsed xmlfeed and print output
foreach ($jackpots->jackpot as $jackpot) {
foreach ($jackpots->jackpot as $jackpot) {
printf("gameId: %s\n", $jackpot->gameId);
printf("gameName: %s\n", $jackpot->gameName);
printf("amount: %s\n", $jackpot->amount);
printf("currency: %s\n", $jackpot->currency);
//insert into databse
foreach ($jackpots->gameId as $jackpot) {
mysql_query("UPDATE my_table SET amount=$jackpot->amount")
or die(mysql_error());}
//show updated records
printf ("Records inserted: %d\n", mysql_affected_rows());
}
}
//close connection
mysql_close($con2);
?>
Obviously, I have a table (my_table) in database that contains all colums (gameId, gameName, amount and currency).
I need to update the “amount” column for every “gameId” column.
When I run the script below, I got an update but all the “amounts” are the same.
Can I have some support, please?
Where is my error?
Thanks in advance!
You have to include the gameId in your query!
Like this:
Be careful, though, when you insert variables hard-coded into the query like this.
You should escape the variables before to avoid SQL injection vulnerability.
It would probably be good to look into PHP frameworks like Code Igniter, Zend or Laravel to get you some basic functionality setup in these tasks.