How to loop multiple variable from first row of table PHP - php

I based on the sales person to loop and insert the data with 3 rows into database 2. But I cannot insert the category with 2 times with 2 sales person based on the first row, it keep get the last row of category data and insert to database. Like below image of 1.
$product_type = $request->post('product_type');
$emp_amount = $request->post('amount');
foreach($_POST['employee'] as $index => $employee_list){
$statement = $this->db->prepare('
INSERT INTO `selling_employee` (
invoice_no, payment_id, category, sale_person, amount, created_at
) VALUES (?, ?, ?, ?, ?, ?)
');
$statement->execute([
$invoice_id, $payment_id, $product_type[$index], $employee_list, $emp_amount[$index], $created_at
]);
}
Any solution how to detect the first row got more than sales person and insert multiple same data based on first row.

Related

Laravel - Find out if the row was inserted or updated

I need to get either a 1 (the record was inserted) or a 2 (the record was updated). How can this be done using Laravel? I can't find any information about this in their documentation.
DB::insert('insert into ratings
(owner_id, game_id, rating) values (?, ?, ?)
on duplicate key update rating = values(rating)',
[
(int)$body['user'], (int)$gameId, (int)$body['amount']
]);
I know I can get pdo like this:
$pdo = DB::connection()->getPdo();
but I don't think that helps...
You will want to use the function affectingStatement which seems to return the number of affected rows...
$res = DB::affectingStatement('insert into ratings
(owner_id, game_id, rating) values (?, ?, ?)
on duplicate key update rating = values(rating)', [
(int)$body['user'],
(int)$gameId,
(int)$body['amount']
]);
For me, this was returning 2 when a row updates, 1 when a row is inserted, and 0 on failure as expected.

Entering Values To DB With Facebook API ID Is Same For All Entries

