php update query with order - php

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.

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 pdo select multiple rows and insert to other table with LIMIT

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.

How to update a value by 1 if the new value inserted into the database clashes with value in the database?

I want to update the database of the sort order column to increase its value by one if the the new value inserted into the database clashes with the value that is already in the database. May I know how should I go about doing it? Please help! Thanks!
Below is my code (I am not sure whether am I on the right track):
$result = mysql_query("SELECT sortorder FROM information ORDER BY id ASC;");
if($result >= 1 ){
$i=1;
while ($initialorder = mysql_fetch_assoc($result))
{
$initialorder = $initialorder["sortorder"];
if ($sortorder == $initialorder ){
$result6 = mysql_query("SELECT * FROM information
WHERE `sortorder` = '$sortorder'");
$row6 = mysql_fetch_array($result6);
$removethis1 = $row6['id'];
$result7 = mysql_query("UPDATE information
SET `sortorder`= ((SELECT `sortorder`
FROM (SELECT MAX(`sortorder`) AS
'$initialorder' FROM information) AS '$initialorder') + 1)
WHERE id='$removethis1'");
}
$query = "INSERT INTO `information`
(`id`,`page`,`description`,`status`,`sortorder`,`keyword`,`date_added`)
VALUES
('$id','$title','$description','$status',
'$sortorder','$keyword','$date_added')";
$result = mysql_query($query, $conn);
header('Location: index.php?status=1&title='.$title);
$i++; }
}
You can do this:
INSERT INTO ON `information`
...
DUPLICATE KEY UPDATE
sortorder = '".$sortorder + 1." '

Duplicates in php while loop

I am getting a bunch of id's from the database - for each id, I want to do a mysql query to get the relating row in another table. This works fine although I also need to get similiar data for the logged in user. With the mysql query I am using - it duplicates the logged in user data according to how many id's it originally pulled. This makes sense although isnt what I want. I dont want duplicate data for the logged in user and I need the data to come from one query so I can order things with the timestamp.
<?php
mysql_select_db($database_runner, $runner);
$query_friends_status = "SELECT DISTINCT rel2 FROM friends WHERE rel1 = '" . $_SESSION ['logged_in_user'] . "'";
$friends_status = mysql_query($query_friends_status, $runner) or die(mysql_error());
$totalRows_friends_status = mysql_num_rows($friends_status);
while($row_friends_status = mysql_fetch_assoc($friends_status))
{
$friends_id = $row_friends_status['rel2'];
mysql_select_db($database_runner, $runner);
$query_activity = "SELECT * FROM activity WHERE user_id = '$friends_id' OR user_id = '" . $_SESSION['logged_in_user'] . "' ORDER BY `timestamp` DESC LIMIT 15";
$activity = mysql_query($query_activity, $runner) or die(mysql_error());
$totalRows_activity = mysql_num_rows($activity);
echo "stuff here";
}
?>
You can try this single query and see if it does what you need
$userID = $_SESSION['logged_in_user'];
"SELECT * FROM activity WHERE user_id IN (
SELECT DISTINCT rel2 FROM friends
WHERE rel1 = '$userID')
OR user_id = '$userID' ORDER BY `timestamp` DESC LIMIT 15";
You probably want something like this:
$user = $_SESSION['logged_in_user'];
  $query_friends_status = "SELECT * FROM friends, activity WHERE activity.user_id = friends.rel2 AND rel1 = '$user' ORDER BY `timestamp` DESC LIMIT 15";
You can replace * with the fields you want but remember to put the table name before the field name like:
table_name.field_name
I hope this helps.
<?php
require_once "connect.php";
$connect = #new mysqli($host, $db_user, $db_password, $db_name);
$result = $connect->query("SELECT p.name, p.user, p.pass, p.pass_date_change,p.email, p.pass_date_email, d.domain_name, d.domain_price, d.domain_end, d.serwer, d.staff, d.positioning, d.media FROM domains d LEFT JOIN persons p
ON d.id_person = p.id AND d.next_staff_id_person != p.id");
if($result->num_rows > 1)
{
while($row = $result->fetch_assoc())
{
echo '<td class="box_small_admin" data-column="Imię i nazwisko"><div class="box_admin">'.$row["name"].'</div></td>';
echo '<td class="box_small_admin" data-column="Domena"><div class="box_admin">'.$row["domain_name"].'</div></td>';
}}
?>

Getting record from another table with id recognition

there are tables artist, track, & etc
in the artist table there is :
id
name
cover
desc
and in the track table there is :
id
name
desc
artistid
so if i goto track.php?id=1 and then they print
id
name
desc
artistid i want to make this show the record from artist table with id recognition
and can you show me how to make multiple filter from recordset. because i have field "pubid" when the value is 1 this mean publish and then if the value is 2 this mean unpublish
sorry bad english
thx u so much
Have a look at SQL Join. In order to only get records which may be published, you'll have to add another WHERE clause. Like:
SELECT name, desc FROM track WHERE id = $id AND pubid = 1;
track.php?id=1
or
track.php?id=1&pubid=1
track.php?id=1&pubid=2
<?php
if (isset($_GET['id'])) {
$artistid = $_GET['id'];
if (isset($_GET['pubid'])) {
$pudid = $_GET['pubid'];
$sql = "select `id`, `name` from `track` where `artistid` = {$artistid} and `pubid` = {$pupid} order by `desc`";
} else
$sql = "select `id`, `name` from `track` where `artistid` = {$artistid} order by `desc`";
$query = mysql_query($sql);
while (($row = mysql_fetch_array($query)) !== false) {
echo $row['name'];
}
}

Categories