if field exist in database, then update price else insert all - php

i need somehow to check if field exist in db, and then if exist, just update price, if not, insert fields. I am using simplxml for parsing data to db from xml.
here is my code without if statement, just insert into two databese.
I need to check from db products if ident exist, so if not exist, do all that code down, if exist just update price in db products
foreach ($lib->product as $data) {
$manufacturer = (string) $data->manufacturer;
$ident = (string) $data->id;
$name = (string) $data->name;
$category = (string) $data->category;
$subcategory = (string) $data->subcategory;
$price = (int) ($data->price * 1.2 * 1.4 * 1.1);
$image = (string) $data->images->image[0];
$insert = $db->prepare('INSERT INTO products (ident, manufacturer,name,category,subcategory,price,image) VALUES (?, ?, ?, ?, ?, ?, ?)');
$insert->bind_param('sssssss', $ident, $manufacturer, $name, $category, $subcategory, $price, $image);
$insert->execute();
foreach($data->specifications->attribute_group as $group) {
$attribute_group = (string) $group->attributes()['name'];
foreach($group as $attr) {
$attribute = (string) $attr->attributes()['name'];
$value = (string) $attr->value;
$insert = $db->prepare('INSERT INTO spec (attr_group,attr_name, attr_value, product_id) VALUES (?, ?, ?, ?)');
$insert->bind_param('ssss', $attribute_group, $attribute, $value, $ident);
$insert->execute();
}
}
}

To do in one query, look up MySQL's ON DUPLICATE KEY UPDATE functionality for INSERT.
Then use $insert->rowCount() if you're using PDO or $insert->affected_rows for mysqli.
If the first insert tried to already insert a key that existed and updates a value, then rowCount()/affected_rows will be 2; if it just inserted a record then rowCount()/affected_rows will be 1. It will be 0 if the INSERT was unsuccessful.
e.g. for PDO:
switch($insert->rowCount()) {
case 2: // UPDATE occurred thanks to 'ON DUPLICATE UPDATE KEY'
// SOME CODE HERE IF YOU LIKE
break;
case 1: // INSERT occurred as no duplicate
// CODE TO INSERT INTO SECOND TABLE
break;
case 0:
default:
// NEITHER THE ABOVE OCCURRED SO CODE TO HANDLE ERROR
}

