cbender165 cbender165 - 1 month ago 17
MySQL Question

First row of query glitch?

I'm working on a webpage... Using HTML, CSS, PHP, and MySQL..

I'm using a while loop to fetch rows and echoing to display result. I also echo a button on each row named "edit" which takes the user to another page that allows the user to edit that single row and update it. A weird thing is happening... Everything works fine, except for the first row of my query result. If I click on the button, it doesn't take me to my next page, however, all other rows work correctly... I'm stumped.. I've used basically the same code elsewhere in other pages, and the button works fine. However, not for this one.. Below is my code, and some screenshots..

<form>
<center>
<?php
$sql = "SELECT id,course,a,b,c,d,e,f,g,h,i,j,k,l,m,n FROM CoursesToSOs";
$result = mysqli_query($db,$sql);

echo "<table id=\"t01\">";

echo "<tr>";
echo "<th>Course</th>";
echo "<th>a</th>";
echo "<th>b</th>";
echo "<th>c</th>";
echo "<th>d</th>";
echo "<th>e</th>";
echo "<th>f</th>";
echo "<th>g</th>";
echo "<th>h</th>";
echo "<th>i</th>";
echo "<th>j</th>";
echo "<th>k</th>";
echo "<th>l</th>";
echo "<th>m</th>";
echo "<th>n</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$id = $row['id'];
$course = $row['course'];
$a = $row['a'];
$b = $row['b'];
$c = $row['c'];
$d = $row['d'];
$e = $row['e'];
$f = $row['f'];
$g = $row['g'];
$h = $row['h'];
$i = $row['i'];
$j = $row['j'];
$k = $row['k'];
$l = $row['l'];
$m = $row['m'];
$n = $row['n'];

echo "<td>".$course."</td>";
echo "<td>".($a==1 ? 'X' : '')."</td>";
echo "<td>".($b==1 ? 'X' : '')."</td>";
echo "<td>".($c==1 ? 'X' : '')."</td>";
echo "<td>".($d==1 ? 'X' : '')."</td>";
echo "<td>".($e==1 ? 'X' : '')."</td>";
echo "<td>".($f==1 ? 'X' : '')."</td>";
echo "<td>".($g==1 ? 'X' : '')."</td>";
echo "<td>".($h==1 ? 'X' : '')."</td>";
echo "<td>".($i==1 ? 'X' : '')."</td>";
echo "<td>".($j==1 ? 'X' : '')."</td>";
echo "<td>".($k==1 ? 'X' : '')."</td>";
echo "<td>".($l==1 ? 'X' : '')."</td>";
echo "<td>".($m==1 ? 'X' : '')."</td>";
echo "<td>".($n==1 ? 'X' : '')."</td>";
echo "<td><form action='EditCourseToSos.php' method='post'><input type='hidden' name='tempid' value='".$row['id']."'/><input type='submit' name='submit-btn' value='edit' /></form></td></tr>";
}
echo "</table>";
?>
</br>
<input type='submit' name='submit-btn' value='Submit'/>
</center>

</form>


After first edit button is pressed..
After a different edit button is pressed, it brought to next page which is correct..

This is super frustrating, anyone have any idea??

Thanks

sg- sg-
Answer

Don't nest HTML forms. That is, don't put a form within a form (like you have). Fix this and you will find it will work.

Take the tag you have on line 1 and move it after the table (ie: after all the other forms)

Comments