Page 1 of 1

Variable Identification

Posted: Wed Sep 01, 2010 7:59 pm
by neesley
I have an HTML table filled with data from a MySQL table. I'd like to be able to edit this data and update the MySQL table from the HTML table.

How do I identify the particular MySQL row after PHP has built the HTML table?

Here is my code that builds the HTML table.

Code: Select all

$result = mysql_query("SELECT * FROM employees ORDER BY personID ASC");

	echo "<table class='box-table-a'>
	<tr>
	<th>Number</th>
	<th>First</th>
	<th>Last</th>
	<th>Phone</th>
	<th>Email</th>
	<th>Rate</th>
	<th>Type</th>
	</tr>";
	
	while($row = mysql_fetch_array($result))
		{
		echo "<tr>";
		echo "<td>" . $row['personID'] . "</td>";
		echo "<td>" . $row['first_name'] . "</td>";
		echo "<td>" . $row['last_name'] . "</td>";
		echo "<td>" . $row['phone'] . "</td>";
		echo "<td>" . $row['email'] . "</td>";
		echo "<td>" . $row['pay'] . "</td>";
		echo "<td>" . $row['paytype'] . " %</td>";
		echo "</tr>";
		}
	echo "</table>";

Re: Variable Identification

Posted: Wed Sep 01, 2010 11:00 pm
by requinix
Depends what your <form> looks like. Which I don't see.

Re: Variable Identification

Posted: Wed Sep 01, 2010 11:16 pm
by neesley
I have the form popping up via Javascript. Here's the code for the entire page:

Code: Select all

<?php

$con = mysql_connect("xxxx","xxxx","xxxx");
if (!$con)
	{
	die('Could not connect: ' . mysql_error());
	}

mysql_select_db("neesleysqltest", $con);

if (isset($_POST['submit'])) {
 
$upd = "UPDATE employees SET pay = '$_POST[pay]' WHERE personID ='???????'";

$result=mysql_query($upd,$con);

if(!$result) {
 die('Error: ' . mysql_error());
}

header("Location: staff.php");

}

else 
{
?>

<div id="popupbox2">
<form name="replace" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<center>New Rate:</center>
<center><input name="pay" size="14"/></center>
<br/>
<center><input type="submit" name="submit" value="Update Info" /></center>
<input type="hidden" name="ID" value="???????"/>
</form>
<br/>
<center><a href="javascript:replace('hide');">close</a></center> <!-- Closes the box-->
</div>

<?php

$result = mysql_query("SELECT * FROM employees ORDER BY personID ASC");

echo "<table class='box-table-a'>
	<tr>
	<th>Number</th>
	<th>First</th>
	<th>Last</th>
	<th>Phone</th>
	<th>Email</th>
	<th>Rate</th>
	<th>Type</th>
	</tr>";
	
while($row = mysql_fetch_array($result))
	{
	echo "<tr>";
	echo "<td>" . $row['personID'] . "</td>";
	echo "<td>" . $row['first_name'] . "</td>";
	echo "<td>" . $row['last_name'] . "</td>";
	echo "<td>" . $row['phone'] . "</td>";
	echo "<td>" . $row['email'] . "</td>";
	echo "<td><a href=\"javascript:replace('show');\">" . $row['pay'] . "</a></td>";
	echo "<td>" . $row['paytype'] . " %</td>";
	echo "</tr>";
	}
echo "</table>";
}
mysql_close($con);
?>
I think I can accomplish this with multiple pages, but I'd rather keep it all in one document. Hence the Javascript popup etc.

Re: Variable Identification

Posted: Thu Sep 02, 2010 12:59 am
by neesley
I figured it out!

A little extra Javascript in the popup link did the trick. I had to learn how to pass variables between Javascript, PHP and HTML forms... Check it out!

Code: Select all

<?php

$con = mysql_connect("xxxx,"xxxx","xxxx");
if (!$con)
	{
	die('Could not connect: ' . mysql_error());
	}

mysql_select_db("neesleysqltest", $con);

if (isset($_POST['submit'])) {
 
$upd = "UPDATE employees SET pay = '$_POST[pay]' WHERE personID ='$_POST[ID]'";

$result=mysql_query($upd,$con);

if(!$result) {
 die('Error: ' . mysql_error());
}

header("Location: staff.php");

}

else 
{
?>

<div id="popupbox2">
<form name="replace" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<center>New Rate:</center>
<center><input name="pay" size="14"/></center>
<br/>
<center><input type="submit" name="submit" value="Update Info" /></center>
<input type="hidden" name="ID" value=""/>
</form>
<br/>
<center><a href="javascript:replace('hide');">close</a></center> <!-- Closes the box-->
</div>

<?php

$result = mysql_query("SELECT * FROM employees ORDER BY personID ASC");

echo "<table class='box-table-a'>
	<tr>
	<th>Number</th>
	<th>First</th>
	<th>Last</th>
	<th>Phone</th>
	<th>Email</th>
	<th>Rate</th>
	<th>Type</th>
	</tr>";
	
while($row = mysql_fetch_array($result))
	{
	echo "<tr>";
	echo "<td>" . $row['personID'] . "</td>";
	echo "<td>" . $row['first_name'] . "</td>";
	echo "<td>" . $row['last_name'] . "</td>";
	echo "<td>" . $row['phone'] . "</td>";
	echo "<td>" . $row['email'] . "</td>";
	echo "<td><a href=\"javascript:replace('show');
				jsvar=" . $row['personID'] . ";
				document.replace.ID.value = jsvar;\">" . $row['pay'] . "</a></td>";
	echo "<td>" . $row['paytype'] . " %</td>";
	echo "</tr>";
	}
echo "</table>";
}
mysql_close($con);
?>
Man, that's four hours I'm never getting back.