php pdo select multiple rows and insert to other table with LIMIT - php

i have 2 tables equip_copy(copyID, equipment_id) and insert it to table
mre_copy (mreID,copyID,equipment_id)
I have tried this Select query but doesnt move. Please anyone can help me?
$display = $con->query("SELECT copyID,equipmentID
FROM equip_copy
WHERE equipmentID= :eid
ORDER BY copyID DESC
LIMIT :elimit");
$display->execute(array("eid" => $id, "elimit"=>$request));
foreach($display as $row){
$newCID = $row['copyID'];
$newEID = $row['equipmentID'];
$sql_table = "INSERT INTO mre_copy(mreID,equipmentID,copyID) values(?,?,?)";
$stmt = $con->prepare($sql_table);
$stmt->execute(array($mreID,$newEID,$newCID));
}

Use insert . . . select. I think this is what you want to do
INSERT INTO mre_copy (mreID, equipmentID, copyID)
SELECT :mreID, copyID, equipmentID
FROM equip_copy
WHERE equipmentID = :eid
ORDER BY copyID DESC
LIMIT :elimit;
I'm not sure where mreID comes from. If it is auto-incremented, then just leave it out of the INSERT altogether.

Related

Get applicants from Table 1 and compare id and get user details from Table 2 Using PHP

Ok so I have two Tables
Applicant list - this shows all applicants
User Table
Now I'm Providing news_id by Post method and I want to list details of all users(email,mobile,username) where the value for user_authToken and user_authtoken is same. Can Someone help me out with this logic using PHP.
$job_id = $_POST['job_id'];
$resultSet = $con->query("SELECT appliers_list.news_id AS jobid ,
appliers_list.user_authToken AS user,
user.user_name AS username,
user.mobile AS mobile,
FROM appliers_list,user
WHERE appliers_list.news_id = '$job_id'
ORDER BY appliers_list.id DESC
");
$rows = $resultSet->fetch_assoc();
First of all, your naming is very inconsistent, it's hard to read and understand.
Second, please use prepare statement, otherwise you open your system to SQL injection.
$news_id = $_POST['job_id'];
$stmt = $con->prepare("SELECT email, mobile, user_name
FROM users
WHERE user_authtoken in (select user_authToken from appliers_list where news_id = ?)");
$stmt->bind_param("i", $news_id);
$stmt->execute();
$resultSet = $stmt->get_result();
while($row = $resultSet->fetch_assoc()) {
// data manipulation here
}
you can use left join to get record from both table :
$job_id = !empty($_POST['job_id']) ? intval($_POST['job_id']) : 0;
$resultSet = $con->query("SELECT appliers_list.*,users.email
FROM appliers_list
left join users on appliers_list.user_authToken = users.user_authToken
WHERE news.news_id = '$job_id'
ORDER BY news.id DESC
");
$rows = $resultSet->fetch_assoc();
You didn't specify a relationship between the user and appliers_list tables, so you're getting all rows in user. You also have an extra comma at the end of the SELECT list.
$job_id = $_POST['job_id'];
$resultSet = $con->query("SELECT appliers_list.news_id AS jobid ,
appliers_list.user_authToken AS user,
user.user_name AS username,
user.mobile AS mobile
FROM appliers_list
JOIN user ON appliers_list.user_authToken = user.user_authToken
WHERE appliers_list.news_id = '$job_id'
ORDER BY appliers_list.id DESC
");
$rows = $resultSet->fetch_assoc();

php update query with order

please i am having issues concerning the order of records after updating.
I have two pages home.php and settings.php.
The home.php contains result from a mysli select query.
$getid = $getdata['user_id'];
$query = mysqli_query($con,"SELECT * FROM posts WHERE `user_id` = ' $get_id' ORDER BY date_added DESC LIMIT 15 ");
The records being selected are id username notes date_added, these records are successfully displayed in desc order.
On my settings.php i'm allowing the user to update their username,while updating their username,i also run a update query that change the username on the notes table. New username are successfully updated both on the user table and notes table. But the unfortunate things is,when i get back to the home.php , the notes are not displayed in desc order again. it is displaying the notes from the top of the table.
The update query function(called from the settings.php):
function update_user($update_data){
$con = db();
global $session_user_id;
$update= array();
array_walk($update_data, 'array_sanitize');
foreach ($update_data as $field => $data) {
$update[] = '`'. $field . '` =\''.$data .'\'';
}
mysqli_query($con,"UPDATE `users` SET " . implode(', ', $update) . , " WHERE `user_id` = '$session_user_id'");
mysqli_query($con,"UPDATE notes SET username = (SELECT `username`FROM users WHERE user_id = $session_user_id) WHERE user_id = $session_user_id ");}
I have tried ordering by date_added, it's not working still.
Kindly help me out, thanks.

need help returning sql sum of $variable table name

I'm trying to return the sum of balances from a table and can get the following code to work when using a specific table name
$qry = mysql_query("SELECT SUM(Balance) AS total FROM table1 ");
$row = mysql_fetch_assoc($qry);
echo $row['total'];
The problem I'm having is that my table name changes and this needs to be a variable but when I use the following code I get no result
$table="table1";
$qry = mysql_query(" SELECT SUM(Balance) AS total FROM $table ");
$row = mysql_fetch_assoc($qry);
echo $row['total'];
Can anyone offer some help please?
How about:
$qry = mysql_query(" SELECT SUM(Balance) AS total FROM " . $table );

using values from a query in another query

i have a query
$result = mysql_query("SELECT * FROM comprofiler WHERE cb_playstationgames LIKE '%FIFA%' ORDER BY id ASC");
in that query there is a user_id wich i need to perform a query on another table.
$gebruikerid = mysql_query("SELECT * FROM users where id LIKE '".$result['user_id']."'");
Now i want to use that value in a while loop
echo "<table><tr><th width=\"300\" align=\"left\" >Avatar</th><th width=\"300\" align=\"left\">Naam</th><th width=\"200\" align=\"left\">PSN Naam</th></tr>";
while($row2 = mysql_fetch_array($result))
{
echo "<tr><td><img height=\"50\" width=\"50\" src=\"/images/comprofiler/" . $row2['avatar'] . "\"></td><td>" . $gebruikernaam . "</td><td>" . $row2['cb_psnnaam'] . "</td></tr>";
}
echo "</table>";
I cannot get the query to read the values from the other table based on the id from the first table. Can someone help me?
In your case the best solution it's to use JOIN.
$result = mysql_query("SELECT comprofiler.*, users.* FROM comprofiler INNER JOIN users ON users.id = comprofiler.user_id WHERE cb_playstationgames LIKE '%FIFA%' ORDER BY comprofiler.id ASC");
You may have to manually specify the columns you want to select (after SELECT ) in case that you have same column names in both table and you need both.
$result = mysql_query("SELECT comprofiler.id as id_comprofiler, users.id as id_user, users.avatar ... " );
To answer your question why it does not work:
Second query does not use mysql_fetch_array.
$gebruikerid = mysql_fetch_array($gebruikerid);
It would be more secure & cleaner if you would use PDO or MySQLi prepared statements like:
$db = new \PDO(SEE PDO CUNSTRUCT)
$query1 = $db->prepare('
SELECT *
FROM comprofiler
WHERE cb_playstationgames LIKE :fifa
ORDER BY id ASC
');
$query2 = $db->prepare('
SELECT * FROM users where id LIKE :id
');
$query1->bindValue(':fifa', '%FIFA%', PDO::PARAM_STR);
$query1->execute();
while ($row = $query1->fetch(PDO::FETCH_ASSOC)) {
$query2->bindParam(':id', $row['user_id'], PDO::PARAM_INT);
$query2->execute();
//Holds associative array of second query
$row2 = $query->fetch(PDO::FETCH_ASSOC);
//$row1 holds associative array of first query
}

Pass column value from Select query to another query for insertion in PHP

I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.
Anyone got any suggestions?
You need to execute select query 1st then use its output to execute 2nd & 3rd query.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
mysql_query($query2);
mysql_query($query3);
}

Categories