I need to make an if else statement that will prevent a user from bidding on an item if they are the highest bidder. I think it would go a little something like this.
if ($accountid = the accountid in the bidhistory table)
{
echo "You are the highest bidder!";
}
else
{
$sql="INSERT INTO bidhistory (accountid, biditemid)
VALUES ($accountid, $itemid)";
mysql_query("
UPDATE bidhistory
SET bidprice = bidprice + 1
WHERE biditemid = " .
#mysql_escape_string($itemid));
$result=mysql_query($sql) or die("Error in adding bid for item: ".mysql_error());
}
?>
I'm not sure how to reference the accountid from the bidhistory table. Also, if there is a better way to do this, please point me in the wright direction. Thanks!
Note:
You can fetch the row of the highest bid with the specific item first.
Then check if the highest bidder (accountid) is the same with the current user.
You can check the notes I had for some lines quoted in /* ... */
Your query for checking:
$result = mysql_query("SELECT accountid FROM bidhistory WHERE biditem = '$itemid' ORDER BY bidhistoryid DESC LIMIT 1"); /* GET THE LAST ROW FOR WHO BIDS LAST; AND REPLACE NECESSARY COLUMN NAME (unique/primary) - bidhistoryid */
while($row = mysql_fetch_array($result)){
$checkaccountid = $row['accountid']; /* STORE THE USER THAT BIDS LAST FOR THIS ITEM */
}
if($checkaccountid == $accountid){ /* THEN COMPARE IT WITH THE CURRENT USER */
/* CODE YOU WANT TO DO IF HE/SHE IS THE LAST BIDDER ALREADY */
}
else {
/* IF NOT, HE/SHE CAN STILL BID */
}
But I recommend that you use mysqli_* rather than the deprecated mysql_* API.
$con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */
/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($stmt = $con->prepare("SELECT accountid FROM bidhistory WHERE biditem = ? ORDER BY bidhistoryid DESC LIMIT 1")){
$stmt->bind_param("i",$itemid); /* BIND THIS VARIABLE TO YOUR QUERY ABOVE */
$stmt->execute(); /* EXECUTE THE QUERY */
$stmt->bind_result($checkaccountid); /* STORE THE RESULT TO THIS VARIABLE */
$stmt->fetch(); /* FETCH THE RESULT */
if($checkaccountid == $accountid){
/* CODE YOU WANT TO DO IF HE/SHE IS THE LAST BIDDER ALREADY */
}
else {
/* IF NOT, HE/SHE CAN STILL BID */
}
$stmt->close();
} /* END OF PREPARED STATEMENT */
Related
I have something that may seem very simple for some people but for the life of me I cannot figure it out (and I've been trying all day long). I have simple DB from which I am trying to get SUM by the client by date. All works fine to the last moment where I add SUM(item) to SELECT - from this point I cannot echo anything. Can anyone help me what is wrong with my code below:
<?php
$q_billing = intval($_GET['q_billing']);
$dblink = mysqli_connect("localhost", "User", "Pass", "Exam_2018");
/* If connection fails throw an error */
if (mysqli_connect_errno()) {
echo "Could not connect to database: Error: ".mysqli_connect_error();
exit();
}
$sqlquery = "SELECT id, client_id, SUM(item) AS total_sales, date
FROM test
WHERE date BETWEEN '".$q_billing."' AND '2018-12-31'
GROUP BY client_id ";
if ($result = mysqli_query($dblink, $sqlquery)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo $row["client_id"]." ".$row['total_sales']."<br />";
}
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close
?>
Just a note that q_billing is DATE, which I tried to replace with constant 2018-01-01.
I tried also adding ORDER BY, but it does not make any difference. I cannot see any error either - just a blank div where the echo should be.
Thank you for any help
if you group by client_id then you should not select id and date
$sqlquery = "SELECT client_id, SUM(item) AS total_sales
FROM test
WHERE date BETWEEN '".$q_billing."' AND '2018-12-31'
GROUP BY client_id ";
I'm using MySQL to show data on my website: http://www.onlinedealsindia.in. You can see the deals (deal boxes) there but I want to limit them. I want only 50 deals (deal boxes) to show up per page. Clicking the more button would show the next 50 deals.
Below is my code to get data from MySQL:
<?php
$host="localhost";
$username="username";
$password="pass";
$dbname="DB NAme";
$conn=new mysqli($host, $username, $password, $dbname);
if($conn->connect_error)
{
die("error".$conn->connect_error);
}
else { echo "";}
$sql= "SELECT * FROM table ORDER BY p_id DESC";
$results=$conn->query($sql);
if($results->num_rows > 0)
{ while($row=$results->fetch_assoc())
{ $pid=$row["p_id"];
?>
The mechanic you are looking for is called skip and take.
In mySQL you can use the LIMIT to accomplish this. With one parameter it returns that many rows. With two parameters, it will skip the first number in rows and return the number of rows for the second number.
This SQL would return 20 rows:
$sql= "SELECT * FROM table LIMIT 10 ORDER BY p_id DESC";
This SQL would return the ten rows skipping the first 10
$sql= "SELECT * FROM table LIMIT 20,10 ORDER BY p_id DESC";
Documentation and examples of LIMIT can be found here
To make this work for your website just pass a parameter that tells what page you are on and how many rows per page. You can then calculate the two numbers you need for your select statement.
$mysqli = new mysqli($host, $username, $password, $dbname);
if($mysqli->connect_error)
{
die("error".$mysqli->connect_error);
}
else {
echo "";
}
$datas = $mysqli->prepare('SELECT id FROM table LIMIT 50 ');
if($datas)
{
$datas->execute();
$datas->bind_result($id);
while($datas->fetch)
{
$store['id']= $id;
$results[] = $store;
}
return $results;
}
foreach($results as $result)
{
echo $result['id'];
}
I am trying to select a row in one table and if it does exists in the second table,do something and if it doesn't,copy the values from table one into the second.
The problem is that,once it finds match (a row that is present in the first and second table),it shows errors for all other rows that did not match.
This is the error
Notice: Trying to get property of non-object in C:\wamp\www\loans.php on line 26
This is my code
<?php
//error_reporting(0);
$mysqli = new mysqli("localhost", "root", "123456", "test");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "select dest_msisdn,text_message,service_id,sender_name from incoming_sms";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$dest_msisdn = $row['dest_msisdn'];
$text_message = $row['text_message'];
$service_id = $row['service_id'];
$sender_name = $row['sender_name'];
/**
Transactions
*/
$m = $mysqli->query("SELECT tel from transactions where tel = $dest_msisdn")->fetch_object()->message;
if(empty($m)){
$ti = "insert into transactions(message,tel) values($text_message,$dest_msisdn)";
$mysqli->query($ti);
}
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
I want to insert rows that i find in table one and are not in table two.
I suggest you check the rows yielded before accessing any properties that the result yields:
$m = $mysqli->query("SELECT tel, message from transactions where tel = $dest_msisdn");
// hi! im missing ^^
// you are accessing the property "message" but its not a selected column in your query
if($m->num_rows <= 0) {
$ti = "insert into transactions(message,tel) values($text_message,$dest_msisdn)";
$mysqli->query($ti);
}
When you chain like that you are assuming there will always be a row. When there isn't a row you cant get the property from it. That's why you get the error. So do something like this:
$r = $mysqli->query("SELECT tel from transactions where tel = $dest_msisdn");
if ($r->num_rows == 0) {
$ti = "insert into transactions(message,tel) values($text_message,$dest_msisdn)";
$mysqli->query($ti);
}
This doesn't answer your question directly, but it will fix your problem and probably solve some others.
The way you are going about inserting (where you first SELECT and then loop through the results in PHP, then SELECT from another table, then INSERT) seems unnecessarily complex. You can get rid of your PHP loop and just execute the INSERT in a single SQL statement without all the PHP overhead. Let your DB do the work for you.
All of this:
$query = "select dest_msisdn,text_message,service_id,sender_name from incoming_sms";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$dest_msisdn = $row['dest_msisdn'];
$text_message = $row['text_message'];
$service_id = $row['service_id'];
$sender_name = $row['sender_name'];
/**
Transactions
*/
$m = $mysqli->query("SELECT tel from transactions where tel = $dest_msisdn")->fetch_object()->message;
if(empty($m)){
$ti = "insert into transactions(message,tel) values($text_message,$dest_msisdn)";
$mysqli->query($ti);
}
}
/* free result set */
$result->free();
Could be changed to just this:
$mysqli->query("INSERT INTO transactions(message,tel) SELECT text_message, tel FROM transactions INNER JOIN incoming_sms on transactions.tel = incoming_sms.dest_msisdn")
code below is a part of my sitemap.xml file. My aim is to write the last modified date of my approved articles. There are 2 possibilities for this data.
If article has no approved comment, then lastmod is the approval
date of the article.
If article has at least 1 approved comment, then lastmod is the
approval date of the last approved comment.
Errors in my output are:
There are some articles in my db that has no comments but also these commentless articles get the approval date of last comment of some other articles. As a result since I have some comments which are approved today, all those commentless articles' lastmode is date of today. but these articles are quite old.
$newsql in my 2nd while loop prints "newstmt prepare error" on
screen, so if ($newstmt = $connection->prepare($newsql)) part doesn't work
Can you please correct me?
Thank you, best regards
code
<?php
//if spesific article is approved then its last modification date = approval date of article
//if spesific article has approved comment(s), then modification date = approval date of its last comment
$sql = "SELECT col_author, col_title, col_approvaldate FROM articles WHERE col_status = ? ORDER by col_approvaldate DESC";
if ($stmt = $connection->prepare($sql))
{
/* bind parameters */
$stmt -> bind_param("s", $bindparam1);
/* assign value */
$bindparam1 = 'approved';
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($author, $title, $articledate);
/* fetch values */
while ($stmt->fetch())
{
//if exist, get approved newest comment approval date
$newsql = "SELECT col_approvaldate FROM comments WHERE col_status = ? AND col_for_author = ? AND col_for_title = ? ORDER by col_approvaldate DESC LIMIT 1";
if ($newstmt = $connection->prepare($newsql))
{
/* bind parameters */
$newstmt -> bind_param("sss", $ybindparam1, $ybindparam2, $ybindparam3);
/* give values */
$ybindparam1 = 'approved';
$ybindparam2 = $author;
$ybindparam3 = $title;
/* execute statement */
$newstmt->execute();
/* bind result variables */
$newstmt->bind_result($commentdate);
/* fetch values */
$biggerdate = '';
while ($newstmt->fetch())
{
// is there any approved comment for this article?
if (!is_null($commentdate))
{$biggerdate = $commentdate;}
}
/* close statement */
$newstmt->close();
}
else {echo 'newstmt prepare error';}
//print the result
echo '<url>'."\r\n";
echo "\t".'<loc>'.root_folder.urlencode('articles').'/'.urlencode(space_to_dash($author)).'/'.urlencode(space_to_dash(no_punctuation($title))).'</loc>'."\r\n";
//if there is no approved comment for this article
if ($biggerdate == '')
{
$biggerdate = $articledate;
}
$datetime = new DateTime($biggerdate);
$biggerdate = $datetime->format('Y-m-d\TH:i:sP');
echo "\t".'<lastmod>'.$biggerdate.'</lastmod>'."\r\n";
echo '</url>'."\r\n";
}
/* close statement */
$stmt->close();
}
?>
How it have to be
You have to get your data using single query.
Comments have to be linked using article id
PDO instead of mysqli ought to be used
So, here you go:
$sql = "SELECT author, title, a.approvaldate, max(c.approvdate) biggerdate
FROM articles a
LEFT JOIN comments c ON c.article_id = a.id AND c.status='approved'
WHERE a.status = 'approved'
GROUP BY a.id
ORDER BY a.approvaldate DESC";
$stmt = $con->prepare($sql);
$stmt->execute();
$data = $stmt->fetchall();
foreach ($data as $row) {
extract($row);
echo "<url>\r\n";
echo "\t<loc>".root_folder.'articles';
echo '/'.urlencode(space_to_dash($author));
echo '/'.urlencode(space_to_dash(no_punctuation($title)))."</loc>\r\n";
//if there is no approved comment for this article
if (!$biggerdate)
{
$biggerdate = $approvaldate;
}
$datetime = new DateTime($biggerdate);
$biggerdate = $datetime->format('Y-m-d\TH:i:sP');
echo "\t<lastmod>$biggerdate</lastmod>\r\n";
echo "</url>\r\n";
}
it is of course not tested and apparently contains many errors but just to show you an ides.
First of all you have to make query work.
then make read linked PDO tag wiki and establish a connection.
Then fix all the errors and typos, if any
EDIT: I'm sorry I was unclear, I try to explain it right this time.
I have this data in a database table called tMenu:
id page_nl text
1 index_1 index1_text
2 index_2 index2_text
3 index_3 index3_text
These are 3 pages on my website called (in this case) index_1, index_2 and index_3. I have programmed it is such a way that each page shows there index1_text.
What I want now is to show page_nl in a menu. The code I have now is:
$qh = mysql_query('SELECT id, page_nl FROM tMenu ORDER BY id');
$row = mysql_fetch_array($qh);
$id = 'id';
<? echo $row['page_nl']; $id=="1" ;?>
<? echo $row['page_nl']; $id=="2" ;?>
<? echo $row['page_nl'];?>
In the way it is now it shows only page_nl from id 1, but I want that the next link shows page_nl from id 2. I hope my question is more clear now.
Your question isn't very clear - are you asking for something like this
$sql = "select * from yourtable where id = 1";
$result = mysql_query($sql);
//I am assuming there are more than 1 rows for ID 1
while($row = mysql_fetch_assoc($result)) {
echo $row['page_nl'];
}
OR ============================
$sql = "select * from yourtable"; //Select All
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
if($row['id'] == 1)
{
echo $row['page_nl'];
}
}
Presuming you mean database table, you need a routine to connect to the database then fetch the info:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "databasename"); // database name
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM table_name"; // put table name here
$result = $mysqli->query($query);
/* numeric array */
/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["id"], $row["page_nl"]);
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>
You need to use a foreach($var as $key =>$value) loop