Forum Moderators: coopster
1. I would like to be able to select from both the _users tables as well as the _winners table and where the user_id and buyer_id are the same, it will show either the name or the username for that buyer_id from the _users table.
2. When it finds that the payment_status is confirmed and after it has been displayed to me, I need it to update the field named printed that I have added in the _winners table to a value I set (i.e. printed), so the next time I run the script it will not show the rows when the column printed is not blank.
This is what I have so far;
<?php
$con = mysql_connect("localhost","username","pswd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM _winners
WHERE payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
echo "<table border='5'>
<tr>
<th>Auction ID</th>
<th>Bid Amount</th>
<th>Deposit Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['auction_id'] . "</td>";
echo "<td>" . $row['bid_amount'] . "</td>";
echo "<td>" . $row['payment_status'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Let me know if you can help me out with this.
Thanks in advance,
Chris
$result = mysql_query("SEELCT * from _winners AS w INNER JOIN _users AS u on u.user_id = w.buyer_id");
If so then in your while loop you would need another query at the end of it to update.
$q2 = @mysql_query("UPDATE _winner SET printed = 'printed' WHERE auction_id = ".mysql_real_escape_string($r['auction_id'])."");
I'm not sure if there would be a better way to do this.
As you loop through you could possibly store all the auction_ids in a comma delimited string and then use an mysql 'IN' clause to update all rows at once.
e.g.
$string_ids = "";
while($row = mysql_fetch_array($result))
{
$string_ids .= $row['auction_id'].",";
echo "<tr>";
echo "<td>" . $row['auction_id'] . "</td>";
echo "<td>" . $row['bid_amount'] . "</td>";
echo "<td>" . $row['payment_status'] . "</td>";
echo "</tr>";
}
$string_ids = substr_replace($string_ids ,"",-1); //remove last comma$q = @mysql_query("UPDATE _winner SET printed = 'printed' WHERE auction_id IN ($string_ids)");
Obviously test this first as it's something I've only just knocked up.
After the script posted below, please read on to see what else I am trying to do with it.
This is the script I am using, and it is working just as I need it to;
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT *, probid_users.name AS name,
probid_winners.winner_id AS winnerid
FROM probid_winners
left join probid_users ON probid_users.user_id = probid_winners.buyer_id
WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
echo "<table border='5'>
<tr>
<th>Name</th>
<th>Auction ID</th>
<th>Bid Amount</th>
<th>Deposit Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['auction_id'] . "</td>";
echo "<td>" . $row['bid_amount'] . "</td>";
echo "<td>" . $row['payment_status'] . "</td>";
echo "</tr>";
$res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . $row["winnerid"]);
}
echo "</table>";
mysql_close($con);
?>
However, even though the script works as needed., I was still hoping to be able to add three buttons to it.
1st button; I would like to be able to have a button to press to print the page.
2nd button; I would like to be able to have a button that will update the printed tables only after I press it. This way, there won't be a chance that I would accidentally refresh the page and have the script automatically update the printed column.
3rd button; I would like to have a button that will actually process the script. This way, the script is not precessing until I am ready and all of the information is not there the moment I get to the page. I guess that I could manage to so this by a button on an index page that redirects me to the page with the script, but I was hoping to see what I could do about having the process button just be directly on the same page.
Let me know if all of this is possible, and if you wouldn't mind, it would be great if you could give me a working mock-up.
Thanks again, in advance, for all of the help. You guys here are great!
2) For the update button you have 2 options. You can either have another form next to each row that submits, or you could merely use $_GET parameters. The issue with the get parameters is that it would only take someone going to a url to update the status. For creating another form it would look similar to the following:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['auction_id'] . "</td>";
echo "<td>" . $row['bid_amount'] . "</td>";
echo "<td>" . $row['payment_status'] . "</td>";
echo "<td>". <form name=\"update_row\" method=\"post\" action=\"page.php?id=".$row['winnerid']."\">
<input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
<input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
</form>
echo "</tr>";if(isset($_POST['updating_row'])){
$res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['winnerid']));
}
}
3) Again here you could use $_GET or $_POST parameters. So if you have a form at the top of the page with a value like "show_records" you just check if this is set. Just wrap an if statement looking for if this is set around the code that shows the results. If it's not the results wont show. I've tried not to write this part out for you as it is reasonable simple. Obviously I hope you are trying to understand what I've written instead of just copying and pasting. Any questions about what's going on then just reply here and I'm sure someone will reply.
if(isset($_POST['updating_row'])){
$res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['winnerid']));
}
should be
if(isset($_POST['updating_row'])){
$res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row']));
}
Anymore instructions you can supply, even if you have to talk to me as if I am a child, is VERY much appreciated.
Thanks again,
Chris
<form name="form1" method="post" action="">
<label>
<input type="submit" name="update" id="update" value="Update Records">
</label>
<form name="form2" method="get" action="">
<label>
<input type="submit" name="show_records" id="show_records" value="Show Records">
</label>
</form>
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my)db", $con);
if(isset($_GET['show_results'])){
$res = mysql_query("SELECT *, probid_users.name AS name,
probid_winners.winner_id AS winnerid
FROM probid_winners
left join probid_users ON probid_users.user_id = probid_winners.buyer_id
WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC = " . mysql_real_escape_string($_GET['show_results']));
}
echo "<table border='5'>
<tr>
<th>Name</th>
<th>Auction ID</th>
<th>Bid Amount</th>
<th>Deposit Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['auction_id'] . "</td>";
echo "<td>" . $row['bid_amount'] . "</td>";
echo "<td>" . $row['payment_status'] . "</td>";
echo "<td>". <form1=\"update_row\" method=\"post\" action=\"page.php?id=".$row['winnerid']."\">
<input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
<input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
</form>
echo "</tr>";
if(isset($_POST['updating_row'])){
$res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row']));
}
}
echo "</table>";
mysql_close($con);
?>
Another things I've just noticed is that in your while loop you are saying :
while($row = mysql_fetch_array($result))
$result doesn't exist in the code you posted as you used $res. You then use $res again for the next query. I'd change this to something else such as $res_2 so there's no chance of it interfering with the first query
<form name="form1" method="post" action="">
<label>
<input type="submit" name="update" id="update" value="Update Records">
</label>
</form>
<form name="form2" method="get" action="test.php?show_record=1">
<label>
<input type="submit" name="show_results" id="show_results" value="Show Results">
</label>
</form>
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my)db", $con);
if(isset($_GET['show_results'])){
$res = mysql_query("SELECT *, probid_users.name AS name,
probid_winners.winner_id AS winnerid
FROM probid_winners
left join probid_users ON probid_users.user_id = probid_winners.buyer_id
WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC = " . mysql_real_escape_string($_GET['show_results']));
}
echo "<table border='5'>
<tr>
<th>Name</th>
<th>Auction ID</th>
<th>Bid Amount</th>
<th>Deposit Status</th>
</tr>";
while($row = mysql_fetch_array($res_2))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['auction_id'] . "</td>";
echo "<td>" . $row['bid_amount'] . "</td>";
echo "<td>" . $row['payment_status'] . "</td>";
echo "<td>". <form1=\"update_row\" method=\"post\" action=\"test.php?id=".$row['winnerid']."\">
<input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
<input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
</form>
echo "</tr>";
if(isset($_POST['updating_row'])){
$res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row']));
}
}
echo "</table>";
mysql_close($con);
?>
Now, I am getting an error of;
Parse error: syntax error, unexpected '<' in C:\VertrigoServ\www\test\test.php on line 43
this line here is missing a "; at the end ( that includes the double quote by the way)
mysql_select_db("my)db", $con);
if(isset($_GET['show_results'])){
$res = mysql_query("SELECT *, probid_users.name AS name,
probid_winners.winner_id AS winnerid
FROM probid_winners
left join probid_users ON probid_users.user_id = probid_winners.buyer_id
WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC = " . mysql_real_escape_string($_GET['show_results']));
}
echo "<table border='5'>
<tr>
<th>Name</th>
<th>Auction ID</th>
<th>Bid Amount</th>
<th>Deposit Status</th>
</tr>";
while($row = mysql_fetch_array($res_2))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['auction_id'] . "</td>";
echo "<td>" . $row['bid_amount'] . "</td>";
echo "<td>" . $row['payment_status'] . "</td>";
echo "<td><form1=\"update_row\" method=\"post\" action=\"test.php?id=".$row['winnerid']."\">
<input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
<input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
</form>";
echo "</tr>";
if(isset($_POST['updating_row'])){
$res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row']));
}
}
echo "</table>";
mysql_close($con);
?>
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\VertrigoServ\www\test\test.php on line 37
Which would be this line;
while($row = mysql_fetch_array($res_2))
[edited by: chrishawkins at 9:25 pm (utc) on Nov. 30, 2009]
if its
if(isset($_GET['show_results'])){
$res = mysql_query("SELECT *, probid_users.name AS name,
probid_winners.winner_id AS winnerid
FROM probid_winners
left join probid_users ON probid_users.user_id = probid_winners.buyer_id
WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC = " . mysql_real_escape_string($_GET['show_results']));
}
change it to:
if(isset($_GET['show_results'])){
$res = mysql_query("SELECT *, probid_users.name AS name,
probid_winners.winner_id AS winnerid
FROM probid_winners
inner join probid_users ON probid_users.user_id = probid_winners.buyer_id
WHERE probid_winners.payment_status='confirmed' AND printed=''
ORDER BY bid_amount DESC");
}
What I would suggest doing is working in phpmyadmin and typing in your mysql queries into there testing them with variables that you know will work. It should help you debug the problems. I'm presuming it is the main query that is causing issues.
Having written all that out i think it's because your while loop is refrencing the wrong query.
while($row = mysql_fetch_array($res_2))
while($row = mysql_fetch_array($res))