Nymphetamine Posted August 23, 2008 Share Posted August 23, 2008 Hey guys, Below is some code which I thought I had working.... <?php $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_assoc($result)){ if ($row['booked'] == 'yes') { echo "<td bgcolor=lightblue><b>Booked</b><br>$row[name]<br><form action=edit.php method=post><input type=hidden name=date value=$row[date]><input type=submit value=View/Edit></td>"; } elseif ($row['booked'] == 'no') { echo "<td bgcolor=lightgray>Empty<br><form action=edit.php method=post><input type=hidden name=date value=$row[date]><input type=submit value=Add></td>"; } } ?> This populates a table with either 'Booked' with their name, or 'Empty' ... depending on the data in the database. The button is meant to post the date to the following page, and this seemed to work at first, but now the following page is getting seemingly random dates. Here's what it looks like... <?php $date = $_POST['date']; mysql_connect("######", "########", "#########") or die(mysql_error()); mysql_select_db("########") or die(mysql_error()); $query = "SELECT * FROM ####### WHERE date = '" . $date . "'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "<form name=###### method=post action=update.php>"; echo "<tr bgcolor=white><td>"; echo "Property:<br><input type=text name=property value=Chateau readonly><p>"; echo "</td><td>"; echo "Date:<br><input type=text name=date value=$date readonly>"; echo "</td><td>"; ... As I said, it seemed to post the date from the correct row at first, but now I'm getting seemingly random dates... and therefore the following 'update.php' edits the wrong row in the database!! For the life of me I cannot figure out why... Some help from you PHP freaks would be great Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/ Share on other sites More sharing options...
eldorik Posted August 23, 2008 Share Posted August 23, 2008 Try this <?php $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_assoc($result)){ if ($row['booked'] == 'yes') { echo '<td bgcolor=lightblue><b>Booked</b><br>'.$row['name'].'<br><form action="edit.php" method="post"><input type="hidden" name="date" value="'.$row['date'].'"><input type="submit" value="View/Edit"></td>'; } elseif ($row['booked'] == 'no') { echo '<td bgcolor="lightgray">Empty<br><form action="edit.php" method="post"><input type="hidden" name="date" value="'.$row['date'].'"><input type="submit" value="Add"></td>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623694 Share on other sites More sharing options...
Nymphetamine Posted August 23, 2008 Author Share Posted August 23, 2008 Nope haha, Using '. .' seems to be invalid. It didn't find any data in my database haha... I think its a problem with it finding the wrong row, since its getting the date from a different row in my database. Eg. when I specify that the hidden input's value is "value=$row[date]", it gets the date from a different row, and not the one its currently on! Oh dear... ??? Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623704 Share on other sites More sharing options...
Nymphetamine Posted August 23, 2008 Author Share Posted August 23, 2008 Bump. I'm really stuck people Any ideas on why it might be retrieving the date from a different row? Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623735 Share on other sites More sharing options...
kenrbnsn Posted August 23, 2008 Share Posted August 23, 2008 Do a "show source" on the generated HTML to see what the script is generating. You may have spaces in some of the variables which will screw up your form, since you don't quote any of the value attributes in your HTML tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623740 Share on other sites More sharing options...
Nymphetamine Posted August 23, 2008 Author Share Posted August 23, 2008 Hi Ken, I did a show source, the html looks good, in the first cell it says the hidden value is "0501" which is what it should be. This makes me think there's something wrong with my edit.php to make it appear as "2712" all the time. <?php $date = $_POST['date']; mysql_connect("######", "########", "#########") or die(mysql_error()); mysql_select_db("########") or die(mysql_error()); $query = "SELECT * FROM ####### WHERE date = '" . $date . "'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "<form name=###### method=post action=update.php>"; echo "<tr bgcolor=white><td>"; echo "Property:<br><input type=text name=property value=Chateau readonly><p>"; echo "</td><td>"; echo "Date:<br><input type=text name=date value=$date readonly>"; echo "</td><td>"; ... Hmmm.... Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623745 Share on other sites More sharing options...
Nymphetamine Posted August 23, 2008 Author Share Posted August 23, 2008 Oh I think I get it! It's getting the POST from the last one executed! Which is why it seemed random earlier, since the page hadn't fully loaded it got the last one to be executed haha... I guess I'll need to do this a different way, any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623746 Share on other sites More sharing options...
MasterACE14 Posted August 23, 2008 Share Posted August 23, 2008 with this: <?php if ($row['booked'] == 'yes') { echo '<td bgcolor=lightblue><b>Booked</b><br>'.$row['name'].'<br><form action="edit.php" method="post"><input type="hidden" name="date" value="'.$row['date'].'"><input type="submit" value="View/Edit"></td>'; } elseif ($row['booked'] == 'no') { echo '<td bgcolor="lightgray">Empty<br><form action="edit.php" method="post"><input type="hidden" name="date" value="'.$row['date'].'"><input type="submit" value="Add"></td>'; } If the only 2 values for booked are yes or no, you dont need the elseif, just put a else <?php else { Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623748 Share on other sites More sharing options...
kenrbnsn Posted August 23, 2008 Share Posted August 23, 2008 You're going to have to start putting in debug statements to dump values. Looking at the code you posted doesn't help. Put <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?> at the top of the processing script to dump what is being sent from the form. Print out your query, etc... Ken Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623749 Share on other sites More sharing options...
Nymphetamine Posted August 23, 2008 Author Share Posted August 23, 2008 Right, MasterAce, I copied and pasted that exactly, but exactly the same porrrblem! Ken, Array ( [date] => 2712 ) It should be posting 0501, not 2712, which is the last date in the loop. I also tried giving the form a unique name (I used the date), I thought that would give the form a different name each time it looped through it and thus solving the problem but no Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623755 Share on other sites More sharing options...
Nymphetamine Posted August 23, 2008 Author Share Posted August 23, 2008 ROFL!! I forgot to close the form at the end with: </form> Thanks for your help though guys! All this time and all it was was that there's no </form> at the end........... I could cry. Infact I will. Quote Link to comment https://forums.phpfreaks.com/topic/120985-solved-this-is-so-confusing/#findComment-623761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.