Forum Moderators: coopster

Message Too Old, No Replies

$id=$_GET['id'];

         

davider

10:46 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



Hello everyone,

I'm new to learning php and have been using the following this tutorial to get up and running -

[freewebmasterhelp.com...]

I'm at the stage where I want to update my database using a form.

The main part of my code consits of -

<?php
$id=$_GET['id'];
//username and password stuff here

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$music="SELECT * FROM music WHERE id='$id'";
$result=mysql_query($music);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$mlink=mysql_result($result,$i,"m_link");
$murl=mysql_result($result,$i,"m_url");
?>
<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id;?>">
Link: <input type="text" name="ud_link" value="<? echo $mlink;?>"><br>
URL: <input type="text" name="ud_url" value="<? echo $murl;?>"><br>
<input type="Submit" value="Update">
</form>
<?
++$i;
}
?>

I get an error saying -

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /customers/david-e.de/david-e.de/httpd.www/PHP/update2.php on line 19

And line 19 is - $num=mysql_numrows($result);

I think the problem may be to do with this line however - $id=$_GET['id'];

As I dont think the $id is getting the information it needs. I'm not totally sure though.

If anyone can help then its much appreciated.

Many Thanks,

David

caspita

10:57 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



Hi davider,

your form is using "POST" method and you are using $_GET['id'] to recover the input... you have to user $_POST['id'] variable to retrieve from a POSTed form.

just change your first line to :

$id = $_POST['id'];

The error your are getting means that the $result resource have no data .. this is because of you trying to get the $id parameter from the URL ($_GET['id']) intead of the posted data ($_POST['id']).

davider

11:10 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



Hi Caspita,

Thanks for the reply. I tried changing the code but I still get the same error? The code I used it copied from the tutorial

so I'm not sure why it is wrong?

Cheers,

David

[edited by: jatar_k at 11:17 pm (utc) on Nov. 29, 2004]
[edit reason] removed url [/edit]

caspita

1:34 am on Nov 30, 2004 (gmt 0)

10+ Year Member



Hi David,

Forget what I said before :-) I read to fast and I was wrong anyway ;-).

Let me try to explain what the code is suppose to do, then you can try to figure out what is going on. I'm assuming your are soooo new so, I'll explain every important line

I'll put my comments using

comment
notation below each line:

<?php
$id=$_GET['id'];

this means your script need a parameter in the URL, so, let's you domain is example.com and your script is named example.php, then to execute the script you need to include the 'id' parameter like : [example.com...] the '?' in the URL indicates a parameter to be passed and then the name and value.

//username and password stuff here

mysql_connect(localhost,$username,$password);

this is the standard function to open a mysql connection, here you have localhost .. which I suposse is fine only that you need to specify the quotes for strings or the $ if it is a variable.. so your line should be even mysql_connect("localhost",$username,$password); or mysql_connect($localhost,$username,$password); ... now, those variables needs to be initialized with values but I think you are doing it in your commented section '//username and password stuff here' ... is also $localhost there? otherwise you need to specify "localhost"... also be sure that localhost is the host that you db is stored in and that the standard port for mysql is being used otherwise you need to explicitly indicate that like "dbhost:1111" host and port .. or "127.1.1.1:1111" ip and host.

@mysql_select_db($database) or die( "Unable to select database");

my recomendation here is to use the regular method to select the database, like $db = mysql_select_db($database); ... also is $database defined in your commented section? otherwise which database are we looking for?

$music="SELECT * FROM music WHERE id='$id'";

this looks fine.. I'm assuiming you have the database and table created and that you put some data in

$result=mysql_query($music);
this looks fine

$num=mysql_numrows($result);
here is where you have the problem.. the reason is that the query didn't get any data back from mysql so this means that 1. It could not connect or 2. It could not select the database or 3. The table didn't exist or 4. The thable has no data with that key... to know what is going on you need to manually be sure tha 1, 2, 3, 4 are not the reason, the wy to know is to check the database from the console or so... also you can put the die() function in the query sentence to get the reason directly from mysql... go back to your line $result=mysql_query($music);
and change it for $result=mysql_query($music) or die( "Unable to query the database" . mysql_error()); then execute the script and let's see what error you get. Then we can go futher

mysql_close();
$i=0;
while ($i < $num) {
$mlink=mysql_result($result,$i,"m_link");
$murl=mysql_result($result,$i,"m_url");
?>
<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id;?>">
Link: <input type="text" name="ud_link" value="<? echo $mlink;?>"><br>
URL: <input type="text" name="ud_url" value="<? echo $murl;?>"><br>
<input type="Submit" value="Update">
</form>
<?
++$i;
}
?>

davider

3:06 pm on Nov 30, 2004 (gmt 0)

10+ Year Member



Hi Caspita,

Thanks very much for the reply and information. I'm still picking away at it, trying to get it working. I downloaded the files for the example but they dont even correspond to the tutorial which is strange. I read all that you suggested and I think everythin is as it should be, but each time I bring up the update.php page I just get a blank screen? Most odd. Anyway, i will continue on my mission and thanks again for taking the time to offer help.

--David