Jump to content

Need help with php forms and mysql


MaryamJamil

Recommended Posts

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 by requinix
please use [code] tags when posting code
Link to comment
Share on other sites

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

  • 1 year later...
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.