473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PHP mysql update limit?

127 100+
Table: cquestions
cqid, cqtext, showdate.

I want to update 'showdate' field to tomorrows date..

database:
Expand|Select|Wrap|Line Numbers
  1.     cqid    cqtext    showdate
  2.     200    q1    2013-05-22
  3.     201    q2    0000-00-00
  4.     202    q3    0000-00-00
the code will display q1. i want to update the 2nd row only to
Expand|Select|Wrap|Line Numbers
  1. 201    q2    2013-05-23
so, tomorrow q2 will be displayed and the third
row will be updated as
Expand|Select|Wrap|Line Numbers
  1. 202    q3    2013-05-24

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $today=date("Y/m/d");
  3. $tomorrow= date("Y-m-d", strtotime("tomorrow"));
  4. echo "<form method='post' id='submit' action='checkresult.php'>";
  5. $sql="SELECT * FROM cquestions where showdate= '$today' limit 1";
  6. $result=mysql_query($sql);
  7. while ($row = mysql_fetch_array($result)) {
  8.     $cqid=mysql_result($result,"cqid");
  9. $update1="update cquestions set showdate='$tomorrow' where showdate='0000-00-00' and cqid!='$cqid' order by cqid limit 1";
  10. mysql_query($update1);
  11.  
  12. echo "<p>" . $row['cqtext'] . "</p>";
  13. $sql2="SELECT * FROM canswers where cqid=".$row['cqid'];
  14. $result2=mysql_query($sql2);
  15. while($row2=mysql_fetch_assoc($result2)){
  16. echo "<input type='radio' name='".$row['cqid']."' value='".$row2['cqans']."' />".$row2['aatext']; }
  17. /*echo "<input type='hidden' name='email' value='email' />";*/
  18. }
  19. echo"<input type='submit' id='submit' name='submit' value='Submit Answers' />";
  20. echo "</form>";
  21. ?>
this code update the next row, but the problem is its executed every time page loads and update the next row... i want to execute the update query only once for the day. how to do it?
May 22 '13 #1
5 2710
Rabbit
12,516 Expert Mod 8TB
Store the last time it was updated and if that was today, don't run the update.
May 22 '13 #2
Luuk
1,047 Expert 1GB
or, only run the update if
"SELECT * FROM cquestions where showdate= '$today'"
did not return a result
(which means there is no record for today...)
May 22 '13 #3
impin
127 100+
i have stored the last_update and checked with the current date. i want to run the update query only once.

updated code:

Expand|Select|Wrap|Line Numbers
  1. $last_update = date("Y/m/d");
  2. if($last_update==$today)
  3. {
  4. $update1="update cquestions set showdate='$tomorrow' where showdate='0000-00-00' and cqid!='$cqid' order by cqid limit 1";
  5. mysql_query($update1);
  6. $update2="update qupdate set last_update='$tomorrow'";
  7. mysql_query($update2);
  8. $sql3="SELECT * FROM qupdate";
  9. $result=mysql_query($sql3);
  10. $last_update=mysql_result($result,"last_update");    
  11. echo "$last_update";
  12. }
last_update was stored in the table ($update2="update qupdate set last_update='$tomorrow'";). first its executing correctly but next time last_update was set to today's date. (($last_update = date("Y/m/d");)) its not set to the last updated date. so the if condition executing all the time. how to fix it?
i want to run the update query only once...
May 23 '13 #4
impin
127 100+
fixed it. but if i change the limit to 2, it doesn't working. only one row is displaying. first two rows should be displayed...

Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT * FROM cquestions where showdate= '$today' limit 1";
Expand|Select|Wrap|Line Numbers
  1. $today=date("Y-m-d");
  2. $sql4="SELECT * FROM qupdate";
  3. $result=mysql_query($sql4);
  4. $last_update=mysql_result($result,"last_update");    
  5. $tomorrow= date("Y-m-d", strtotime("tomorrow"));
  6. echo "$last_update";
  7. echo "<form method='post' id='submit' action='checkresult.php'>";
  8. $sql="SELECT * FROM cquestions where showdate= '$today' limit 1";
  9. $result=mysql_query($sql);
  10. while ($row = mysql_fetch_array($result)) {
  11. $cqid=mysql_result($result,"cqid");    
  12. if($last_update==$today)
  13. {
  14. $update1="update cquestions set showdate='$tomorrow' where showdate='0000-00-00' and cqid!='$cqid' order by cqid limit 1";
  15. mysql_query($update1);
  16. $update2="update qupdate set last_update='$tomorrow'";
  17. mysql_query($update2);
  18. $sql3="SELECT * FROM qupdate";
  19. $result=mysql_query($sql3);
  20. $last_update=mysql_result($result,"last_update");    
  21. echo "$last_update";
  22. }
May 23 '13 #5
Luuk
1,047 Expert 1GB
It fout want to store the last update date in 'qupdate',
Than you should 1st check what the currenlty stored date is ("select ... from qupdate "), and compare that date with today

If the dates are not equal, than you should update 'qupdate' with the date for today.

After above exercise, you should be able to get the (3) questions for today....
May 24 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Titus Cheung | last post by:
Hello, Thought I read somewhere saying that there is a limit to how many connections are available per mySQL account or something like that. Can someone please clarify? I wrote a PHP tool...
2
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I...
1
by: Paul | last post by:
Hello, I know mysql update queries can only utilize one index, making them slow in some cases. My update are taking about 3 hours, joining 2 tables on their primary keys. (table sizes about 1...
1
by: Peter | last post by:
I have a web page with an datagrid which users can update. The update event fires if the datagrid has a relatively few number of rows (eg 100). However if a user selects a dataset with a large...
1
by: Stoyan | last post by:
PHP version 4.4.4 MySQL Version 4.1.21 There is a problem in this code: .... $sql = 'UPDATE Msg2Ptg hdr="Test", dt1="09.08.2007 10:32:14 ", About="xxx " limit 1'; mysql_query($sql) or die...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
3
by: PaulLFC | last post by:
Hi, I am writing a booking system for a Paintball company I work for here in the UK, and I seem to be having trouble with the mysql UPDATE code. Althogh no errors show, the mysql_affected_rows...
1
by: whitep8 | last post by:
Hi All, Can anybody see anything wrong with the following......its a mysql update query, however when ran (and adding vardump) i get nothing added to the database.... the error is......... ...
3
by: fishnfrogs | last post by:
Hi, I can't figure out why this isn't working. I'm trying to loop through an array and do a mysql update. However, it doesn't work. for($i = 0; $i < $len; ++$i) { $param = $array . '%';...
3
by: Reed Remington | last post by:
A normal MySQL UPDATE is written: UPDATE table SET user='Username', pass='Password' WHERE id='Userid' I have about 150+ columns of data that need updated. Hoping for some shorthand. Is there a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.