foreach with mysqli_multi_query issue - php

I have the below code which works fine and updates each record contained in the array:
$check_list = isset($_POST['check_list']) ? $_POST['check_list'] : array();
foreach($check_list as $check_list) {
$query = "UPDATE `stock` SET `signature_id` = 0,
user_id = 0,
`status_id` = 1
WHERE `id` = '$check_list'";
$result = mysqli_query($conn, $query);
I now need it to execute multiple queries for each result in the array so I have changed the code to the following using mysqli_multi_query:
$check_list = isset($_POST['check_list']) ? $_POST['check_list'] : array();
foreach($check_list as $check_list) {
$query = "UPDATE `stock` SET `signature_id` = 0,
user_id = 0,
`status_id` = 1
WHERE `id` = '$check_list';
INSERT INTO `returned`
(`id`, `stock_id`, `signature_id`,
`user_id`, `timestamp`)
VALUES ('','$check_list','$id',
'$user_id',now())";
$result = mysqli_multi_query($conn, $query);
But it now only executes one UPDATE and one INSERT for the first record in the array, and ignores the others

#RiggsFolly is giving the best advice about prepared parameterised statements and transactions due to re-usability and security, but if you want/need to stay with mysqli_multi_query, (because you don't want to transition to a new querying process mid-project or because it is otherwise unappealing to you) here is how mysqli_multi_query can serve you:
Query Combination:
If the SET values stay the same and only the id's are different, all UPDATE queries can be merged into a single query. If the values are static you can use implode(), if not you can chose between using a (verbose/ugly) CASE statement in the SET clause of a single query, or create multiple UPDATE queries as in your original post.
$queries="UPDATE `stock` SET `signature_id`=0,`user_id`=0,`status_id`=1 WHERE `id` IN (".implode(',',$check_list).");";
Likewise with the INSERT queries, they can all be merged into one statement with implode() or a foreach loop that only extends the VALUE portion.
$queries.="INSERT INTO `returned` (`stock_id`,`signature_id`,`user_id`,`timestamp`) VALUES ('".implode("','$id','$user_id',now()),('",$check_list)."','$id','$user_id',now());";
or
$queries.="INSERT INTO `returned` (`stock_id`,`signature_id`,`user_id`,`timestamp`) VALUES ";
foreach($check_list as $k=>$check_list){
// manipulate $id and $user_id as needed
$queries.=($k==0?"":",")."('$check_list','$id','$user_id',now())";
}
Failure Awareness:
If you don't need any kind of indication of success then a one-liner will do (keep this outside of any loops of course):
mysqli_multi_query($conn,$queries)
otherwise, you'll need a slightly larger block of code:
if(mysqli_multi_query($conn,$queries)){
do{
echo "<br>Rows = ",mysqli_affected_rows($conn);
} while(mysqli_more_results($conn) && mysqli_next_result($conn));
}
if($mysqli_error=mysqli_error($conn)){
echo "<br>Syntax Error: $mysqli_error";
}
I have tested my solution using implode() for both queries and was successful using:
$check_list=array(1,3,5,6,10,11);
and a database setup of:
CREATE TABLE `stock` (
id int(10) NOT NULL AUTO_INCREMENT,
signature_id int(10) NOT NULL,
user_id int(10) NOT NULL,
status_id int(10) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE `returned` (
id int(10) NOT NULL AUTO_INCREMENT,
stock_id int(10) NOT NULL,
signature_id int(10) NOT NULL,
user_id int(10) NOT NULL,
`timestamp` datetime NOT NULL,
PRIMARY KEY (id)
);
/* Declaring your `id` columns with AUTO_INCREMENT means you can omit them from your INSERT query. */
/* Depending on your mysql version, creating a default datetime for `timestamp` may be possible which then would permit omitting `timestamp` from your INSERT query too. */
INSERT INTO `stock` (`signature_id`,`user_id`,`status_id`) VALUES
(1,1,1),
(2,2,2),
(3,3,3),
(4,4,4),
(5,5,5),
(6,6,6),
(7,7,7),
(8,8,8),
(9,9,9),
(10,10,10),
(11,11,11),
(12,12,12);
The built query looks like this:
UPDATE `stock` SET `signature_id`=0,`user_id`=0,`status_id`=1 WHERE `id` IN (1,3,5,6,10,11);INSERT INTO `returned` (`stock_id`,`signature_id`,`user_id`,`timestamp`) VALUES ('1','','',now()),('3','','',now()),('5','','',now()),('6','','',now()),('10','','',now()),('11','','',now());

Related

How to allow mutilple page to insert multiple data at the same time (will not clash) in MySQL PHP

I have a online page which will allow user to create an account for them in order to acceess our page.
I worry in some period, there will be a lots of user who create at the same time.
In that case, I worry my database will be clash or conflict.
Can I know is that anyway to prevent that happens?
My table as below:
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`userid` varchar(30) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`name` varchar(40) DEFAULT NULL
)
ALTER TABLE `user`
ADD PRIMARY KEY (`id`);
ADD UNIQUE KEY `userid` (`participant_id`);
ALTER TABLE `user`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
COMMIT;
So my id will be just number and auto increment.
userid wil be unique.
I created the page by using PHP.
And I use following insert command at my page:
do {
$query = "INSERT IGNORE INTO user(userid, password, name) VALUES ('$userid','$password','$name')";
$insert = $conn->query($query);
} while( $insert && ($conn -> affected_rows == 0) );
Are this code can work perfectly to prevent the date conflit?
Another extra question is, how about if I create another extra page which will insert information 'user' table and can I used the same code at the new page?

How to work with temporary table in codeigniter, like updating the column and fetching the result?

i need a little help.
i am working with a module in codeigniter which deals with
$sql = "CREATE TEMPORARY TABLE `temp_user_rank` (
`user_id` int(11) NOT NULL,
`job_type` varchar(150) NOT NULL,
`score` int(11) NOT NULL
) ENGINE=MyISAM AS ( SELECT `user_id`,`job_type` ,`score` FROM jh_user_profile WHERE user_id IN($result_users) )";
$this->db->query($sql);
//after the insertion
I need to update the table score and then fetch the result user_id based on score using $this->db->query('select user_id FROM temp_user_rank') ;
I got the answer working with temporary table in codeigniter.
$this->db->query("DROP TABLE IF EXISTS temp_user_rank");
//passing the above sql here
$this->db->query($sql);
$result = $this->db->query('SELECT * FROM temp_user_rank')->result_array();
the result variable contains all results

Symfony2 MySQL: INSERT SELECT syntax error

I am having problems with writing correct MySql query. I want to insert new collection for every user with id higher than 1000 but less than 10000.
$conn = $this->em->getConnection();
$stmt = $conn->prepare('INSERT INTO collection (name, type)
values(:name, :type)
SELECT * FROM user WHERE id<:endUser AND id>:startUser');
$stmt->bindValue('name', 'Default');
$stmt->bindValue('type', 0);
$stmt->bindValue('startUser', 1000);
$stmt->bindValue('endUser', 10000);
$stmt->execute();
This what I tried to write, but I get syntax error. Please explain me how to correct query
UPD
I should have given detailed structure of tables.
Collection
CREATE TABLE IF NOT EXISTS `collection` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`type` smallint(6) NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_FC4D6532A76ED395` (`user_id`)
);
User
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
User has one-to-many relationship with Collection.
With a SELECT INTO you have to select the values you want to place in the new row and only those values. And you dont use the VALUES() clause.
As you are using static values for the new rows and not values from the user table you can do it like this.
Oh and I see in your edit you were using the wrong table name It should have been fos_user
Also as fos_user.user_id is a NOT NULL field you need to include that column in the list of fields in the insert.
$conn = $this->em->getConnection();
$stmt = $conn->prepare('INSERT INTO collection (user_id, name, type)
SELECT id, 'default', 0
FROM fos_user
WHERE id > :startUser AND id < :endUser');
$stmt->bindValue('startUser', 1000);
$stmt->bindValue('endUser', 10000);
$stmt->execute();

MYSQL select where timestamp data not retrieve

I have mysql table as follows;
CREATE TABLE IF NOT EXISTS `cheque_data` (
`auto_no` int(11) NOT NULL AUTO_INCREMENT,
`job_no` int(11) NOT NULL,
`client_id` text NOT NULL,
`ch_no` text NOT NULL,
`ch_date` date NOT NULL,
`ch_bank` text NOT NULL,
`ch_amount` decimal(10,2) NOT NULL,
`sync` int(1) NOT NULL DEFAULT '0',
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`auto_no`)
)
When I run SQL command in PHPMYADMIN it will retrieve row;
SELECT * FROM `cheque_data` WHERE timestamp='2014-03-10 19:37:31'.
But in PHP,
$sql = "select * from `cheque_data` where timestamp = '2014-03-10 19:37:31'";
$result_remote = mysqli_query($con,$sql) or die(mysqli_error($con));
$row_count = mysqli_num_rows($result_remote);
echo $row_count; //no data
It doesn't give any data. But when I change timestamp to other column, it works.
$sql = "select * from `cheque_data` where auto_no = '1'";
$result_remote = mysqli_query($con,$sql) or die(mysqli_error($con));
$row_count = mysqli_num_rows($result_remote);
echo $row_count; //data found
I want to know, what is the reason for this issue?
It is only not working with where timestamp
Thank you,
Sameera
add backticks(`) around timestamp field, as it is reserved word or use this query
$sql = "select * from `cheque_data` where `timestamp` = '2014-03-10 19:37:31'";
Possibly, the problem in your PHP script, not MySQL.
P.S. keep in mind, that it is better to wrap column names in ` not to confuse MySQL. In your statement it is okay, but fyi there is a function called TIMESTAMP(), and in some cases MySQL may think of a non-wraped column name as of a function name.

How do I manage multiple combined index keys in Yii MVC?

I have developed a script using Yii MVC and i have a problem with the index keys and criterias.
I want to prevent the insertion of a record that is already stored in the database;
My example, fails to check and tries to add a new record each time.
Why ? And how to do this ?
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '21-/popular/category/1.html' for key 'index_link'. The SQL statement executed was: INSERT INTO `categories` (`server_id`, `website_id`, `slave_category_id`, `link`, `name`, `image`, `videos`, `status`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7)
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`server_id` int(11) NOT NULL,
`website_id` int(11) NOT NULL,
`slave_category_id` int(11) NOT NULL,
`link` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`videos` int(11) NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_link` (`website_id`,`link`),
UNIQUE KEY `index_name` (`website_id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=332 ;
the PK is id;
another index, is website_id + link;
another index is website_id + name;
the following code fails to check and prevent the insertion:
1
$criteria_categories = new CDbCriteria();
$criteria_categories->condition = " `server_id`=':server_id' and `website_id`=':website_id' and `link`=':link' and `name`=':name' ";
$criteria_categories->params = array(
':server_id' => $model_website->server_id,
':website_id' => $model_website->id,
':link' => $matches_url[$value->link][$key2],
':name' => $matches_url[$value->name][$key2],
);
$record_categories = Categories::model()->find($criteria_categories);
print_r($record_categories);
if (!$record_categories) {
$model_categories = new Categories();
$model_categories->server_id = $model_website->server_id;
$model_categories->website_id = $model_website->id;
$model_categories->slave_category_id = 1; //??
$model_categories->link = $matches_url[$value->link][$key2];
$model_categories->name = $matches_url[$value->name][$key2];
$model_categories->image = $matches_url[$value->image][$key2];
$model_categories->videos = 0;
$model_categories->status = 0;
$model_categories->save();
}
$criteria_categories->condition = " `server_id`=':server_id' and `website_id`=':website_id' and `link`=':link' and `name`=':name' ";
You have a couple errors going on. One is syntax. If using the parameter binding (which is a good thing to use), you don't want to quote the parameters.
That is, write :link instead of ':link'.
The above condition also does not correctly check for existing records that have either of your two unique keys. Try the following:
$criteria_categories->addCondition("`server_id`=:server_id AND `website_id` = :website_id
AND (`link`=:link OR `name` = :name)");
This both quotes correctly and will find the record if either of your two unique keys match, rather than requiring both to match.

Categories