Jump to content

For loop for a query from mysql table


portabletelly

Recommended Posts

Hi all I dont kno if I can explain what I am after correctly but anyway here I go.

 

I have a backend php page that lets me update information in a mysql table.

 

The web page has several forms on the same page one after the other the only difference between the forms is the part of the sql query where it says id = 1.

 

What I am trying to accomplish is

 

a for loop

where if I have a mysql table with 5 rows, with data that starts at id 1 through to id 5.

I would like the for loop to print out the form data as below until it reaches the last id.

 

I also need the ids in the sql query to change so this would need to be a variable of some sorts.

 

Can someone point me in the right direction as how this can be accomplished if googled it but cant really find what I am after.

 

<form method="post" action="processhp.php"> 
<br>Article Name:<br><input type="Text" name="heading" value ="<?php $db =mysql_connect("localhost", "*******", "*******");
mysql_select_db("aandstec_aandstech",$db);
$result = mysql_query("SELECT * FROM ******* WHERE id = '1'",$db); 

while($myrow = mysql_fetch_array($result)) 
{ 

echo $myrow["article"]; 


} ?>

">
<br>
ID:<br><input type="Text" name="id" value="1" readonly >
<br>
Data:<br><TextArea type="Text" name="data" rows="3" cols="50"><?php

$db =mysql_connect("localhost", "*******", "*******");
mysql_select_db("*******",$db);
$result = mysql_query("SELECT * FROM personnel WHERE id = '1'",$db); 

while($myrow = mysql_fetch_array($result)) 
{ 

echo $myrow["data"]; 


} 

?></textarea>
<input type="Submit" name="submit" value="Update"> </form>

Link to comment
https://forums.phpfreaks.com/topic/178223-for-loop-for-a-query-from-mysql-table/
Share on other sites

Something like this?

 

$r = mysql_query("SELECT t.`id`,t.`article`,p.`data` FROM `*******` as t LEFT JOIN `personnel` as p ON t.`id`=p.`id` WHERE t.`id` BETWEEN 1 AND 5");
while($rr = mysql_fetch_assoc($r)) {
    echo "Article Name: <input type='text' name='heading' value='$rr[article]' /><br />".
         "ID: <input type='text' name='id' value='$rr[id]' readonly /><br />".
         "Data: <textarea type='text' name='data' rows='3' cols='50'>$rr[data]</textarea><hr />";
}

 

It's difficult to figure out exactly what you're trying to achieve from your description and code, but this might get you on the right track, maybe?

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.