$query = "SELECT * name FROM yourtable";
$bool = true;
if($r = #mysql_query($query))
{
while($row = #mysql_fetch_array($r))
{
if( ($row['id'] == $id))
{
$bool = false;
break;
}
}
}
if($bool) {
UPDATE
}
else
{
INSERT
}

Related

mysql insert inside foreach loop, cron not working

This code works if i run in a browser. When i run the script via a cron, it doesn't get through the array and stops halfway? Why is this?
$url_array = array("eur-gbp","eur-aud","usd-chf","eur-usd","eur-jpy","gbp-jpy","eur-cad","eur-chf","usd-cad","usd-jpy","cad-chf","cad-jpy","gbp-usd","aud-usd","gbp-chf","chf-jpy","gbp-cad","aud-cad","aud-chf","aud-jpy","aud-nzd","eur-nzd","gbp-aud","gbp-nzd","nzd-chf","nzd-usd","nzd-cad","nzd-jpy");
$option_array = array(1,2,3,4,5,6,7);
$type_array = array(1,2,3,4,5,6);
foreach($url_array as $url_type) {
//code
foreach($option_array as $option) {
//code
foreach($duration_array as $duration) {
//code
foreach($type_array as $type) {
//mysql insert
$sql = "SELECT * FROM `data_analysis` WHERE date_time='".$date."' AND type='".$url_type."' LIMIT 1";
$query = $this->db->query($sql);
$result = $query->fetch_assoc();
if($result){
$sql = "UPDATE `data_analysis` SET value='".$percentage."', price_change='".$price."', parent='1' WHERE date_time='".$date."' AND type='".$url_type."'";
} else {
$sql = "INSERT IGNORE INTO `data_analysis` (date_time,value,price_change,type,parent) VALUES ('".$date."','".$percentage."','".$price."','".$url_type."','1')";
}
}
}
}
}
This isn't the exact code as it is too long to post but similar. The code works perfectly in the browser?? running via cron it stops at gbp-jpy? Why is this?
Is there a mysql query limit?
Add a unique index on (type, date_time) to the table. Then combine your two queries into 1. Also, use a prepared statement.
$stmt = $this->db->prepare("
INSERT INTO data_analysis (date_time, value, price_change, type, parent)
VALUES (?, ?, ?, ?, '1')
ON DUPLICATE KEY UPDATE value = VALUES(value), price_change = VALUES(price_change), parent = VALUES(parent)");
$stmt->bind_param("ssss", $date, $percentage, $price, $url_type);
foreach($url_array as $url_type) {
//code
foreach($option_array as $option) {
//code
foreach($duration_array as $duration) {
//code
foreach($type_array as $type) {
//mysql insert
$stmt->execute();
}
}
}
}

PHP prepared statement doesn't work with intval()

Please look my code:
$insertStmt = $conn->prepare("INSERT INTO orders (OrderID, OrderTrackingNumber, OrderTotal, CustomerID) VALUES (?, ?, ?, ?)");
$insertStmt->bind_param("ssdi", $orderID, strval("Not Ship Yet"), $orderTotal, $userID);
if ($insertStmt->execute()) {
$insertStmt = $conn->prepare("INSERT INTO ordersproducts (OrderID, ProductID, ProductSold) VALUES (?, ?, ?)");
$updateStmt = $conn->prepare("UPDATE products SET ProductQuantity = ? WHERE ProductID = ?");
foreach ($orderedProducts as $orderedProduct) {
$productQuantity = intval($orderedProduct->ProductQuantity) - intval($orderedProduct->ProductAddedQuantity);
$insertStmt->bind_param("sii", $orderID, intval($orderedProduct->ProductID), intval($orderedProduct->ProductAddedQuantity));
$updateStmt->bind_param("ii", intval($productQuantity), settype($orderedProduct->ProductID, "integer"));
if ($insertStmt->execute() && $updateStmt->execute()) {
if ($updateStmt->affected_rows == 1) {
$isSuccefull = TRUE;
} else {
$isSuccefull = FALSE;
break;
}
} else {
$isSuccefull = FALSE;
echo $insertStmt->error . " | " . $updateStmt->error;
break;
}
}
}
At the line of $updateStmt->bind_param, if I convert $orderedProduct->ProductID to int by intval($orderedProduct->ProductID), the updateStmt will not work ($updateStmt->affected_rows = 0). However, I use settype($orderedProduct->ProductID, "integer"); then it will work like a champ. And only this place gets that issue; others work very well.
Why?
Thanks for helping me.

Return the id (auto increment) of an inserted line

I insert data into a table called 'roster'. The first column (id_roster) is an id using mysql auto-increment.
I run a SELECT to find the id_roster
I use this id_roster to insert it into a table 'roster_par_membre' along with other data
if ($insert_stmt = $mysqli->prepare("INSERT INTO `roster`(`nom_roster`, `description_roster`, `id_organisation`, `created_by`, `creation_date`,`modified_by`) VALUES (?, ?, ?, ?, ?, ?)")) {
$insert_stmt->bind_param('ssiisi', $roster_name, $description_roster, $organisation_id, $user_id, $creation_date, $user_id);
if (!$insert_stmt->execute()) {
$reponse = 'Sorry, a database error occurred; please try later';
} else {
// if INSERT OK -> create a new line in roster_membre table
//1. get the roster_id
$sql = "SELECT r.id_roster
FROM roster r
WHERE r.nom_roster = ?
LIMIT 1";
$stmt = $mysqli->prepare($sql);
if ($stmt) {
$stmt->bind_param('s', $roster_name);
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($id_roster);
$stmt->fetch();
$level = 1;
//2. create a line with the roster_id and insert the membre as level 1
$insert_stmt = $mysqli->prepare("INSERT INTO `roster_par_membre`(`id_membre`, `id_roster`, `level`, `modified_by`) VALUES (?,?,?,?)");
$insert_stmt->bind_param('iiii', $user_id, $id_roster, $level, $user_id);
$insert_stmt->execute();
$reponse = 'success';
}
So far the code is working but it is not very nice.
Is there a way when we create a new line in a table to directly return a value (id with auto-increment) to be used in a sql query (to insert data into a second table)? or maybe to merge the two query (the two INSERT) in one statment?
short edit: it is an AJAX $response the return value (JSON)
Ok,solution:
//1. get the roster_id
$sql = "SELECT r.id_roster
FROM roster r
WHERE r.nom_roster = ?
LIMIT 1";
$stmt = $mysqli->prepare($sql);
if ($stmt) {
$stmt->bind_param('s', $roster_name);
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($id_roster);
$stmt->fetch();
Just need to replace all this part by
$id_roster = $mysqli->insert_id;
nice and easy. THANKS to albanx
these are the functions I used for query on projects that I do not want to use any framework (just php):
/**
*
* Executes query methods
* #param string $query the query string
* #param array $vals array of values
* #param bool $show show the query
* #return int/array/false
*/
function q($query, $vals=array(), $show_query=false)
{
$conn = new mysqli(...)
$offset = 0;
foreach ($vals as $v)
{
$cv = $conn->real_escape_string($v);//escape the value for avoiding sql injection
$fv = ($v===NULL) ? 'NULL':"'".$cv."'"; //if value is null then insert NULL in db
$qpos = strpos($query, '?', $offset);//replace the ? with the valeue
$query = substr($query, 0, $qpos).$fv.substr($query, $qpos+1);
$offset = $qpos+strlen($cv)+1;
}
$result = $conn->query($query);
if($show || $result===false) echo $query."<br>";
$rows = array();
if($result===true)
{
return $conn->affected_rows;
}
else if($result===false)
{
return false;
}
else
{
while ($row = $result->fetch_array(MYSQLI_ASSOC) )
{
$rows[]=$row;
}
}
return $rows;
}
function lastid()
{
return $this->qval("SELECT LAST_INSERT_ID()");
}
Usage example:
q('INSERT INTO USER(name, email) VALUES(?,?)', array('admin','admin#admin.com'));
$id = lastid();

pdo binding returns false in update

I have some issues with an update script. I bind my values but it returns false and I cannot see what I am doing wrong.
I am running this:
$row = $db->query('
UPDATE '. $config->db_prefix .'_adverts
SET ad_type = ?,
title = ?,
text = ?,
price = ?,
category = ?,
condition = ?
WHERE aid = ?')
->bind(1, $ad_type)
->bind(2, $title)
->bind(3, $text)
->bind(4, $price)
->bind(5, $category)
->bind(6, $condition)
->bind(7, $aid)->execute();
}
The bind function is this:
public function bind($pos, $value, $type = null) {
if( is_null($type) ) {
switch( true ) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($pos, $value, $type);
return $this;
}
a var_dump($this) gives me:
object(DB)#1 (2) { ["dbh":protected]=> object(PDO)#2 (0) { } ["stmt":protected]=> object(PDOStatement)#15 (1) { ["queryString"]=> string(211) " UPDATE rno_adverts SET ad_type = ?, title = ?, text = ?, price = ?, category = ?, condition = ? WHERE aid = ?" } }
but I can't see what is wrong.
EDIT:
query function is this:
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
return $this;
}
and execute is this:
public function execute($var = null) {
return $this->stmt->execute($var);
}
ERROR:
Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition = 3 WHERE aid = 1'
The output of the query:
UPDATE rno_adverts SET ad_type = 3, title = "Gul bil", text = "En flot gul bil med hvide striber", price = 500, category = 4, condition = 3 WHERE aid = 1
I have gone blind on this query so I can't see what the problem is. If I remove category and condition, it works without problems. Both fields are INT NOT NULL in the database.
You can only use the bind functions with prepared statements. ->query() is for generating queries where you already know all the values/variables of a query beforehand.
Just get rid of this bind function and be all right
$sql = 'UPDATE '. $config->db_prefix .'_adverts SET
ad_type = ?, title = ?, text = ?, price = ?, category = ?, condition = ?
WHERE aid = ?';
$db->query($sql);
$db->execute(array($ad_type, $title, $text, $price, $category, $condition, $aid));
From your question one can not say specifically where the error is located. I therefore suggest you add more error checks first, e.g. with bindvalue(), you don't check the return value:
$this->stmt->bindValue($pos, $value, $type);
Instead throw an exception if this failed:
$bind = $this->stmt->bindValue($pos, $value, $type);
if (!$bind) {
throw new Exception(sprintf("Unable to bind parameter %s with a value of type #%d", $pos, $type));
}
This will prevent you have problems binding a value but it got unnoticed.
Similar with the execution. If you want to provide information why it fails, you need to read out the error information and throw an exception.

Retrieving array values from within a class

Working on my first OOP app and I am having some trouble accessing the values of an array returned by a public function within my class. Here is the function -
//Process new transaction
public function addTransaction($amount, $payee, $category, $date, $notes) {
global $db;
//Check to see if payee exists in Payee table
$checkPayee = $db->prepare("SELECT * FROM payees WHERE name = ?");
$checkPayee->execute(array($payee));
$checkPayeeNum = $checkPayee->fetchColumn();
$payeeDetails = $checkPayee->fetch();
if ($checkPayeeNum < 1) {
$insertPayee = $db->prepare("INSERT INTO payees (name, cat) VALUES(?, ?)");
$insertPayee->execute(array($payee, $cat));
} else {
if ($payeeDetails['cat'] == "") {
$updatePayee = $db->prepare("UPDATE payees SET cat=? WHERE name=?");
$updatePayee->execute(array($cat, $payee));
}
}
//Process the transaction
$proc = $db->prepare("INSERT INTO transactions (amount, payee, cat, date, notes) VALUES (?, ?, ?, ?, ?)");
$proc->execute(array($amount, $payee, $cat, $date, $notes));
//Prepare array for JSON output
$todaysTrans = $this->fetchDailyTotal();
$weeklyTotal = $this->fetchWeeklyTotal();
$accountBalance = $this->balanceAccount();
$jsonOutput = array("dailyTotal" => $todaysTrans, "weeklyTotal" => $weeklyTotal, "accountBalance" => $accountBalance);
return $jsonOutput;
}
Instantiating the object is not the issue, trying to figure out how to access the $jsonOutput array. How would one accomplish this?
Thanks!
// In some other PHP file...
include 'YourClass.php';
$yourObject = new YourClass();
$returnedArray = $yourObject->addTransaction(...);
// Access the returned array values
echo 'Daily Total: ', $returnedArray['dailyTotal'], "\n";
echo 'Weekly Total: ', $returnedArray['weeklyTotal'], "\n";
echo 'Account Balance: ', $returnedArray['accountBalance'], "\n";
Also, for what it's worth, it's very confusing for you to be returning a PHP array called $jsonOutput, as it's not JSON encoded, which is what most developers will expect it to be. If you're wanting it to be JSON encoded, use json_encode() (see here for more info).

Categories