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> </td><td colspan="3"><strong>Update data in mysql</strong> </td></tr><tr><td align="center"> </td><td align="center"> </td><td align="center"> </td><td align="center"> </td></tr><tr><td align="center"> </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> </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> </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> </td></tr></table></td></form></tr></table><?// close connection mysql_close();?>
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
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
Chris Harte
Your query is not returning anything. Try this on line 19
$row_num = mysql_num_rows($result);
echo "number of rows returned $row_num";
Ray Paseur
@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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Lukasz Chmielewski
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
http://www.sitepoint.com/books/phpmysql4/
HTH, ~Ray