Solved

Update Mysql Data with PHP Code not working

Posted on 2011-03-08
6
334 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 109

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 109

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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 109

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
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).

830 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