Solved

php code question

Posted on 2003-11-02
10
210 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now