Solved

php code question

Posted on 2003-11-02
10
208 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:smozgur
ID: 9667877
Your update.php cannot get "id" field value.

Suat
0
 
LVL 17

Expert Comment

by:smozgur
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:
smozgur 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:smozgur
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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:smozgur
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:smozgur
ID: 9668213
Hi source2k3,

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

Suat
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

19 Experts available now in Live!

Get 1:1 Help Now