Update MySQL table: tiny bug fix needed.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Update MySQL table: tiny bug fix needed.

Post by Dakke »

I'm almost there with my update script. Yet I'm overlooking something, most likely due to the fact that I'm very very new to PHP.

I posted a request today about a ready to go script to update a table using php. Not really satisfied, I tried a tutorial.
Previous post (thanks Micknc). Not that the post is actually 'relevant', but simply to avoid cross-posting:
viewtopic.php?f=1&t=80518


Now I tried this one:
http://www.phpeasystep.com/mysql/9.html. All files are to be found on that page, but for your convenience, I added the files (all the way down). So all credit goes to the url.
It works just fine, except that it does not update the table. It says at the end, successful, but checking up on the data shows no change at all. The update.php works fine (I think) showing the data of the record of your choice. The third (when update.php posts to update_ac.php) seems to work, but actually never receives any data from the update.php
I use Taco to 'code' (if you can call my messy method 'coding') and it shows that the

Code: Select all

<input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>">
in update.php is not highlighted as a form. Deleting the

Code: Select all

value="<?php echo $rows['name']; ?>"
does highlight it, so I thought that there might be a possible issue.

Or is there also an issue with (in update_ac.php)

Code: Select all

 
// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);
 
To those who are experienced: I'm learning here, but feel free to laugh with such newbie talk. :wink:


Overview
In this tutorial create 3 files
1. list_records.php
2. update.php
3. update_ac.php

Step
1. Create table "test_mysql" in database "test"
2. Create file list_records.php
3. Create file update.php
4. Create file update_ac.php

Creating Table and Dump Data:

Code: Select all

 
 CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;
 
-- 
-- Dumping data for table `test_mysql`
-- 
 
INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Blueton', 'bb5@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (2, 'Jame', 'Campbell', 'jame@somewhere.com');
INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', 'mark@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travor', 'lin65@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (5, 'Joey', 'Ford', 'fordloi@somewhere.com');
INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson', 'gibson@phpeasystep.com');
 

list_record.php

Code: Select all

 
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
 
<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['lastname']; ?></td>
<td><? echo $rows['email']; ?></td>
 
// link to update.php and send value of id
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
 
 
update.php

Code: Select all

 
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
// get value of id that sent from address bar
$id=$_GET['id'];
 
 
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
 
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
 
<?
 
// close connection
mysql_close();
 
?>
 
update_ac.php

Code: Select all

 
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
 
// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);
 
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
 
else {
echo "ERROR";
}
 
?>
 
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Re: Update MySQL table: tiny bug fix needed.

Post by Dakke »

I reposted on this forum. Hopefully I got more luck on this one.

http://w3schools.invisionzone.com/index ... opic=18678
Post Reply