Forum Moderators: coopster

Message Too Old, No Replies

problem with php/mysql tutorial

         

yuppster

5:42 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



I'm trying to learn php and mysql. I've been going through the tutorial at [freewebmasterhelp.com...]
All was fine until I got to the "update" script which is supposted to update tables in the database. I get
the "update query failed" error. I think others have had problems getting this to work as well. Below is the code from the two files in the tutorial. Also, if anyone knows a better, simple "update" script that would be handy. Thanks.

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();

?>

Timotheos

8:00 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi yuppster,

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

coopster

8:04 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



May be a register_globals [php.net] issue as well. Have you tried using the $_POST superglobal array instead?

jatar_k

11:25 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



strangely this is not the first time people have posted about trouble with that tutorial.

vinyljunkie

3:19 am on Feb 18, 2005 (gmt 0)

10+ Year Member



I see one problem with that script. This statement:

$num=mysql_numrows($result); 

should be coded this way instead:

$num=mysql_num_rows($result); 

yuppster

5:46 am on Feb 18, 2005 (gmt 0)

10+ Year Member



Thanks for all of your help. Still can't figure this out though. I keep getting a MySQL error 1064 now. I even installed phpmyedit (http://phpmyedit.org/home.php), because that SHOULD work, but still got that same error. (Oddly, the only thing I can do is delete tables, not update or add them.)

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.

yuppster

2:00 pm on Feb 18, 2005 (gmt 0)

10+ Year Member



Alright, I figured out a big problem. I had customized my field names in the tutorial - one being "desc" for description - and I just found out that's mysql reserved word, so it was giving me errors when trying to update. I replaced it with anoter word, so now phpmyedit is working as it should. But that tutorial code is still not working. It's giving me a blank page when I load update.php. I still want to figure that out for the sake of learning how to do this. Thanks again for all your help.

vinyljunkie

1:22 am on Feb 19, 2005 (gmt 0)

10+ Year Member



Is desc the only thing you've changed from the original script? Believe it or not, a blank screen IS indicative of progress. ;)

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.

hakre

7:44 am on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the only thing which pops into my eyes is
mysql_close();
, i would use this after the last time use of
mysql_result
(ie after the while loop).

vinyljunkie

3:31 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



On this page:
[freewebmasterhelp.com...]
it says:

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.

vinyljunkie

3:38 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



Duh! Never mind, I found it.

hakre

3:56 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



so what was the problem?

vinyljunkie

4:20 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



Brain fart. I found the code and downloaded it. Now I will go through the tutorial myself to see if I can figure out where the problem lies.

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!

yuppster

8:22 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



Some observations:

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]

hakre

12:06 am on Feb 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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...

if the mysql returns 0 rows, your query is return 0 rows. that's it. you can run in circles and your database might have 3 or 300 entries: your query returns 0 of them. check your query. print it out (echo) and check it!. btw. you don't need to put $id in '', just leave it without in the where clause.

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.

vinyljunkie

2:50 am on Feb 20, 2005 (gmt 0)

10+ Year Member



OK, I've gone through this tutorial and gotten the scripts to work on my server with some modifications, as follows:

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>

yuppster

3:14 am on Feb 20, 2005 (gmt 0)

10+ Year Member



Okay, I used the scripts from the tutorial site and made the same modifications you did, and I get this:

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.

vinyljunkie

4:30 am on Feb 20, 2005 (gmt 0)

10+ Year Member



Which script are you trying to run? The one that adds records to the database or what? Whichever one you're running, add this statement just above where it assigns the value of $query and post the results here in this thread. Thanks.

die($query);

yuppster

6:14 pm on Feb 20, 2005 (gmt 0)

10+ Year Member



I'm trying to get the update.php/updated.php scripts to work. The add.html/insert.php scripts work fine.

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."

vinyljunkie

8:14 pm on Feb 20, 2005 (gmt 0)

10+ Year Member



One of the things this script doesn't tell you (and is one of its failings) is that you must pass a query string to the update.php script so it knows which record in the database you want to update. Unfortunately, it doesn't give you a selection list or anything so you know which record to update. Catch-22.

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.

yuppster

1:51 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



Thanks vinyljunkie, that was totally it. It works now. I guess he does mention that in the tutorial but it's kind of confusing if your a beginner like me. It would have been nice if he included in the example files a hmtl page with links passing the variable to update.php. Anyways, thank you all for your help.

vinyljunkie

3:30 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



You're welcome. I'm glad I could help out. :) One of the things that would help with that tutorial is to have a hyperlink to each of the records in the database that would automatically put you in the update screen. That and adding the code I mentioned, plus fixing the one error that I found.

Wonder if I should contact them and point them to this thread. I think I will.