MaryamJamil Posted June 11, 2014 Share Posted June 11, 2014 (edited) I need help I am retrieving rows from MySQL table now what I want is that as rows retrieved with each row and edit button will insert but i don't know how to insert here is my code <?php require('connect.php'); session_start(); $query="SELECT * FROM project WHERE projectstatus='Open'"; $result=mysql_query($query); $num=mysql_numrows($result); $i=0;$j=1; while ($i < $num) { $projectid=mysql_result($result,$i,"projectid"); $fkey=mysql_result($result,$i,"fkey"); $paying=mysql_result($result,$i,"paying"); $completionmonth=mysql_result($result,$i,"completionmonth"); $completiondate=mysql_result($result,$i,"completiondate"); $personspecify=mysql_result($result,$i,"personspecify"); $projecttitle=mysql_result($result,$i,"projecttitle"); $categories=mysql_result($result,$i,"categories"); $tools=mysql_result($result,$i,"tools"); $shortdescription=mysql_result($result,$i,"shortdescription"); $date=mysql_result($result,$i,"date"); $time=mysql_result($result,$i,"time"); $i++; echo "<br>".$j."<br>"; echo "<br>".$projectid."<br>"; echo "<br>".$fkey."<br>"; echo "<br>".$paying."<br>"; echo "<br>".$completionmonth.' month '.$completiondate.' days '."<br>"; echo "<br>".$personspecify."<br>"; echo "<br>".$projecttitle."<br>"; echo "<br>".$categories."<br>"; echo "<br>".$tools."<br>"; echo "<br>".$shortdescription."<br>"; echo "<br>".$date."<br>"; echo "<br>".$time."<br>"; echo "<br>"."<br>"."<br>"; // I want to insert edit button here but dont know how echo "------------------------------------------------------------------------"."<br>"; $j++; } ?> </body> </html> Edited June 11, 2014 by requinix please use [code] tags when posting code Quote Link to comment https://forums.phpfreaks.com/topic/289117-need-help-with-php-forms-and-mysql/ Share on other sites More sharing options...
Psycho Posted June 11, 2014 Share Posted June 11, 2014 Several issues in that code: 1. You should not use 'external' variables to create a while() loop. Use a while() loop to get one row at a time. Makes the code much, much simpler. What you have now could easily break if the values for $i or $j get modified incorrectly. Not to mention that variables with names like $i and $j provide no context as to what they hold. 2. No reason to call mysql_result() many times instead of just calling a mysql_fetch_ one time. As to your question it can be implemented many ways. You can create ONE form and have a submit button for each record which will pass the ID of the record to your edit page. Or, you can make a mini form for each record. Which one you go with will be determined by several factors. You also need to decide what 'value' is passed to the edit page (I'm assuming it is the fkey). Here is one example: <?php require('connect.php'); session_start(); $query = "SELECT fkey, projectid, paying, completionmonth, completiondate, personspecify, projecttitle, categories, tools, shortdescription, date, time FROM project WHERE projectstatus='Open'"; $result = mysql_query($query); $count = 0; $projectList = ''; while ($row = mysql_fetch_assoc($result)) { $count++; $projectList .= "<br>{$count}<br>\n"; $projectList .= "<br>{$row['projectid']}<br>\n"; $projectList .= "<br>{$row['fkey']}<br>\n"; $projectList .= "<br>{$row['paying']}<br>\n"; $projectList .= "<br>{$row['completionmonth']} month {$row['completiondate']} days <br>\n"; $projectList .= "<br>{$row['personspecify']}<br>\n"; $projectList .= "<br>{$row['projecttitle']}<br>\n"; $projectList .= "<br>{$row['categories']}<br>\n"; $projectList .= "<br>{$row['tools']}<br>\n"; $projectList .= "<br>{$row['shortdescription']}<br>\n"; $projectList .= "<br>{$row['date']}<br>\n"; $projectList .= "<br>{$row['time']}<br><br><br><br>\n"; // I want to insert edit button here but dont know how $projectList .= "<form action='edit.php' method='post'>"; $projectList .= "<input type='hidden' name='fkey' value='{$row['fkey']}'>"; $projectList .= "<button type='submit'>Edit</button>"; ec$projectList .=ho "</form><br>\n"; } ?> <html> <head></head> <body> <?php echo $projectList; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/289117-need-help-with-php-forms-and-mysql/#findComment-1482506 Share on other sites More sharing options...
cyberRobot Posted June 12, 2014 Share Posted June 12, 2014 Note that the following line from Psycho's code has some extra characters: ec$projectList .=ho "</form><br>\n"; It should be: $projectList .= "</form><br>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/289117-need-help-with-php-forms-and-mysql/#findComment-1482529 Share on other sites More sharing options...
MaryamJamil Posted March 12, 2016 Author Share Posted March 12, 2016 mysql will be removed in future. Kindly use mysqli Quote Link to comment https://forums.phpfreaks.com/topic/289117-need-help-with-php-forms-and-mysql/#findComment-1531952 Share on other sites More sharing options...
benanamen Posted March 12, 2016 Share Posted March 12, 2016 mysql will be removed in future. Uhhh, the future is NOW! It mysql_* has ALREADY been removed from Php. Quote Link to comment https://forums.phpfreaks.com/topic/289117-need-help-with-php-forms-and-mysql/#findComment-1531955 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.