Trouble in updating a row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

Trouble in updating a row

Post by Spectrum_tr »

Hi. I have a trouble but first, i will write my code below :

Code: Select all

<html>
<body>

<?php
	
$link = mysql_connect('localhost', 'root')
   or die('Could not connect: ' . mysql_error());

mysql_select_db('arda') or die('Could not select database');

$dept_ID = $_POST['dept_id'];
$dept_NAME = $_POST['dept_name'];
$man_ID = $_POST['man_id'];
$loc_ID = $_POST['loc_id'];

$emp_ID = $_POST['emp_id'];
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$p_number = $_POST['p_number'];
$h_date = $_POST['h_date'];
$j_ID = $_POST['j_id'];
$sal = $_POST['sal'];
$com_pct = $_POST['com_pct'];
$mang_ID = $_POST['mang_id'];
$deprt_ID = $_POST['deprt_id'];

echo " <P><STRONG>Departments Table </STRONG></P> ";
echo " <TABLE width=700 border=1> ";
  echo "  <TR> ";
    echo " <TD width=75><STRONG>DEPARTMENT_ID</STRONG></TD> ";
    echo " <TD width=75><STRONG>DEPARTMENT_NAME</STRONG></TD> ";
    echo " <TD width=75><STRONG>MANAGER_ID</STRONG></TD> ";
    echo " <TD width=75><STRONG>LOCATION_ID</STRONG></TD> ";
    echo " <TD width=150>&nbsp;</TD> ";
  echo " </TR> ";

	$query = "insert into departments values ( '$dept_ID' , '$dept_NAME' , '$man_ID' , '$loc_ID' )";

	mysql_query($query);
	
	$query2 = "Select * from departments";

	$result = mysql_query($query2);

	while($row = mysql_fetch_row($result))
	{
		echo "<TR>";
		echo "<TD>$row[0]</TD>";
		echo "<TD>$row[1]</TD>";
		echo "<TD>$row[2]</TD>";
		echo "<TD>$row[3]</TD>";
		echo "<TD>[<a href=update.php>update</a>][<a href=remove.php>remove</a>]</TD>";		
		echo "</TR>";
	}

echo " </TABLE> ";

echo " <P><a href=\"insert_d.php\">insert a row into departments table </a></P><HR> ";

echo " <P><STRONG>Employees Table </STRONG></P> ";
echo " <TABLE width=1300 border=1> ";
  echo " <TR> ";
    echo " <TD width=80><STRONG>EMPLOYEE_ID</STRONG></TD> ";
    echo " <TD width=80><STRONG>FIRST_NAME</STRONG></TD> ";
    echo " <TD width=80><STRONG>LAST_NAME</STRONG></TD> ";
    echo " <TD width=80><STRONG>EMAIL</STRONG></TD> ";
    echo " <TD width=80><STRONG>PHONE_NUMBER</STRONG></TD> ";
    echo " <TD width=80><STRONG>HIRE_DATE</STRONG></TD> ";
    echo " <TD width=80><STRONG>JOB_ID</STRONG></TD> ";
    echo " <TD width=80><STRONG>COMMISION_PCT</STRONG></TD> ";
    echo " <TD width=80><STRONG>MANAGER_ID</STRONG></TD> ";
    echo " <TD width=80><STRONG>DEPARTMENT_ID</STRONG></TD> ";
    echo " <TD width=160>&nbsp;</TD></TR> ";

	$query3 = "insert into employees values ( '$emp_ID' , '$f_name' , '$l_name' , '$email' , '$p_number' ,
						  '$h_date' , '$j_ID' , '$sal' , '$com_pct' , '$mang_ID' , '$deprt_ID' )";

	mysql_query($query3);

	$query4 = "Select * from employees";

	$result2 = mysql_query($query4);

	while($row2 = mysql_fetch_row($result2))
	{
		echo "<TR>";
		echo "<TD>$row2[0]</TD>";
		echo "<TD>$row2[1]</TD>";
		echo "<TD>$row2[2]</TD>";
		echo "<TD>$row2[3]</TD>";
		echo "<TD>$row2[4]</TD>";
		echo "<TD>$row2[5]</TD>";
		echo "<TD>$row2[6]</TD>";
		echo "<TD>$row2[7]</TD>";
		echo "<TD>$row2[8]</TD>";
		echo "<TD>$row2[9]</TD>";
		echo "<TD>[<a href=update.php>update</a>][<a href=remove.php>remove</a>]</TD>";	
		echo "</TR>";
	}
    
echo " </TABLE> ";
echo " <P><a href=\"insert_e.php\">insert a row into employees table </a></P> ";

?>

</body>
</html>

i have a database with 2 tables : employees and departments. i can do the insertion every time. when i click the insert link below, i can add a row (by posting with html and adding it to the database) and return back to indexx.php again. and when i return to indexx.php, the row that i add is shown. it shows me the contents of the table everytime.

but i have a problem. i have a put two links on the right of each row, to update or remove a row. but i dont know how to do it? i mean in my code, i use :

Code: Select all

while($row = mysql_fetch_row($result))
{
	echo "<TR>";
	echo "<TD>$row[0]</TD>";
	echo "<TD>$row[1]</TD>";
	echo "<TD>$row[2]</TD>";
	echo "<TD>$row[3]</TD>";
	echo "<TD>[<a href=update.php>update</a>][<a href=remove.php>remove</a>]</TD>";		
	echo "</TR>";
}
But the $row is changing in every loop. but i have just one update.php. How can the "update.php" will understand that the row that it will update is that row?

But on the right of the all rows there is this update link. When i press to one of them, how can it understand that its coming from the second row for example?

Thank you for your help...
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

do somtin like

Code: Select all

"<TD>[<a href=update.php?id=".$row['id'].">update</a>][<a href=remove.php?id=".$row['id'].">remove</a>]</TD>";
then on the update or remove page do this

Code: Select all

$id = mysql_real_escape_string($_GET['id']);
$sql = 'select * from table_name where id = "'.$id.'"';//or do a delete query on the remove thing
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

Post by Spectrum_tr »

Thank you so much for your help. I did it. I have one last question. A confirmation box should appear before i delete the row. How can i create a confirmation box with php?
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

Post by Spectrum_tr »

anybody can answer my last question?
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

either put it on a "page 2" in between , or use Javascript popup alert bot that allows you to say "yes" or "no" for JavaScript, I have no idea- Ive never used it for confirmations - I always did mine in the "next page" and resubmitted on confirm
Post Reply