Link to home
Start Free TrialLog in
Avatar of bschwarting
bschwarting

asked on

php loop, multiple sql updates

Ok, I'm open to suggestions, angel :), on my project.  I've hit another roadblock and might be approaching this whole thing the wrong way.  I was trying to create a small footprint of code and loop it all, but can't seem to get everything to work.

I want to print out 365 checkboxes (see attached image), then update a table in the database with the answer for 1(yes) or 0(no).  I'm not sure if doing 365 updates all at once is the best idea or not, for each of my users.

My end game is this:
#1) print out 365 checkboxes
#2) check the individual users table to see which checkbox is already a yes, and check it for them (maybe with a value=$checked ?)
#3) allow my users to select/deselect one, or all at the same time and click submit, and update accordingly

Here is the code I have so far, which will print the table, create all the answer variables, then it will update the 1st box that is checked and nothing more for some reason.

any ideas would be appreciated!
<?php
 
//GET VARIABLES FROM SUBMISSION ON SAME PAGE
$answer1 = $_POST['answer1']; 
$answer2 = $_POST['answer2']; 
$answer3 = $_POST['answer3']; 
$answer4 = $_POST['answer4']; 
$answer5 = $_POST['answer5'];
$answer6 = $_POST['answer6']; 
$answer7 = $_POST['answer7']; 
$answer8 = $_POST['answer8']; 
$answer9 = $_POST['answer9']; 
$answer10 = $_POST['answer10'];
 
 
//DAILY READINGS
$daily1reading = ', example, read page 1';
$daily2reading = ', example, read page 2';
$daily3reading = ', example, read page 3';
$daily4reading = ', example, read page 4';
$daily5reading = ', example, read page 5';
$daily6reading = ', example, read page 6';
$daily7reading = ', example, read page 7';
$daily8reading = ', example, read page 8';
$daily9reading = ', example, read page 9';
$daily10reading = ', example, read page 10';
 
$date365t = mktime(0, 0, 0, 1, 0, date(Y));
$date365 = date("Y-m-d", $date365t);
 
?>
 
<form method="post" action="365-days">
 
<?php
 
echo '<table width=650><tr>';
for($loop365=1;$loop365<=365;$loop365++) {
  if(($loop365-1)%15==0) echo '</tr><tr>'; ?>
  <td align="center"><span title="<?php 
  echo "Daily reading for ".date('F jS',mktime(0,0,0,1,$loop365)); $dailyreading = "daily{$loop365}reading"; echo $$dailyreading; echo " (Day $loop365)";
$date365t += 86400;
$date365 = date("Y-m-d", $date365t);
 
echo "#"; echo ${$answerloop365}; echo "#"; 
?>"><?php echo $loop365; ?></span></td>
<td align="center">
<input type='checkbox' value="0" title="<?php 
  echo "Daily reading for ".date('F jS',mktime(0,0,0,1,$loop365)); $dailyreading = "daily{$loop365}reading"; echo $$dailyreading; echo " (Day $loop365)"; ?>" value='1' name="answer<?php echo ($loop365+1); ?>" />
</td><?php
 
$answerloop365 = ${'answer'.$loop365};
 
if ( $answerloop365 == '1' ) {
$date1 = date('Y-m-d');
$sql1 = "UPDATE $createtablename SET date='$date1', answer='$answerloop365' WHERE id = ( '$loop365' );";
mysql_query($sql1) or die ("Error in query: $sql1. ".mysql_error());
echo "<b>INSERTED</b>";
} else {
echo "NOT READY TO INSERT";
}
 
}
echo '</tr></table>';
 
?>
 
<table width="650" border="0">
<tr>
<td>
<br>
<input type="submit" value="Update All 365 Days Now!"><input type="Reset">
</form>
</td>
</tr>
</table>

Open in new window

Capture.PNG
Avatar of profya
profya
Flag of Sudan image

Error line 49: $$dailyreading
ASKER CERTIFIED SOLUTION
Avatar of profya
profya
Flag of Sudan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bschwarting
bschwarting

ASKER

profya, thanks for this!  when I try to use it, I get the white screen of death.  I've looked through all your code and can't find the error.  maybe you can assist?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that works great!

I'm missing two things now:

#1) Yours is doing the same thing, it's updating the next record instead of the record selected (see above where #1 updated id #2, should be id #1)
#2)  Once this is in the database, I need the script when it is first pulled up to see which of these are already a "1" and check them off
maybe something like this?

select id, answer from $createtablename where answer = 1

if answer = 1
$checked = checked
} else {
$checked = ' '

then make the input a variable for selected

<input type='checkbox' $checked />
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
profya, for some reason i keep getting a bunch of records that won't update.  what is it looking at before it updates?
Excuse me, I didn't get your point. When this happens? When you click on the submit button? Try echoing the sql statement and see the logic, is there anything missing? specifically the WHERE clause.
When the date field is blank in the database, it will take the submit fine and update.  If there is a date in the database already, it won't update.
can you help me with #2?  not sure I am following.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, that makes sense on the update routine.  

#2 works great!  you are a genius!

my last issue, #3, is my only remaining issue!  right now, when the page is 1st brought up, and all the boxes are checked, if the user has made a mistake, and wants to deselect the box, it never updates the answer to 0.  what is the best strategy for that?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Genius, pure genius!