I am creating an inventory system for Magic the Gathering Cards and need to update the prices with the main card info.
I have two tables, Cards and Prices
Cards has the following columns:
ID, Name, Ed, Price
Prices has the following columns:
Name, Ed, Price
I need Cards.Price to be replaced with the value in Prices.Price.
Below is the code for two different attempts to make this work (I know I am probably making this a lot harder than it needs to be....)
Attempt #1
$query = "SELECT * FROM Cards";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$name=$row['Name'];
$ed=$row['Ed'];
$queryb = "SELECT Price FROM Prices WHERE Name='".$name."' AND Ed='".$ed."'";
$resultb = mysql_query($queryb) or die(mysql_error());
$rowb = mysql_fetch_array($resultb) or die(mysql_error());
$newPrice = $rowb['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."'");
}
Attempt #2
$queryCards = "SELECT * FROM Cards";
$queryPrices = "SELECT * FROM Prices";
$dblink = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $dblink);
$resultCards = mysql_query($queryCards) or die(mysql_error());
$resultPrices = mysql_query($queryPrices) or die(mysql_error());
$rowCards = mysql_fetch_array($resultCards) or die(mysql_error());
$rowPrices = mysql_fetch_array($resultPrices) or die(mysql_error());
if ($rowCards['Name']==$rowPrices['Name'] && $rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
while($rowPrices = mysql_fetch_array($resultPrices))
{
if ($rowCards['Name']==$rowPrices['Name'] &&
$rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
}
$rowPrices = mysql_fetch_array($resultPrices) or die(mysql_error());
while($rowCards = mysql_fetch_array($resultCards))
{
while($rowPrices = mysql_fetch_array($resultPrices))
{
if ($rowCards['Name']==$rowPrices['Name'] &&
$rowCards['Ed']==$rowPrices['Ed'])
{
$newPrice = $rowPrices['Price'];
mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE
Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'");
}
}
}
UPDATE Cards
JOIN Prices ON Cards.Name = Prices.Name AND Cards.Ed = Prices.Ed
SET Cards.Price = Prices.Price
Related
<?php
require_once "config.php";
$sql = "SELECT * FROM charges";
$results = mysqli_query($link, $sql);
$row = mysqli_fetch_array($results);
$count = mysqli_num_rows($results);
for ($i=0; $i <=$count ; $i++) {
$id = $row['c_id'];
$newBalance = $row['charge']+$row['balance'];
$query = "UPDATE charges SET balance = $newBalance WHERE c_id = $id";
mysqli_query($link, $query)
}
?>
that's my php code
and the below pic is my database tables
database table
Your query is:
UPDATE charges SET balance = balance + charge
With php it is:
$sql = "UPDATE charges SET balance = balance + charge";
$results = mysqli_query($link, $sql);
I'm running one while inside another while but the second one is running only one time why and how can I fix it. I have also try with for but running again only once.
$sql = "SELECT DISTINCT season FROM search WHERE link = '$getid' Order by id asc";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysql_fetch_assoc($result))
{
$season = $list['season'];
$sql = mysql_query("SELECT * FROM search WHERE link = '$getid' and season = '$season'");
$episodes = mysql_num_rows($sql);
echo '1st';
$sqls = "SELECT * FROM search WHERE link = '$getid' and season = '$season' Order by id asc";
$results = mysql_query($sqls, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($lists = mysql_fetch_assoc($results))
{
$episode = $lists['episode'];
echo'2nd';
}
}
You are overriding the variables, use different ones:
$sql = "SELECT DISTINCT season FROM search WHERE link = '$getid' Order by id asc";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysql_fetch_assoc($result))
{
$season = $list['season'];
$sql2 = mysql_query("SELECT * FROM search WHERE link = '$getid' and season = '$season'");
$episodes = mysql_num_rows($sql2);
echo '1st';
$sqls = "SELECT * FROM search WHERE link = '$getid' and season = '$season' Order by id asc";
$results2 = mysql_query($sqls, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($lists2 = mysql_fetch_assoc($results2))
{
$episode = $list2['episode'];
echo'2nd';
}
}
I am pulling everything from a database table and displaying specific data in php.
So far all my code is working, I'm able to display in php everything but I'm trying to make another php file to only display specific data. For example I want it to find everything in the movieview table and only display all row data that has a playcount of equal or greater than 1 and to display it by title ascending which is
$SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
$imdb = $db_field['c09'];
$run = $db_field['c11'];
$row = mysql_fetch_assoc($run);
$sum = $row['value_sum'];
Now I want to echo $sum; to add up all values of c11 from all the results that matched playcount of equal or greater then 1. The reason I haven't tried the SQL sum is because below I am displaying more columns which i believe is the reason for select *
edit: if i do the following:
$db_handle = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
$imdb = $db_field['c09'];
$run = $db_field['c11'];
echo $run;
it does display the times of all the items with a playcount of 1 or more which is good. now im just just wish to make a total of all those $run values in 1 sum and echo that. i also have extra tables that are called in the php file, just a note, like mid, idb, and c00.
now, if i do :
$db_handle = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "select SUM(c11) AS totalrun from movieview WHERE playCount>=1 Order By c00 Asc ";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
$imdb = $db_field['c09'];
$run = $db_field['totalrun'];
i am able to echo the totals, yey!, but i cant call on the other columns listed above.
If i understood well your question, you can do this:
$counter=0;
while ( $db_field = mysql_fetch_assoc($result) ) {
$imdb = $db_field['c09'];
$run = $db_field['c11'];
$row = mysql_fetch_assoc($run);
$sum = $row['value_sum'];
$counter .= $row['c11'];
}
echo $counter;
If you just keep on adding the total run it should work , If I have understood it correctly
$db_handle = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "select * from movieview WHERE playCount>=1 Order By c00 Asc";
$result = mysql_query($SQL);
$run=0;
while ( $db_field = mysql_fetch_assoc($result) ) {
$imdb = $db_field['c09'];
$run = $run + $db_field['c11'];
}
echo $run;
i was able to do it by creating another sql query. not sure how proper it is but it works. the only thing missing is that each result is only counted 1x so if a playcount of 2 or more is only seen as 1. will work on that later unless you can reply with a fix :D
while ( $db_field = mysql_fetch_array($result) ) {
$run = $db_field['SUM(c11)'];
I'm trying to optimize this check I have. I need to check table called lines and see if any row has matching Earned and Maxearned values (only rows with Position 1,2,3,4). If they do, I need to grab Earned from that row, write it in a different table called bank and remove that row from table called lines. This is what I have:
$sql3 = "SELECT * FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned";
$result3 = mysql_query($sql3);
if (mysql_num_rows($result3) != 0)
{
while ($row3 = mysql_fetch_array($result3))
{
$users[] = $row3['User'];
}
foreach ($users as $user)
{
$sql6 = "SELECT * FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned AND User = '$user'";
$result4 = mysql_query($sql6);
while ($row4 = mysql_fetch_array($result4))
{
$earned = $row4['Earned'];
}
$today = date("Y-m-d");
$method = "Queue money";
$type = "Earned";
$status = "Completed";
$sql4 = "INSERT INTO bank (User,Amount,Method,Transdate,Type,Status) VALUES ('$user','$earned','$method','$today','$type','$status')";
$sql5 = "DELETE FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned AND User = '$user'";
}
$sql7 = "UPDATE `lines` SET Position = Position - 1 WHERE Linenum = '$linenum'";
}
I'm trying to avoid having to run a different query ($sql6 and the while after that) to grab the value of Earned column. Is there a way to do this? I've tried everything and this is pretty much the best I came up with.
You can do something like this:
mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");
$sql3 = "SELECT * FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned";
$result3 = mysql_query($sql3);
if (mysql_num_rows($result3) != 0)
{
while ($row3 = mysql_fetch_array($result3))
{
$users[] = $row3['User'];
}
$users_to_compare = "(" . rtrim(implode(",", $users),',') . ")";
$sql4 = "SELECT * FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned AND User IN $users_to_compare";
$result4 = mysql_query($sql4);
while ($row4 = mysql_fetch_array($result4))
{
$earned = $row4['Earned'];
$today = date("Y-m-d");
$method = "Queue money";
$type = "Earned";
$status = "Completed";
$sql5 = "INSERT INTO bank (User,Amount,Method,Transdate,Type,Status) VALUES ('{$row4['User']}','$earned','$method','$today','$type','$status')";
$result5 = mysql_query($sql5);
}
$sql6 = "DELETE FROM `lines` WHERE Position <= 4 AND Linenum = '$linenum' AND Earned = Maxearned AND User IN $users_to_compare";
$result6 = mysql_query($sql6);
$sql7 = "UPDATE `lines` SET Position = Position - 1 WHERE Linenum = '$linenum'";
$result7 = mysql_query($sql7);
if ($result5 && $result5 && $result7) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
}
}
Going one step further you can also use Batch INSERT for you insert queries
Note: Dont forget that mysql_* versions are depreceated you should use mysqli_*
I have a script that allows an admin to view orders that customers have made, where they can decide to view the order details or delete the order. A user can order 1 or more items, for which entries in the database are created with the same order_id, but with different product_id's. When I display my orders these duplicate entries show up, however I only want 1 entry for every order_id. Below is my function
function viewOrdersAdmin(){
//This block grabs the orders
$order_list = "";
//Selecting all the orders in the table from that member
$sql = mysql_query("SELECT * FROM `transactions`") or die(mysql_error());
while ($transactions = mysql_fetch_array($sql)) {
//creating variables from the information
$order_id = $transactions["order_id"];
$mem_id = $transactions["mem_id"];
$order_details = mysql_query("SELECT * FROM `transactionDetails` WHERE `order_id` = $order_id") or die(mysql_error());
$orderDetailsCount = mysql_num_rows($order_details);
while ($row = mysql_fetch_array($order_details)) {
//creating variables from the information
$order_product_id = $row["Product_ID"];
$member_details = mysql_query("SELECT * FROM `members` WHERE `mem_id` = $mem_id") or die(mysql_error());
$memberDetailsCount = mysql_num_rows($member_details);
while ($row2 = mysql_fetch_array($member_details)) {
//creating variables from the information
$order_mem_fname = $row2["mem_first_name"];
$order_mem_lname = $row2["mem_last_name"];
$order_list .= "Order ID:$order_id - Customer Name: $order_mem_fname $order_mem_lname <a href='manage_order.php?orderid=$order_id'>View</a> • <a href='manage_orders.php?deleteid=$order_id'>Delete</a><br/>";
}
}
}
if (count($orderDetailsCount) == 0) {
$order_list = "You have no orders to display";
}
print_r($order_list);
}
SELECT *
FROM transactions
GROUP BY order_id