Jump to content

Mysql skips first entry?


Moopsish

Recommended Posts

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,

Link to comment
https://forums.phpfreaks.com/topic/285907-mysql-skips-first-entry/
Share on other sites

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
	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.