Please be gentle, I am not a PHP developer and do not know the language very well.
I have the following SQL PHP statement:
$SerializedPricingArray = serialize($PricingArray);
$signup_order = $i + 1;
$SQL_Statement = "SELECT IF (EXISTS (SELECT [p].[planname] FROM [package] [p] WHERE [p].[planid] = 10 AND [p].[planname] = '" . $json_post['pricestructure']['tld'][$i]['tld'] . "'), \n"
. "(UPDATE [package] SET [pricing] = '" . $SerializedPricingArray . "', [signup_order] = " . $signup_order . "), \n"
. "(INSERT INTO [package] ([planname], [description], [asset_html], [highlight], [planid], [showpackage], [pricing], [style], [automaticactivation], [allowdirectlink], "
. "[sendwelcome], [stockInfo], [emailTemplate], [bundledProducts], [advanced], [signup_order]) VALUES ('" . $json_post['pricestructure']['tld'][$i]['tld'] . "', '', '', "
. "0, 10, 1, '". $SerializedPricingArray . "', 0, 1, 0, 1, 'a:5:{s:12:\"stockEnabled\";i:0;s:14:\"availableStock\";i:0;s:7:\"soldOut\";i:0;s:13:\"acceptSoldOut\";i:0;s:14:\"showStockLevel\";i:0;}', "
. "35, 'a:3:{s:8:\"optional\";a:0:{}s:8:\"required\";a:0:{}s:7:\"coupons\";a:0:{}}', 'a:3:{s:19:\"autoInitiateTranfer\";s:1:\"1\";s:17:\"enableNamesuggest\";s:1:\"1\";s:14:\"maxNamesuggest\";s:1:\"5\";}', "
. $signup_order . ")));\n";
echo "Working on TLD " . $json_post['pricestructure']['tld'][$i]['tld'] . "\n";
$_NULL = mysqli_query($conn, $SQL_Statement);
And when I run the code, it goes through its loop, but does nothing to the database.
What am I doing wrong?
EDIT 1
Here is the query the above code generates:
SELECT IF (EXISTS (SELECT p.`planname` FROM `package` p WHERE p.`planid` = 10 AND p.`planname` = 'com'),
UPDATE `package` SET
`pricing`='a:2:{s:7:"taxable";i:0;s:9:"pricedata";a:1:{i:0;a:5:{s:9:"registrar";s:4:"enom";i:1;a:5:{s:6:"period";s:1:"1";s:9:"period_id";s:1:"1";s:5:"price";s:5:"11.99";s:8:"transfer";s:5:"11.99";s:5:"renew";s:5:"11.99";}i:2;a:5:{s:6:"period";s:1:"2";s:9:"period_id";s:1:"2";s:5:"price";s:5:"23.98";s:8:"transfer";s:5:"23.98";s:5:"renew";s:5:"23.98";}i:5;a:5:{s:6:"period";s:1:"5";s:9:"period_id";s:1:"5";s:5:"price";s:5:"59.95";s:8:"transfer";s:5:"59.95";s:5:"renew";s:5:"59.95";}i:10;a:5:{s:6:"period";s:2:"10";s:9:"period_id";s:2:"10";s:5:"price";s:6:"119.90";s:8:"transfer";s:6:"119.90";s:5:"renew";s:6:"119.90";}}}}',
`signup_order`=1
WHERE `planid`=10 AND `planname`='COM',
INSERT INTO `package` (`planname`, `description`, `asset_html`, `highlight`, `planid`, `showpackage`, `pricing`, `style`, `automaticactivation`, `allowdirectlink`, `sendwelcome`, `stockInfo`, `emailTemplate`, `bundledProducts`, `advanced`, `signup_order`)
VALUES (
'com',
'',
'',
0,
10,
1,
'a:2:{s:7:"taxable";i:0;s:9:"pricedata";a:1:{i:0;a:5:{s:9:"registrar";s:4:"enom";i:1;a:5:{s:6:"period";s:1:"1";s:9:"period_id";s:1:"1";s:5:"price";s:5:"11.99";s:8:"transfer";s:5:"11.99";s:5:"renew";s:5:"11.99";}i:2;a:5:{s:6:"period";s:1:"2";s:9:"period_id";s:1:"2";s:5:"price";s:5:"23.98";s:8:"transfer";s:5:"23.98";s:5:"renew";s:5:"23.98";}i:5;a:5:{s:6:"period";s:1:"5";s:9:"period_id";s:1:"5";s:5:"price";s:5:"59.95";s:8:"transfer";s:5:"59.95";s:5:"renew";s:5:"59.95";}i:10;a:5:{s:6:"period";s:2:"10";s:9:"period_id";s:2:"10";s:5:"price";s:6:"119.90";s:8:"transfer";s:6:"119.90";s:5:"renew";s:6:"119.90";}}}}',
0,
1,
0,
1,
'a:5:{s:12:"stockEnabled";i:0;s:14:"availableStock";i:0;s:7:"soldOut";i:0;s:13:"acceptSoldOut";i:0;s:14:"showStockLevel";i:0;}',
35,
'a:3:{s:8:"optional";a:0:{}s:8:"required";a:0:{}s:7:"coupons";a:0:{}}', 'a:3:{s:19:"autoInitiateTranfer";s:1:"1";s:17:"enableNamesuggest";s:1:"1";s:14:"maxNamesuggest";s:1:"5";}',
1
)
);
EDIT 2
I've changed the code to the following:
$StockInfo = "a:5:{s:12:\"stockEnabled\";i:0;s:14:\"availableStock\";i:0;s:7:\"soldOut\";i:0;s:13:\"acceptSoldOut\";i:0;s:14:\"showStockLevel\";i:0;}";
$BundledProducts = "a:3:{s:8:\"optional\";a:0:{}s:8:\"required\";a:0:{}s:7:\"coupons\";a:0:{}}', 'a:3:{s:19:\"autoInitiateTranfer\";s:1:\"1\";s:17:\"enableNamesuggest\";s:1:\"1\";s:14:\"maxNamesuggest\";s:1:\"5\";}";
$SerializedPricingArray = serialize($PricingArray);
$signup_order = $i + 1;
$SQL_Statement = "INSERT INTO package (`planname`, `description`, `asset_html`, `highlight`, `planid`, `showpackage`, `pricing`, `style`, `automaticactivation`, `allowdirectlink`, `sendwelcome`, `stockInfo`, `emailTemplate`, `bundledProducts`, `advanced`, `signup_order`) VALUES ('" . $json_post['pricestructure']['tld'][$i]['tld'] . "', '', '', 0, 10, 1, '". $SerializedPricingArray . "', 0, 1, 0, 1, '" . $StockInfo . "', 35, '" . $BundledProducts . "', " . $signup_order . ") "
. "ON DUPLICATE KEY UPDATE `package` SET `pricing` = VALUES(`pricing`), `signup_order` = VALUES(`signup_order`) WHERE `planid` = 10 AND `planname` = '" . $json_post['pricestructure']['tld'][$i]['tld'] . "';";
$result = mysql_query($conn, $SQL_Statement);
echo "Working on TLD " . $json_post['pricestructure']['tld'][$i]['tld'] . "\n";
echo $result . "\n\n";
When the PHP page is executed, it "appears" to go through all 458 records, however when I check the table on MySQL, there are only the original 58 records.
Any other suggestions?
Addition 1
Here is the MySQL table structure:
+---------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| planname | varchar(45) | NO | | NULL | |
| description | text | NO | | NULL | |
| asset_html | text | NO | | NULL | |
| highlight | text | NO | | NULL | |
| planid | int(11) | NO | | 0 | |
| showpackage | smallint(6) | NO | | 1 | |
| pricing | text | NO | | NULL | |
| style | tinyint(4) | YES | | 0 | |
| automaticactivation | tinyint(4) | YES | | 1 | |
| allowdirectlink | tinyint(4) | NO | | 1 | |
| sendwelcome | tinyint(4) | NO | | 1 | |
| stockInfo | text | NO | | NULL | |
| emailTemplate | smallint(5) | NO | | 0 | |
| bundledProducts | text | NO | | NULL | |
| advanced | text | NO | | NULL | |
| signup_order | int(11) | NO | | 1 | |
+---------------------+-------------+------+-----+---------+----------------+
I think your query has some error. []
used in SQL SERVER and for mysql must use ``
For check detail error use this
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform a query, check for error
if (!mysqli_query($conn, $SQL_Statement);){
echo("Error description: " . mysqli_error($con));
}
Your query is not correct first read if
statement sql This
I FINALLY figured out a solution. It is as follows:
$StockInfo = "a:5:{s:12:\"stockEnabled\";i:0;s:14:\"availableStock\";i:0;s:7:\"soldOut\";i:0;s:13:\"acceptSoldOut\";i:0;s:14:\"showStockLevel\";i:0;}";
$BundledProducts = "a:3:{s:8:\"optional\";a:0:{}s:8:\"required\";a:0:{}s:7:\"coupons\";a:0:{}}', 'a:3:{s:19:\"autoInitiateTranfer\";s:1:\"1\";s:17:\"enableNamesuggest\";s:1:\"1\";s:14:\"maxNamesuggest\";s:1:\"5\";}";
$SerializedPricingArray = serialize($PricingArray);
$signup_order = $i + 1;
$SQL_SELECT_Statement = "SELECT * FROM package WHERE planname='" . $json_post['pricestructure']['tld'][$i]['tld'] . "';";
$SQL_UPDATE_Statement = "UPDATE package SET pricing='" . $SerializedPricingArray . "', signup_order=" . $signup_order . " WHERE planid=10 AND planname='" . $json_post['pricestructure']['tld'][$i]['tld'] . "';";
$SQL_INSERT_Statement = "INSERT INTO package (`planname`, `description`, `asset_html`, `highlight`, `planid`, `showpackage`, `pricing`, `style`, `automaticactivation`, `allowdirectlink`, `sendwelcome`, `stockInfo`, `emailTemplate`, `bundledProducts`, `advanced`, `signup_order`) VALUES ('" . $json_post['pricestructure']['tld'][$i]['tld'] . "', '', '', '0', 10, 1, '". $SerializedPricingArray . "', 0, 1, 0, 1, '" . $StockInfo . "', 35, '" . $BundledProducts . "', " . $signup_order . ")";
echo "Working on TLD " . $json_post['pricestructure']['tld'][$i]['tld'] . "\n";
$result = mysqli_query($conn, $SQL_SELECT_Statement);
if (mysqli_num_rows($result) > 0 ) {
echo "Updating " . $json_post['pricestructure']['tld'][$i]['tld'] . " . . . .\n\n";
$result2 = mysqli_query($conn, $SQL_UPDATE_Statement);
} else {
echo "Inserting " . $json_post['pricestructure']['tld'][$i]['tld'] . " . . . .\n\n";
$result2 = mysqli_query($conn, $SQL_INSERT_Statement);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.