When entering each tagged_places from the Facebook API the id and the place_id are entered as the same random number.
When I print the object to the screen before entry both the id and the place_id are completely different numbers than what is entered in to the DB.
These numbers should be unique from each other, but every entry in the DB from my FB user account has the same id and place_id matching each other.
Does anyone have knowledge of this issue I am having? I need the id to be unique so I can use it to identify repeat inserts into my DB.
EDIT I notice that the number that is entered in my database for every new entry is the same number which has 10 digits. I am thinking this could be a phone number... EDIT
This is an example of an ID from the output of the variable,
[id] => 10202525749041541
And this is what shows up in the DB 2147483647
Here is my insertion code,
<?php
$stmt = $con->prepare('
INSERT INTO taggedPlaces
(id, created_time, place_id, city, country, latitude, longitude, state, street, zip, name)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
');
foreach($graphObject['tagged_places']->data as $data) {
if (time() - strtotime($data->created_time) < 86400) {
$stmt->execute(array(
$data->id,
$data->created_time,
$data->place->id,
$data->place->location->city,
$data->place->location->country,
$data->place->location->latitude,
$data->place->location->longitude,
$data->place->location->state,
$data->place->location->street,
$data->place->location->zip,
$data->place->name
));
}
}
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
?>
My problem was the Mysql field is set to int for id and place_id
int max value is 2147483647
And I was trying to enter a value of 10202525749041541
Solution for this problem was change the Mysql field from INT to BIGINT

Comparing A Time Value Against Database To Stop Repeat Entries

I am inserting values into a database.
If the time the values were created is less than 24 hours then I insert like this,
$stmt = $con->prepare('
INSERT INTO taggedPlaces
(id, created_time, place_id, city, country, latitude, longitude, state, street, zip, name)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
');
foreach($graphObject['tagged_places']->data as $data) {
if (time() - strtotime($data->created_time) < 86400) {
$stmt->execute(array(
$data->id,
$data->created_time,
$data->place->id,
$data->place->location->city,
$data->place->location->country,
$data->place->location->latitude,
$data->place->location->longitude,
$data->place->location->state,
$data->place->location->street,
$data->place->location->zip,
$data->place->name
));
}
}
Everytime I return to the page it takes the same entries and continuously adds them to the database.
I would like to say something like
if $data->created_time == any created_time value in the DB then don't add this value,
as well as currently I am doing
if (time() - strtotime($data->created_time) < 86400)
to make sure it is not older then 24 hours.
How can I add this condition?
Option 1: (Recommeded)
Make id the primary key for your table, taggedPlaces:
ALTER TABLE taggedPlaces ADD PRIMARY KEY(id)
Then change your insert statement to use INSERT IGNORE which will skip duplicate inserts.
Option 2:
Make created_time a unique field in taggedPlaces:
ALTER TABLE taggedPlaces ADD UNIQUE(created_time);
Then, again, use INSERT IGNORE to skip duplicates.
Option 3: (Not recommeded, but will work)
Prior to running your insert, perform another query to check if $data->created_time is already in the table:
$check = $con->prepare('
SELECT id FROM taggedPlaces
WHERE created_time = ?
');
$check->execute(array($data->created_time));
if (count($check->fetchAll()) == 0) {
// No duplicates found. Proceed...
}

data not coming in proper order from table using php

I am trying to insert data from feed into the database using Zend and also adding a column date_updated in table so that whenever the article in feed is updated the column also gets updated. But the problem is, the first article is being inserted earlier and thenafter the others. So, when I am trying to do a select of top 10 articles based on date_updated DESC, the article being inserted at last is getting on top and if i use ASC then the older ones are getting selected. Please suggest how do i proceed. The query I am writing is:
$sql = "INSERT INTO news_article
(original_article_id, headline,summary, keywords, link, section, topic, date_published, date_updated, content, source_id)
VALUES (?,?,?,?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE
original_article_id = ?,
headline = ?,
summary = ?,
keywords = ?,
link = ?,
section = ?,
topic = ?,
date_published = ?,
date_updated = ?,
content = ?,
source_id = ?";
$values = array(
"original_article_id"=>$id,
"headline"=>$item->title,
"summary"=>$summary,
"keywords"=>$keywords,
"link"=>$item->link,
"section"=>"property",
"topic"=>"property",
"date_published"=>$formattedPubDate,
"date_updated"=>$currentDate,
"content"=>$data,
"source_id"=>"3"
);
$result = $db->query(
$sql,
array_merge(array_values($values), array_values($values))
);
and thenafter i am using
SELECT * FROM news_article ORDER BY date_updated DESC LIMIT 10
use below query
SELECT * FROM news_article ORDER BY date_updated DESC LIMIT 0,10
After limit we need to pass offset and number of records to fetch.

mysql/php insert/update on duplicate key

I have a product info table with more than 130 columns/fields.
I want to write a php script that adds a new product to the table OR updates the existing product if it already exist. The first field is the product key.
The product information is stored in a numbered php array : $product_info[0] to $product_info[130].
Basically something like this :
INSERT INTO table (a,b,c) VALUES ($product_info[0],$product_info[1],$product_info[2])
ON DUPLICATE KEY UPDATE a='$product_info[0]', b='$product_info[1]', c='$product_info[2]'
Is there something more efficient than typing each of the 130 fields twice?
Yes, there is, use the VALUES() function:
INSERT INTO `table` (a, b, c) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE a = VALUES(a), b = VALUES (b), c = VALUES(c)
Basically, in the UPDATE part, VALUES(column) will return the specified value for that column for the current row in question. So you can do interesting things like:
ON DUPLICATE KEY UPDATE
a = VALUES(a),
b = VALUES(b) + VALUES(c),
The beauty of that syntax, is it also supports multiple insert rows:
INSERT INTO `table` (a, b, c)
VALUES (?, ?, ?),
VALUES (?, ?, ?),
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE a = VALUES(a), b = VALUES (b), c = VALUES(c)
Unfortunately MySQL does not support merging... having an ORM can help ease the pain of coding multiple IF EXISTS UPDATE ... ELSE INSERT code
REPLACE

Categories