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
Solved

php code question

Posted on 2003-11-02
10
213 Views
Last Modified: 2006-11-17
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
Comment
Question by:source2k3
  • 6
  • 4
10 Comments
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 9667877
Your update.php cannot get "id" field value.

Suat
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 9667887
"id" is also a field in your table, right?

Suat
0
 
LVL 1

Author Comment

by:source2k3
ID: 9667892
Yes, I tried using the tutorial here:
http://www.freewebmasterhelp.com/tutorials/phpmysql/7

but the code they suggest doesn't work.  
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 17

Accepted Solution

by:
Suat Ozgur earned 500 total points
ID: 9667913
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
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 9667930
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
 
LVL 1

Author Comment

by:source2k3
ID: 9667963
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
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 9668014
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
 
LVL 1

Author Comment

by:source2k3
ID: 9668075
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
 
LVL 1

Author Comment

by:source2k3
ID: 9668128
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
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 9668213
Hi source2k3,

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

Suat
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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