Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Updating a row with an update button.

Posted on 2008-10-06
4
Medium Priority
?
3,185 Views
Last Modified: 2013-12-12
Ok my old piece of code had a drop down box where the name of the user is pulled from the database and all his information was then i could update it. I want to get rid of the drop down and combine it with this page, see script below. if possible could someone help me or tell me how to make my update button, pull the data from the current page it is on then use it to update that user.
<?php  require_once("includes/db_connection.php"); ?>
 
 
<?php
 
	if(isset($_POST['id']))
	{
		$id = $_POST['id'];
 
		$delete = mysql_query("DELETE FROM tblbasicform WHERE id='$id'"); 
	}
	
$sql = "SELECT * from tblbasicform";
$res = mysql_query($sql) or die(mysql_error());
 
echo "<table border=1 align=centre>";
echo "<tr><td>id</td> <td>Name</td><td>Email</td><td>Buissnes Name</td><td>Location</td><td>Free or Paid</td><td>Delete</td><td>Update</td></tr>";
while($row = MYSQL_FETCH_ARRAY($res))
{
echo "<tr><td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['buissnes_name']."</td>";
echo "<td>".$row['location']."</td>";
echo "<td>".$row['type']."</td>";
echo '<td><form action="'.basename($_SERVER['PHP_SELF']).'" method="POST">
<input type="hidden" name="id" value="'.$row['id'].'"><input type="submit" name="button=" id="button" value="Delete Record"></form></td>';
echo "<td>( <a href=form2.php>Update</a> ) </td></tr>";
}
echo "</table><br>"; 
?>
 
<a href="userform.php">Add a new user.</a>
 
///////////////////////////////////////////
THIS IS MY DROP DOWN BOX PAGE IF THIS HELPS
///////////////////////////////////////////
 
<?php  require_once("includes/db_connection.php");
 
 
$sql = "SELECT * from tblbasicform";
$res = mysql_query($sql) or die(mysql_error());
 
echo "<form action=\"form2.php\" method=\"post\">";
echo "<select name=\"name\">";
 
while($row = mysql_fetch_array($res)){
 
 echo "<option>". $row['name'];
        echo "</option>";
	$name = $row['name'];
    $email = $row['email'];
    $location = $row['location'];
	$buissnes_name = $row['buissnes_name'];
	$type = $row['type'];
	$id = $row['id'];
  }
echo "</select><br>";
echo "<input type=submit value=\"Update Record\">";
echo "</form>";
 
?>

Open in new window

0
Comment
Question by:Lee-Bartlett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
webwyzsystems earned 2000 total points
ID: 22651475
You are very close. Notice how your old code puts all the row data between <FORM> and </FORM>. You need to do the same thing for your new code.
Right now, your new code only encapsulates the ID number for the person. Consequently, when you submit the change, there is no data being sent!
So, in your new code, put this line:

<form action="'.basename($_SERVER['PHP_SELF']).'" method="POST">

In the first <TD> tag for the row. That way, your page should have a series of N (where N is the number of rows in the table) forms all setup. Then, when you click Update, all the data should be sent to your update script.

I think that should put you on the right path. Hth!
0
 

Author Comment

by:Lee-Bartlett
ID: 22651634
Slightly confused me, im am new to php so i dont grasp stuff so easy. i tried this and this didnt work, did u mean like this ?
<?php
echo "<table border=1 align=centre>";
echo "<tr><td>id</td> <td>Name</td><td>Email</td><td>Buissnes Name</td><td>Location</td><td>Free or Paid</td><td>Delete</td><td>Update</td></tr>";
while($row = MYSQL_FETCH_ARRAY($res))
{
echo "<tr><td>"<form action="'.basename($_SERVER['PHP_SELF']).'" method="POST">".$row['id'].>""</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['buissnes_name']."</td>";
echo "<td>".$row['location']."</td>";
echo "<td>".$row['type']."</td>";
echo '<td>
<input type="hidden" name="id" value="'.$row['id'].'"><input type="submit" name="button=" id="button" value="Delete Record"></form></td>';
echo "<td>( <a href=form2.php>Update</a> ) </td></tr>";
}
echo "</table><br>"; 
?>

Open in new window

0
 

Author Comment

by:Lee-Bartlett
ID: 22651991
the error im getting is


Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/nexodom/public_html/test/update/updatedelete.php on line 20
0
 

Author Comment

by:Lee-Bartlett
ID: 22661690
I tried somthing like this and that failed, all im trying to do is use the update button go to my update page with the rows data
<?php  require_once("includes/db_connection.php"); ?>
<?php
 
$name = $_POST['name'];
$email = $_POST['email'];
$location = $_POST['location'];
$type = $_POST['type'];
$buissnes_name = $_POST['buissnes_name'];
$id = $_POST['id'];
?>
 
<?php
 
	if(isset($_POST['id']))
	{
		$id = $_POST['id'];
 
		$delete = mysql_query("DELETE FROM tblbasicform WHERE id='$id'"); 
	}
	
 
$sql = "SELECT * from tblbasicform";
$res = mysql_query($sql) or die(mysql_error());
 
echo "<table border=1 align=centre>";
echo "<tr><td>id</td> <td>Name</td><td>Email</td><td>Buissnes Name</td><td>Location</td><td>Free or Paid</td><td>Delete</td><td>Update</td></tr>";
while($row = MYSQL_FETCH_ARRAY($res))
{
echo "<tr><td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['buissnes_name']."</td>";
echo "<td>".$row['location']."</td>";
echo "<td>".$row['type']."</td>";
echo '<td><form action="'.basename($_SERVER['PHP_SELF']).'" method="POST">
<input type="hidden" name="id" value="'.$row['id'].'"><input type="submit" name="button=" id="button" value="Delete Record"></form></td>';
 
 
 
$sql = "SELECT * from tblbasicform";
$res = mysql_query($sql) or die(mysql_error());
 
 
echo "<td><form action="form2.php" method="POST">";
 
while($row = mysql_fetch_array($res)){
 
echo "<input type="hidden" name="name" value="$row['name']">"
echo "<input type="submit" name="button=" id="button" value="Update Record"></form></td>";
 
}
 
 
echo "</tr>";
}
echo "</table><br>"; 
?>
 
<a href="userform.php">Add a new user.</a>

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question