Solved

Update Mysql Data with PHP Code not working

Posted on 2011-03-08
6
331 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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