Page 1 of 1

Selecting and editing one row from a returned search

Posted: Mon Apr 12, 2010 6:16 am
by steven.cottom
Hi, I'm new to PHP and web development so please be patient whilst i try to explain my project.

I have created a simple mysql databse which stores all of the enquiry data that the user submits via an html form. (This bit works)

I have a seperate page where the user can search the surname column and return the entire row. (This bit also works)

I now want to add a checkbox to the end of each row in the returned search, and a button at the bottom of the page which will direct the user to a page where they can then update the data and save it.

Here is my search code so far:

Code: Select all

<?php 

mysql_connect ("localhost", "MyUsername", "MyPassword") or die (mysql_error());
mysql_select_db ("enquiries");

$search1 = $_POST['search1'];
$sql = mysql_query("select * FROM enquiry WHERE surname LIKE '%$search1%'");

echo "Your search for <u><strong>$search1</strong></u> returned the following results:<br />";
echo "<br />";
print ('<table style="border-width: thin; border-style: solid; border-color: grey; cellpadding=1; border-width: 2px;">');
print '<tr bgcolor="grey">';
print '<td width="9%"><t><strong>ENTRY DATE</strong></t></td>';
print '<td width="4%"><t><strong>TITLE</strong></t></td>';
print '<td width="9%"><t><strong>FIRST NAME</strong></t></td>';
print '<td width="8%"><t><strong>SURNAME</strong></t></td>';
print '<td width="8%"><t><strong>DOB</strong></t></td>';
print '<td width="10%"><t><strong>ADDRESS 1</t></strong></td>';
print '<td width="10%"><t><strong>ADDRESS 2</t><strong></td>';
print '<td width="10%"><t><strong>ADDRESS 3</t></strong></td>';
print '<td width="10%"><t><strong>POST CODE</t></strong></td>';
print '<td width="20%"><t><strong>EMAIL</t></strong></td>';
print '<td width="11%"><t><strong>SELECT</t></strong></td>';
print '</tr>';

while ($row = mysql_fetch_array( $sql ))
{
	
	print '<tr bgcolor="lightgrey">';
	print '<td width="9%"><t>'.$row['date'].'</t></td>';
	print '<td width="4%"><t>'.$row['title'].'</t></td>';
	print '<td width="9%"><t>'.$row['firstname'].'</t></td>';
	print '<td width="8%"><t>'.$row['surname'].'</t></td>';
	print '<td width="8%"><t>'.$row['dob'].'</t></td>';
	print '<td width="10%"><t>'.$row['address1'].'</t></td>';
	print '<td width="10%"><t>'.$row['address2'].'</t></td>';
	print '<td width="10%"><t>'.$row['address3'].'</t></td>';
	print '<td width="10%"><t>'.$row['postcode'].'</t></td>';
	print '<td width="20%"><t>'.$row['email'].'</t></td>';
	print '<td width="11%" align="center"><form action="update.php" method="post"><input name="check" type="checkbox" value="Yes" /></td>';
	print '</tr>';
}
	print '</table><br />';
	print '<input name="edit" type="button" value="Edit" />';
	print '<input name="letter" type="button" value="Create Letter" /></form>';
?>
I dont understand how to configure the checkbox to select the row.

Any help would be appreciated.

Thanks

Re: Selecting and editing one row from a returned search

Posted: Mon Apr 12, 2010 10:55 am
by katierosy
<input type="checkbox" name=check[<?print $row['id'];?>]>

On submit give print_r($_POST['check']);
you will be able to know how to use foreach($_POST['check'] as $chk){ // update based on id}

Re: Selecting and editing one row from a returned search

Posted: Mon Apr 12, 2010 11:41 am
by roders
I will also add to katierosy reply to move the <form tag, outside the while loop.

Re: Selecting and editing one row from a returned search

Posted: Mon Apr 12, 2010 4:58 pm
by steven.cottom
i'm sorry for being so numb but i still dont understand. Can you try to explain a little more.

Thanks very much for replying.

Re: Selecting and editing one row from a returned search

Posted: Tue Apr 13, 2010 4:38 am
by steven.cottom
roders wrote:I will also add to katierosy reply to move the <form tag, outside the while loop.
Doesn't it need to stay inside the while loop so that the checkbox appears on each returned row?

Thanks

Re: Selecting and editing one row from a returned search

Posted: Tue Apr 13, 2010 4:44 am
by roders
steven.cottom wrote:Doesn't it need to stay inside the while loop so that the checkbox appears on each returned row?
Well the form tag should be outside the while then the checkbox tag should be inside the while loop. Using like katierosy wrote

Code: Select all

<input type="checkbox" name=check[<?print $row['id'];?>]> 
Using

Code: Select all

foreach($_POST['check'] as $chk){ // update based on id}