My problem is similar to here: PHP Questions and Answers - Page 750 - Bytes … -php-mysql except I changed my code to have one button at the top instead of one at the end of every row, to make things easier for the users.
I take values from a MYSQL table and print them into an HTML table using PHP, in a while loop. On EVERY row that is printed out there are two columns that the user can interact with: a dropdown box, and a text input. They can change the value of the dropdown box and enter text in the text input.
At the very top of my page I have an ‘Update’ button, which I would like to connect to a MYSQL UPDATE query to update every record depending on the conditions I set.
The problem is, at the minute I have put the form tags around the two fields in the while loop, so basically multiple forms with the same name will be generated if more than record is in the table and is displayed. The only other thing I can think of doing is to encase the majority of my code inside form tags, but I still wouldn’t understand how to update the fields for each record (each row displayed).
I hope this makes sense, please tell me if not!
Here is my code (which clearly isn’t working for updating the records. Some of the MYSQL at the top is rough and was written whilst I was trying to figure out how I would update every row, so also wont do anything):
<?php
session_start();
require("connect.php");
if (!isset($_SESSION['email']))
header('Location: index.php');
//setting the user id
$useremail = $_SESSION['email'];
$useridquery = mysql_query("SELECT userid FROM users WHERE email = '$useremail'");
$userid = mysql_fetch_row($useridquery);
$userid = $userid[0];
//checking lead status
$newstatus = $_POST['statusdropdown'];
$statusquery = mysql_query("SELECT status FROM leads WHERE userid = $userid");
$oldstatus = mysql_fetch_row($statusquery);
$oldstatus = $oldstatus[0];
//update button submission
if ($submit)
{
//$statusupdate = "UPDATE leads SET status WHERE $newstatus != $oldstatus";
//$commentsupdate = "UPDATE leads SET comments WHERE comments!=''";
//mysql_query($statusupdate, $commentsupdate);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" href="leadsboxes.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset-utf-8" />
<title>Dashboard | Lead management</title>
</head>
<body id="leads">
<div id="container"/>
<div id="header">
<div id="accountinfo">
<table border="0" align="right" cellpadding="2" cellspacing="0">
<tr>
<td nowrap="nowrap">
<div id="email"><?php echo $_SESSION['email']; ?></div><!--end email-->
</td>
<td nowrap="nowrap">|</td>
<td nowrap="nowrap"><a href="help.php">Help</a></td>
<td nowrap="nowrap">|</td>
<td nowrap="nowrap"><a href="logout.php">Logout</a></td>
<td nowrap="nowrap"> </td>
</tr>
</table>
</div><!--end accountinfo-->
<img src="images/logo.png" id="logo" alt="Lead management" /><!--end logo-->
<div id="search">
<form method="get" id="searchleads" action="">
<fieldset class="search">
<input type="text" class="box" />
<button class="btn" title="Submit Search">Search</button>
</fieldset>
</form>
</div><!--end search-->
<div id="navigation">
<a id="dashboardnav" href="dashboard.php" title="Dashboard"><span>Dashboard</span></a>
<a id="leadsnav" href="leads.php" title="Leads"><span>Leads</span></a>
<a id="accountnav" href="account.php" title="Account"><span>Account</span></a>
</div><!--end navigation-->
</div><!--end header-->
<div id="content">
<div id="allleads">
<div class="maincssbox">
<div class="maincssbox_head">
<div id='updatebutton'><input type="submit" name="Submit" value="Update" /></div><!--end updatebutton-->
<h2> Leads</h2>
</div><!--end cssbox-->
<div class="maincssbox_body">
<div id="allleadstable">
<?php
$submit = $_POST['submit'];
$result = mysql_query("SELECT * FROM leads WHERE userid = $userid");
echo "<table border ='1' width='910'>";
echo "<tr>";
echo "<th width='50'>Date</th>";
echo "<th width='30'>Title</th>";
echo "<th width='120'>Name</th>";
echo "<th width='40'>DOB</th>";
echo "<th width='120'>Email</th>";
echo "<th width='100'>Phone</th>";
echo "<th width='150'>Address</th>";
echo "<th width='80'>Status</th>";
echo "<th width='100'>Comments</th>";
echo "</tr>";
while ($row = mysql_fetch_array($result))
{
//Assign variables
$leadid = $row['leadid'];
$displaydate = $row['dateadded'];
$displaytitle = $row['title'];
$displayname = $row['forename']." ".$row['surname'];
$displaydob = $row['dobday']."/".$row['dobmonth']."/".$row['dobyear'];
$displayemail = $row['email'];
$displayphone = $row['phone'];
$displayaddress = $row['housenumber']." ".$row['street']." ".$row['postcode'];
//Status dropdown box
$statusvalue = $row['status'];
if ($statusvalue=='New')
{
$displaystatus = '<select name="statusdropdown"><option value="New" selected="selected">New</option><option value="Contacted">Contacted</option><option value="Qualified">Qualified</option><option value="Converted">Converted</option><option value="Nurturing">Nurturing</option><option value="Bad Lead">Bad lead</option></select>';
}
elseif ($statusvalue=='Contacted')
{
$displaystatus = '<select name="statusdropdown"><option value="New">New</option><option value="Contacted" selected="selected">Contacted</option><option value="Qualified">Qualified</option><option value="Converted">Converted</option><option value="Nurturing">Nurturing</option><option value="Bad Lead">Bad lead</option></select>';
}
elseif ($statusvalue=='Qualified')
{
$displaystatus = '<select name="statusdropdown"><option value="New">New</option><option value="Contacted" >Contacted</option><option value="Qualified" selected="selected">Qualified</option><option value="Converted">Converted</option><option value="Nurturing">Nurturing</option><option value="Bad Lead">Bad lead</option></select>';
}
elseif ($statusvalue=='Converted')
{
$displaystatus = '<select name="statusdropdown"><option value="New">New</option><option value="Contacted">Contacted</option><option value="Qualified">Qualified</option><option value="Converted" selected="selected">Converted</option><option value="Nurturing">Nurturing</option><option value="Bad Lead">Bad lead</option></select>';
}
elseif ($statusvalue=='Nurturing')
{
$displaystatus = '<select name="statusdropdown"><option value="New">New</option><option value="Contacted">Contacted</option><option value="Qualified">Qualified</option><option value="Converted">Converted</option><option value="Nurturing" selected="selected">Nurturing</option><option value="Bad Lead">Bad lead</option></select>';
}
elseif ($statusvalue=='Bad lead')
{
$displaystatus = '<select name="statusdropdown"><option value="New">New</option><option value="Contacted">Contacted</option><option value="Qualified">Qualified</option><option value="Converted">Converted</option><option value="Nurturing">Nurturing</option><option value="Bad Lead" selected="selected">Bad lead</option></select>';
}
//Comments field
$commentsvalue = $row['comments'];
$displaycomments = "<input type='text' maxlength='255' name='commentsfield' value=\\"$commentsvalue\\" />";
//the table contents
echo "<tr>";
echo "<td>";
echo "$displaydate";
echo "</td>";
echo "<td>";
echo "$displaytitle";
echo "</td>";
echo "<td>";
echo "$displayname";
echo "</td>";
echo "<td>";
echo "$displaydob";
echo "</td>";
echo "<td>";
echo "$displayemail";
echo "</td>";
echo "<td>";
echo "$displayphone";
echo "</td>";
echo "<td>";
echo "$displayaddress";
echo "</td>";
echo "<td>";
echo "<form id ='changes' action='leads.php' method='POST'>$displaystatus";
echo "</td>";
echo "<td>";
echo "$displaycomments</form>";
echo "</td>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</div><!--end allleadstable-->
</div><!--end cssbox_head-->
</div><!--end cssbox_body-->
</div><!--end leads-->
</div><!--end content-->
<div id="footer">
<a href="dashboard.php"> Dashboard </a>|<a href="leads.php"> Leads </a>|<a href=""> Account </a><p id="footercopyright">Copyright © 2011</p>
</div><!--end footer-->
</div><!--end container-->
</body>
</html>
Thanks in advance for any help!