Jump to content

Mysql skips first entry?


Moopsish
Go to solution Solved by Ch0cu3r,

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,

Edited by Moopsish
Link to comment
Share on other sites

  • Solution

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.