In the "user_id" column of my table, I'd like to insert the ID of the user who just registred from my page. The idea is to associate his recent generated income with the users id, just to spot an eventual double registration of the income.
In order to do this, I tought to update the user_id column, on the row where income_id has the biggest value, i.e. the last generated income, but something isn't working. My code is:
$query = "SELECT max( id_income ) FROM `affiliate_income`";
$last_income = mysql_query($query, $conn) or die(mysql_error());
$last = mysql_fetch_assoc($last_income);
$updtsql = "UPDATE affiliate_income SET `id_user`=".$row_user_code['id_user']."WHERE id_income =".$last;
$result = mysql_query($updtsql, $conn) or die(mysql_error());
any ideas?
Actually you can do it in one query,
UPDATE affiliate_income a
INNER JOIN (SELECT MAX(id_income) id_income FROM affiliate_income) b
ON a.id_income = b.id_income
SET a.id_user = 'valueHere'
You get the value of $last as array.So you have to giv the query like the following
$updtsql = "UPDATE affiliate_income SET `id_user`=".$row_user_code['id_user']."WHERE id_income =".$last['id_income'];
You try to get the max id_income but in the second query (updtsql) you try to find the id_income = array. Besides, you do not put a white space before WHERE clause.
$query = "SELECT max( id_income ) AS ii FROM `affiliate_income`";
$last_income = mysql_query($query, $conn) or die(mysql_error());
$last = mysql_fetch_assoc($last_income);
$updtsql = "UPDATE affiliate_income SET `id_user`=".$row_user_code['id_user']." WHERE id_income =".$last['ii'];
$result = mysql_query($updtsql, $conn) or die(mysql_error());
Related
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." '
Hopefully this will be quite simple for someone.
I have the following code:
<?php
// Connects to your Database
mysql_connect("localhost", "xxxxx", "xxxxx") or die(mysql_error());
mysql_select_db("xxxxx") or die(mysql_error());
require("../includes/common.php");
require("admin_header.php");
require("admin_menu.php");
$query = "Truncate TABLE pt_menutitles";
$result = mysql_query($query) or die(mysql_error());
// Menu Headers for Category
$data = "select a.menuheader, a.total from(SELECT distinct (menuheader),count(*) as total FROM `pt_products` WHERE `menuheader` <> '' group by `menuheader` order by total desc limit 4) a order by a.menuheader";
$result = mysql_query($data) or die(mysql_error());
while($info = mysql_fetch_array($result))
{
$menudata = "select a.subcategory, a.menuheader,a.totcount FROM(SELECT distinct (subcategory),menuheader,count(*) as totcount FROM `pt_products` WHERE `menuheader`='".$info['menuheader']."' AND subcategory <> ''group by `subcategory` order by totcount desc limit 4) a order by a.subcategory";
$menuresult = mysql_query($menudata) or die(mysql_error());
while($menuinfo = mysql_fetch_array($menuresult))
{
$sql = "Insert into pt_menutitles (menu, title, totalcount) select '".$menuinfo['menuheader']."','".$menuinfo ['subcategory']."','".$menuinfo ['totcount']."'";
$result = mysql_query($sql) or die(mysql_error());
}
}
?>
Basically I take the top 4 menu titles that have the most items in them, then select the top 4 subcategories in them titles and insert them into a table.
What is happening though is that i'm onlyt getting Menu Title 1 and Subcategories 1 to 4 inserted into my table.
It's as though the the loop is ending after the first time round?
Any advise would be great!
Cheers
Chris
This line replaces your existing result and ends the loop. Use any other (non-existing) variable name, but not $result.
$sql = "Insert into pt_menutitles (menu, title, totalcount) select '".$menuinfo['menuheader']."','".$menuinfo ['subcategory']."','".$menuinfo ['totcount']."'";
$result = mysql_query($sql) or die(mysql_error());
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);
}
I'd like to update a row in a MySQL table when a specific row number is reached
This is a little confusing since , the real row number of the record isn't a column in the table.
And there's no question of iterating over rows , since we're not iterating over an array as in mysql_fetch_array()
So , if I'd like to update - say the 3rd row of the table , what would the query be like?
I'm a noob at MySQL
Thanks a ton for your help ! :D
$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
$query = "SELECT MyColoumn FROM Mytable";
$result = mysqli_query($link, $query);
$row_needed = 3; //Your needed row e.g: 3rd row
for ($i=1,$i=$row_needed,$i++) {
$row = mysqli_fetch_array($result);
}
// now we are in 3rd row
$query = "UPDATE MyColumn FROM MyTable SET MyColumn = '".$MyColumnUpdate."' WHERE MyColumn = '".$row['MyColumn']."' ";
$result = mysqli_query($link, $query);
...
Try this query
UPDATE
tbl a,
(Select
#rn:=#rn+1 as rowId,
tbl.*
from
tbl
join
(select #rn:=0) tmp) b
SET
a.columnName = <VALUE>
WHERE
b.rowId = <rowNumber> AND
a.id = b.id;
NOTE The id column must be a unique one, you can use the primary key of that table...
SQLFIDDLE
I have following script:
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT * FROM `other_table`";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[$row['username']];
How can I set one variable row inside another, since it does not work. Basically, I need to select username, and then select column with user username from other table, in which is written user points.
I was thinking about adding:
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT `".$row['username']."` FROM `other_table` WHERE `uid` = 1";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[xxxxxxxxxx]; // DONT KNOW HOW TO DEFINE IT, so it takes out found variable (there is only one).
Guess you want something like
SELECT * FROM table1 t1, table2 t2 WHERE t1.user_name = t2.user_name?
Think about using JOIN
$sql = "SELECT * FROM users;";
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = sprintf("SELECT * FROM other_table where username='%s';", $row['username']);
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
// now $row1 contains the tuple of this user and could access the variables are you would
// normally do e.g. $row1['ID']
SELECT * FROM users AS u INNER JOIN other_table AS o ON u.username = o.username
I'm assuming you want to do this because you want to be able to access all the rows from either table where a particular user name is the same (e.g. the data from users where username="john" and the data from other_table where username="john" for all usernames). No need to nest a result set to do this, just use a JOIN statement and then you can access all the columns as if it was a single result set (because it is):
$sql = "SELECT * FROM users AS u INNER JOIN other_table AS o ON u.username = o.username";
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$item = $row['any_column'];
FYI you should list out the column you want to retrieve instead of using *, even if you want to retrieve them all, as it is better practice in case you add new columns in the future.