Moopsish Posted February 3, 2014 Share Posted February 3, 2014 (edited) Hi, First post here I have a problem.. My Mysql database seems to skip the first entry completely.. IE: you have the database: ----------------- ID | entry | ----------------- 1 | This | ---------------- 2 | is | ----------------- 3 | annoying| ----------------- outputs: is, annoying if it helps here's the part of my code I use to generate/display the data: $sql = mysqli_query($link, "SELECT * FROM foto"); $row = mysqli_fetch_assoc($sql); $array = array(); echo "<form method=\"POST\"> <input type=\"hidden\" name=\"id[]\" value=\"".$row['id']."\">"; while ($row = mysqli_fetch_assoc($sql)) { echo "<input type=\"text\" name=\"foto[]\" value=".htmlspecialchars($row['foto'])."></input>"; } echo "<input type=\"submit\" name=\"submit3\" value=\"update\"></input></form>"; thanks, Edited February 3, 2014 by Moopsish Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 3, 2014 Share Posted February 3, 2014 $row = mysqli_fetch_assoc($sql); why do you have the above line, at line #2, in the posted code? Quote Link to comment Share on other sites More sharing options...
Moopsish Posted February 3, 2014 Author Share Posted February 3, 2014 (edited) why do you have the above line, at line #2, in the posted code? Thats so I can echo the Id in line #5. It's the way I learned it.. is there a better way? Edited February 3, 2014 by Moopsish Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted February 3, 2014 Solution Share Posted February 3, 2014 (edited) The while loop wont show the first row because you are calling mysqli_fetch_assoc() on line #2. This is because each time you fetch a row with mysqli_fetch_*() it'll return the next row in the result set. What is the purpose of this code? Is so you can edit multiple records at the same time? If this is the case then you'd setup your form differently. I'd set the record id as the key to foto[]. Example code $sql = mysqli_query($link, "SELECT * FROM foto"); echo "<form method=\"POST\">"; while ($row = mysqli_fetch_assoc($sql)) { echo "<p>#{$row['id']} <input type=\"text\" name=\"foto[{$row['id']}]\" value=".htmlspecialchars($row['foto'])." /></p>"; } echo "<input type=\"submit\" name=\"submit3\" value=\"update\" /> </form>"; To update all records your code would be if(isset($_POST['submit3'])) { $stmt = mysqli_prepare($link, 'UPDATE foto SET foto=? WHERE id=?'); mysqli_stmt_bind_param($stmt, 'is', $row_id, $row_value); // bind id and foto value foreach($_POST['foto'] as $row_id => $row_value) { mysqli_stmt_execute($stmt); // update row } } Edited February 3, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Moopsish Posted February 3, 2014 Author Share Posted February 3, 2014 @ch0cu3r thank you so much! the purpuse is indeed to update multiple records. and I've been working on that for awhile now. thank you for helping me get further into this code -Moopsish Quote Link to comment 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.