Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Update Mysql Data with PHP Code not working

Posted on 2011-03-08
6
Medium Priority
?
345 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
LVL 111

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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