Solved

Update Mysql Data with PHP Code not working

Posted on 2011-03-08
6
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 110

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 110

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 17

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 110

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

752 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