Forum Moderators: coopster
update.php:
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM projects WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>
<form action="updated.php">
<input type="hidden" name="ud_id" value="<? echo "$id";?>">
First Name: <input type="text" name="ud_first" value="<? echo "$first"?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br>
<input type="Submit" value="Update">
</form>
<?
++$i;
}
?>
updated.php:
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
$query="UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone', mobile='$ud_mobile', fax='$ud_fax', email='$ud_email', web='$ud_web' WHERE id='$ud_id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo "Record Updated";
mysql_close();
?>
That's a pretty basic script and I can't see anything wrong with it. Maybe somebody else with an eagle eye can see a problem.
You could change the query line to get a little more error info.
mysql_query($query) or die (mysql_errno().": ".mysql_error()."<BR>");
echo $query;
I'm wondering if the mysql permissions allow the user to make updates?
Tim
So I'm guessing it's something to do with how my database is set up or permissions or something. Register_globals is turned on. I set up my database through phpmyadmin, which i access through cpanel. Phpmyadmin was already set up when i bought my webspace. I don't enter a password to get to it - and when I try to log out of phpmyadmin it gives me the message "Wrong username/password. Access denied." I don't know if that has anything to do with it. I'm really clueless about all of this. How do you set proper permissions for the database users? In cpanel the main and only user (me) has privileges set to "all". I maintain a blog with wordpress that uses a another database on the same server and have never had any problems with it. Any insight would be great. Thanks again.
A blank screen could be indicative of any sort of problem. What I do to identify the cause of that is usually remove logical chunks of code, upload the script to the web server, run it, see if I still get a blank screen or some kind of other display. Then based on how that works, I'll put back the original code, rip more code out of the script and repeat the above process.
Something else you can do that's a little less drastic is put a die("We got to this point."); statement in a logical place and see if your screen displays that. Keep moving that statement around until you have isolated the offending code.
Just keep trying different things like that, or post your modified code so maybe we can help spot the problem.
Hope this helps.
Throughout this tutorial I have given you pieces of code to make a contacts database script. You can download the full script as a zip file so that you can examine the code (see Related Links).
Where are the Related Links? I want to download this script so I can try it myself and see if I can figure out where your problem is with it.
The fact that someone said in this thread that it isn't the first time someone has questioned this tutorial has got me curious and determined to get to the bottom of it.
Wish me luck!
If I take out the WHERE id='$id' part of the query, the html part of the page displays, but of course it doesn't update.
If I leave it as it and do a "echo $num;" before the while loop, I get 0, so it never goes through the while loop, hence the blank page. I've got 3 entries in my database, so the number should be 3...
In the written part of the tutorial it seems that the "update.php" file should have contain the line - $id=$_GET['id']; But if you download the files from the site, update.php doesn't contain that line. I've tried adding it and it doesn't seem to make any difference.
Thanks for keeping on this. I really appreciate this. I found another post with someone having trouble with it-
[webmasterworld.com...] ,
although they didn't seem to figure it out either.
[edited by: jatar_k at 8:25 pm (utc) on Feb. 19, 2005]
[edit reason] fixed link [/edit]
If I leave it as it and do a "echo $num;" before the while loop, I get 0, so it never goes through the while loop, hence the blank page. I've got 3 entries in my database, so the number should be 3...
and: install yourself a copy of phpmyadmin and browse your database, then you know which ids are used. even there are 3 rows in the database, there is not need that they will have the ids 1,2 and 3. this is very important.
In insert.php, I added the following statements just before the query statement:
$first = mysql_escape_string($_POST['first']);
$last = mysql_escape_string($_POST['last']);
$phone = mysql_escape_string($_POST['phone']);
$mobile = mysql_escape_string($_POST['mobile']);
$fax = mysql_escape_string($_POST['fax']);
$email = mysql_escape_string($_POST['email']);
$web = mysql_escape_string($_POST['web']); In update.php, the following statement:
$query="SELECT * FROM contacts WHERE id='$id'"; should be corrected:
$query="SELECT * FROM contacts WHERE id=$id"; In updated.php, I added the following statements just before the query:
$ud_id = mysql_escape_string($_GET['ud_id']);
$ud_first = mysql_escape_string($_GET['ud_first']);
$ud_last = mysql_escape_string($_GET['ud_last']);
$ud_phone = mysql_escape_string($_GET['ud_phone']);
$ud_mobile = mysql_escape_string($_GET['ud_mobile']);
$ud_fax = mysql_escape_string($_GET['ud_fax']);
$ud_email = mysql_escape_string($_GET['ud_email']);
$ud_web = mysql_escape_string($_GET['ud_web']); <rant>
I have to say that as a tutorial, this one leaves a lot to be desired. In order to be able to update a record, you need to know which record you want first. These scripts don't give you a search or a list from which to select in order to do that.
OTOH, it is supposed to be a fairly simple tutorial to get someone started. Still, it should work regardless of whether someone has register globals on or off.
</rant>
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in update.php on line 7
(I've also tried change it to mysql_num_rows(); and it does the same thing)
It must be something with my database I guess. I set it up in phpmyadmin - with id set to int type, auto_increment and primary, index, and unique key -
all the other fields are varchar type. that's it.
What else could be messed up? Thanks for your help and going through the whole thing. This forums sure are great.
When I add die($query); (up update.php) just before it assigns the query, I get a blank page.
I've also added (in the same place):
mysql_query($query) or die (mysql_errno().": ".mysql_error()."<BR>");
echo $query;
and I get "1065: Query was empty."
What I did to test this, because I knew I had a record in my database with an id of 4 (just as an example) was write the url as:
www.mysite.com/update.php?id=4
I think that's what your problem is with the update piece. Anyway, hope this helps.
Wonder if I should contact them and point them to this thread. I think I will.