OutOfInk Posted March 13, 2014 Share Posted March 13, 2014 I have a while(); statement producing data from the mysql table. But every time I post the data.. It is defaulting to the last value. I'm out of ideas on how to resolve this. When i Check the html source its displaying the data correct but once posted its going to the last value. $start_date = date("Y/m/d", strtotime("- 7 days")); $end_date = date("Y/m/d", strtotime("+ 7 days")); $leader = mysql_query("SELECT information FROM `table` WHERE `Date` > '$start_date' AND `Date` < '$end_date'"); while ($redeal = mysql_fetch_row($leader)){ echo "<input type='hidden' name='round' value='$redeal[6]' />"; echo "<tr class='innertable'>"; echo "<td><input type='radio' name='beton' value='$redeal[0]' /></td>"; echo "<td align=left>$redeal[0]($<b>".number_format($redeal[3], 2)."</b>)</td>"; echo "<td>V</td>"; echo "<td><input type='radio' name='beton' value='$redeal[1]' /></td><td align=left>$redeal[1] ($<b>".number_format($redeal[4], 2)."</b>)</td>"; echo "<td align=left><input type='radio' name='beton' value='Draw' /> $51.00</td>"; echo "<td align=left>$redeal[2]</td>"; echo "<td align=left>$redeal[7]<br />$redeal[8]</td></tr><input type='hidden' name='pwnow' value='$redeal[5]' />"; <- } Quote Link to comment https://forums.phpfreaks.com/topic/286935-hidden-html-element-defaulting-to-last-value/ Share on other sites More sharing options...
jairathnem Posted March 13, 2014 Share Posted March 13, 2014 mysql_fetch_row fetches a single row. use mysql_fetch_assoc instead of that. Even better suggestion - move to PDO instead of mysql. Quote Link to comment https://forums.phpfreaks.com/topic/286935-hidden-html-element-defaulting-to-last-value/#findComment-1472431 Share on other sites More sharing options...
Ch0cu3r Posted March 13, 2014 Share Posted March 13, 2014 (edited) mysql_fetch_row fetches a single row. use mysql_fetch_assoc instead of that. Nonsense. They are both do the same, except mysql_fetch_assoc returns an associative array of field names, rather than a numerically index array @OutOfInk Your form code is correct. The problem is to with how you are naming your input fields. If your form contained one record it is fine, but for having multiple records it is not. This is because you have not named your input fields with a unique name, eg you cant have two fields named the same otherwise the latter field will always override the previous fields value. This is why only the last record is received when form is posted. The solution to is to append square brackets to the field name, eg: $i = 0; while ($redeal = mysql_fetch_row($leader)){ echo "<input type='hidden' name='round[$i]' value='$redeal[6]' />"; echo "<tr class='innertable'>"; echo "<td><input type='radio' name='beton[$i]' value='$redeal[0]' /></td>"; echo "<td align=left>$redeal[0]($<b>".number_format($redeal[3], 2)."</b>)</td>"; echo "<td>V</td>"; echo "<td><input type='radio' name='beton[$i]'' value='$redeal[1]' /></td><td align=left>$redeal[1] ($<b>".number_format($redeal[4], 2)."</b>)</td>"; echo "<td align=left><input type='radio' name='beton[$i]' value='Draw' /> $51.00</td>"; echo "<td align=left>$redeal[2]</td>"; echo "<td align=left>$redeal[7]<br />$redeal[8]</td></tr><input type='hidden' name='pwnow[$i]' value='$redeal[5]' />"; $i++; } You 'll see I added [$i] to the end of the field name, this is to ensure each field carries a unique name and more importantly each record has a unique index which we can use latter on in PHP to retrieve the values for that record. Example of this would be of using a loop like foreach($_POST['round'] as $index => $value) { $round = $value; $beton = $_POST['beton'][$index]; $pwnow = $_POST['pwnow'][$index]; echo "<b>Round:</b> $round - <b>Beton:</b> $beton - <b>Pwnow:</b> $pwnow<br />"; } Edited March 13, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286935-hidden-html-element-defaulting-to-last-value/#findComment-1472432 Share on other sites More sharing options...
jairathnem Posted March 13, 2014 Share Posted March 13, 2014 oops. you are right. thanks for correcting. Quote Link to comment https://forums.phpfreaks.com/topic/286935-hidden-html-element-defaulting-to-last-value/#findComment-1472435 Share on other sites More sharing options...
OutOfInk Posted March 13, 2014 Author Share Posted March 13, 2014 Thanks @Ch0cu3r This is the output i get; Round: 1 - Beton: Draw - Pwnow: 1Round: 1 - Beton: - Pwnow: 2Round: 1 - Beton: - Pwnow: 3Round: 1 - Beton: - Pwnow: 4Round: 1 - Beton: - Pwnow: 5Round: 1 - Beton: - Pwnow: 6Round: 1 - Beton: - Pwnow: 7Round: 1 - Beton: - Pwnow: 8Round: 1 - Beton: - Pwnow: 9 That makes perfect sense, how would extract that first line as that is the line i need. Its radio option as they can only select on option out of the 9 rounds Quote Link to comment https://forums.phpfreaks.com/topic/286935-hidden-html-element-defaulting-to-last-value/#findComment-1472527 Share on other sites More sharing options...
Ch0cu3r Posted March 14, 2014 Share Posted March 14, 2014 (edited) In that case it'll probably be best to revert back to your original code, but to output a new <form> for each record instead while ($redeal = mysql_fetch_row($leader)){ echo "<form action='submit-page.php' method='post' />"; // start a new form for each record echo "<input type='hidden' name='round' value='$redeal[6]' />"; echo "<tr class='innertable'>"; echo "<td><input type='radio' name='beton' value='$redeal[0]' /></td>"; echo "<td align=left>$redeal[0]($<b>".number_format($redeal[3], 2)."</b>)</td>"; echo "<td>V</td>"; echo "<td><input type='radio' name='beton' value='$redeal[1]' /></td><td align=left>$redeal[1] ($<b>".number_format($redeal[4], 2)."</b>)</td>"; echo "<td align=left><input type='radio' name='beton' value='Draw' /> $51.00</td>"; echo "<td align=left>$redeal[2]</td>"; echo "<td align=left>$redeal[7]<br />$redeal[8]</td></tr><input type='hidden' name='pwnow' value='$redeal[5]' />"; echo '<input type="submit" name="submit" value="Submit" />'; // output submit button echo "</form>"; // close form } Edited March 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286935-hidden-html-element-defaulting-to-last-value/#findComment-1472569 Share on other sites More sharing options...
OutOfInk Posted March 15, 2014 Author Share Posted March 15, 2014 By doing this though causes 9 submit buttons instead of just the one though Quote Link to comment https://forums.phpfreaks.com/topic/286935-hidden-html-element-defaulting-to-last-value/#findComment-1472737 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.