Solved

Update Mysql Data with PHP Code not working

Posted on 2011-03-08
6
332 Views
Last Modified: 2012-08-13
This code is supposed to allow me to view data in a mysql database & then allow me to edit it & submit it updating the database. I'm getting an error "attached in the photo"  when I go to the page in the browser.
<?php
$host="localhost"; // Host name 
$username="uname"; // Mysql username 
$password="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();

?>

Open in new window

mysql-error.JPG
0
Comment
Question by:wantabe2
  • 3
  • 2
6 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 35068992
Same issues as the other script.  You need to use data visualization to see the error messages!

Change this code from this:
$result=mysql_query($sql);

... to this:
$result=mysql_query($sql) or die( mysql_error() );

Add the error_reporting(E_ALL) and "display errors" settings to the script and run it again.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35069009
Also, please let me suggest a really good book to help you get some foundation in how PHP and MySQL work together.  It will not make you a pro, but it has great examples and is very readable.  Also comes with a downloadable code library that you can copy and use for your own apps.
http://www.sitepoint.com/books/phpmysql4/

HTH, ~Ray
0
 
LVL 16

Expert Comment

by:Chris Harte
ID: 35069013
Your query is not returning anything. Try this on line 19

$row_num = mysql_num_rows($result);
echo "number of rows returned $row_num";
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35069154
@Munterman:  Actually the query is returning an error value.  The warning message is pretty clear.  MySQL_Query() returns either a results resource on success or FALSE on failure.  The message says, "... expects parameter 1 to be a resource, boolean given..."  In this case, the boolean is FALSE.

Man page here (see the return values):
http://us.php.net/manual/en/function.mysql-query.php
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35069197
There are 3 possibilities of this error:
1. The name of the table is for non-existing one
2. There is no ?id=yournumber in the GET variable from the browser addressbar
3. Well, I forgot the 3rd one

Turn on error reporting as Ray told, it is essential to "debug" mysql/php errors
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35069205
What was the cause ?
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article discusses how to create an extensible mechanism for linked drop downs.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

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

18 Experts available now in Live!

Get 1:1 Help Now