• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

php code question

Hi there.  I have a database that I've just set up using php/mysql.  I have the 'add record' page working well and a page to display the contents of the database also working well.

My problem is now making an 'edit record' page(s).

I've made 2 pages for updating.  The first is 'update.php' which is a simple page displaying the current records in a basic form which allows you to edit the current records.  This page then sends the changed info to 'updated.php' which updates the database with the changes (in theory).  

My problem is that updated.php isn't sending the changes to the database properly.  I confirmed that update.php is sending the proper info to updated.php by setting up a temporary echo for each variable received on the updated.php page.  

The problem is that the code I'm using for updated.php isn't taking those variables and sending them to mysql in a format it can use.

Anyway, I'll post my code here.  The table is called 'main' with columns: name, state, country, web, email.

I'd appreciate any help possible.

# begin update.php (first page with form)

<?
$user="myusername";
$password="mypassword";
$database="mydatabasename";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM main ORDER BY name ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$id=$_GET['id'];
$name=mysql_result($result,$i,"name");
$state=mysql_result($result,$i,"state");
$country=mysql_result($result,$i,"country");
$web=mysql_result($result,$i,"web");
$email=mysql_result($result,$i,"email");
?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
<input type="text" name="ud_name" value="<? echo "$name" ?>">Breeder Name<br>
<input type="text" name="ud_state" value="<? echo "$state" ?>">2 Letters For State<br>
<input type="text" name="ud_country" value="<? echo "$country" ?>">Country, Default is USA.<br>
<input type="text" name="ud_web" value="<? echo "$web" ?>">Web Site<br>
<input type="text" name="ud_email" value="<? echo "$email" ?>">E-mail Address<br>
<input type="Submit" value="Update">
</form>
<?
++$i;
}
mysql_close();
?>

# -- end update.php--

# -- begin updated.php--
<?
$user="myusername";
$password="mypassword";
$database="mydatabasename";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$ud_id=$_POST['ud_id'];
$ud_name=$_POST['ud_name'];
$ud_state=$_POST['ud_state'];
$ud_country=$_POST['ud_country'];
$ud_web=$_POST['ud_web'];
$ud_email=$_POST['ud_email'];

$query="UPDATE main SET name = '$ud_name',state = '$ud_state',country = '$ud_country',web = '$ud_web',email = '$ud_email' WHERE id = '$ud_id'";

mysql_query($query);
mysql_close();
?>
Breeder Updated Successfully.
<br><br>
Click <a href="update.php">here</a> to update another.
<br><br>
Click <a href="add.html">here</a> to add another.
<br><br>
Click <a href="index.php">here</a> to view the breeder list.
0
source2k3
Asked:
source2k3
  • 6
  • 4
1 Solution
 
Suat OzgurWeb / Application DeveloperCommented:
Your update.php cannot get "id" field value.

Suat
0
 
Suat OzgurWeb / Application DeveloperCommented:
"id" is also a field in your table, right?

Suat
0
 
source2k3Author Commented:
Yes, I tried using the tutorial here:
http://www.freewebmasterhelp.com/tutorials/phpmysql/7

but the code they suggest doesn't work.  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Suat OzgurWeb / Application DeveloperCommented:
Change

$id=$_GET['id'];

code line in update.php with the following:

$id=mysql_result($result,$i,"id");

It will save the record that the one you clicked the button below it.

Actually I'd rather retrieve one record in a page instead listing all in the same page. But it is your decision.

Suat
0
 
Suat OzgurWeb / Application DeveloperCommented:
Because that tutorial assumes that you are editing one record (page has only one record so id value comes from the "opener" php code).

But your code lists all records in the table. It is why it doesn't work.

My changing helps it to work but I still suggest selecting an id and opening it first to edit instead listing all records then letting user to edit one and click the button under that record.

Suat
0
 
source2k3Author Commented:
Thanks, that fixed it.  

I'm not sure how you mean to open just one id.  What changes would I make to do that?

Sorry, I'm still pretty new to this.
0
 
Suat OzgurWeb / Application DeveloperCommented:
No problem.

I would make a form to open a specific "id" record ; by using following SQL string:

query = "SELECT * FROM main WHERE id = " . $_POST['id'];

$_POST['id'] is supposed to come from another "id selection" php code.

Same tutorial has a discussion about this:  Selecting Single Record

http://www.freewebmasterhelp.com/tutorials/phpmysql/6

Suat
(Thanks for the grade)
0
 
source2k3Author Commented:
I actually had just figured it out a moment after I posted that.

I used $query="SELECT * FROM main WHERE id='$id'";

and use update.php?id=

Now, I need to figure out how to add an edit link on the admin page I'm going to make.  I'm going to make it a table with each record and an edit link next to it if I can figure out how to.

I think it's something along the lines of adding a variable like $xyz = mysql_fetch_assoc($result); and making a link next to the id in the table for something like <a href="update.php?id=<?php echo $xyz['id'];?>">Edit</a>

Am I close?
0
 
source2k3Author Commented:
Never mind.  I figured it out.  I made an admin page with:

<?
$user="myusername";
$password="mypassword";
$database="mydatabasename";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM main ORDER BY name ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();
?>
<img src=banner5.jpg><br><br>

<b><center>The Labrador Retriever Breeder Database</b><br><br></center>

<div align="center">
<table border="0" cellspacing="2" cellpadding="2" width=85% style="border-collapse: collapse">
<tr>
<td><font face="Arial, Helvetica, sans-serif" size="1"><a href="state.php">View by State</a></td></table>
<table border="2" cellspacing="2" cellpadding="3" width=85% style="border-collapse: collapse"

bordercolor="#111111">
<tr>
<th bgcolor="#003366"><font face="Arial, Helvetica, sans-serif" color="#FFFFFF">Breeder</font></th>
<th bgcolor="#003366"><font face="Arial, Helvetica, sans-serif"color="#FFFFFF">Location</font></th>
<th bgcolor="#003366"><font face="Arial, Helvetica, sans-serif"color="#FFFFFF">Web Site</font></th>
<th bgcolor="#003366"><font face="Arial, Helvetica, sans-serif"color="#FFFFFF">E-mail</font></th>
<th bgcolor="#003366"><font face="Arial, Helvetica, sans-serif"color="#FFFFFF">Update</font></th>
</tr>
<?
$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"name");
$state=mysql_result($result,$i,"state");
$country=mysql_result($result,$i,"country");
$web=mysql_result($result,$i,"web");
$email=mysql_result($result,$i,"email");
$update=mysql_result($result,$i,"id");
?>
<center>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><b><? echo "$name"; ?></font></b></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$state, $country"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="<? echo "$web"; ?>">Website</a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo "$email"; ?>">E-mail</a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="update.php?id=<? echo "$update";

?>">Update</a></font></td>
</center>
</tr>

<?
++$i;
}


echo "</table>";
?>

##EOF**
Thanks again for the help.
0
 
Suat OzgurWeb / Application DeveloperCommented:
Hi source2k3,

Sorry, I was away for a while. But it seems you already solved it :)
Nice job!

Suat
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now