Page 1 of 2
[SOLVED]mysql_fetch_array()
Posted: Mon Jan 26, 2004 8:48 am
by ol4pr0
I got this problem with retrieving information from the database.
2 errors occure.
Notice: Undefined index: Paqnr in c:\apache\htdocs\retrieve.php on line 31
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\apache\htdocs\retrieve.php on line 33
Script;
Code: Select all
// formulario, solo pone el codigo paquete
ECHO <<<HTML
<html>
<head>
<body class="v">
<form name="retrieve" action={$_SERVER['PHP_SELF']} method="GET" class="center">
<table width="250" border="0" class="center" cellpading="3">
<tr>
<td>
<div class="center"><b>Required information?</b></div>
</td>
</tr>
Enter Paq. number:<input type="Paqnr" name="paqnr">
<br>
<Div class="r"><input name="Paqnr" type="submit" ID="Paqnr" WIDTH="5"
CLASSID="CLSID:B6FC3A14-F837-11D0-9CC8-006008058734"></div>
HTML;
// connectarse con el base datos.
$dbase ="home";
$tablename ="pongo";
$connect= mysql_connect('localhost')
or die("Cant connect: " .mysql_error());
// consultar el tabla pongo
$query = "SELECT * FROM pongo WHERE Paqnr='" .$_GET['Paqnr']. "'"; //line 31 ( i also tried ='" .$paqnr. " ' ";
$result = mysql_query($query);
if($row = mysql_fetch_array($result)) // line 33
{
echo "<tr class="V"> ";
echo "<td width="100" class="v"> ";
echo "<div class="r">Numero Paquete</div> ";
echo "</td>\n ";
echo "<td width="150"> ";
echo $row['Paqnr'];
echo " </td>\n ";
echo " </tr>\n ";
and so On......
}
?>
What am i doing wrong.
Posted: Mon Jan 26, 2004 9:12 am
by JayBird
yes, i see the problem, nothing wrong with your PHP, its your HTML
this line
Should be
Mark
Posted: Mon Jan 26, 2004 9:13 am
by ol4pr0
HAd it like that
doesnt make any differance.
line 31
line 33
is whats making my life difficult on this sunny day.
Thanks for noticing tho.
Posted: Mon Jan 26, 2004 9:16 am
by JayBird
also,
your form element name is "paqnr", but you are requesting "Paqnr"
Notice the different case, should be the same!
Mark
Posted: Mon Jan 26, 2004 9:20 am
by twigletmac
[Edit: that'll teach me to write a post, get caught up in other things and then submit it - too slow

]
It's because of this:
Firstly, there is no such 'Paqnr' type - you probably want it to be of type text:
Code: Select all
<input type="text" name="paqnr" />
Now PHP is case-sensitive so $_GET['Paqnr'] is not the same as $_GET['paqnr']. You'll end up confusing matters even more because your submit button is called 'Paqnr' - you should really change its name. You should also check that the variable is in the URL before trying to use it and that it is of a type you'd expect, using user inputted data directly in a SQL statement is asking for trouble.
I started trying to correct your code but ended up confused over what it was trying to do - you select a number from the form and then redisplay this number before selecting another? I guess I'm just missing what's in the 'and so On...' bit.
Mac
Posted: Mon Jan 26, 2004 9:20 am
by ol4pr0
Oke i think you are aming for the Capital P here. so i did change that also changed the input type='text'. Just to make sure i am not a complete idiot.
However it didnt resolve my issue
Posted: Mon Jan 26, 2004 9:23 am
by twigletmac
If the variable is not in the URL you'll get the undefined index error - you need to do some checking before you use the variable.
Mac
Posted: Mon Jan 26, 2004 9:24 am
by JayBird
can you now show us your ammended code
Mark
Posted: Mon Jan 26, 2004 9:25 am
by ol4pr0
Well i had it all like input='text' and yes mysql also has Paqnr. not paqnr.
since i tried to figure out why it wouldnt reconize my Paqnr i start messing around with the Paqnr in places i could mess with it. To see if it would reconize.
mysql database looks like this.
CREATE TABLE pongo (
Empresa varchar(20) NOT NULL default '',
Codigo int(16) NOT NULL default '0',
Paqnr int(16) NOT NULL default '0',
Desde varchar(20) NOT NULL default '',
Envio varchar(20) NOT NULL default '',
Peso_Total varchar(50) NOT NULL default '',
Peso varchar(10) NOT NULL default '',
Valor int(9) default NULL,
Information varchar(20) NOT NULL default '',
Comentario varchar(20) NOT NULL default '',
Codigo_Env int(15) default NULL,
PRIMARY KEY (Paqnr)
) TYPE=MyISAM;
Posted: Mon Jan 26, 2004 9:30 am
by JayBird
Maybe to make your debugging easier, ill briefly tell you what the eror messages mean
Notice: Undefined index: Paqnr in c:\apache\htdocs\retrieve.php on line 31
This bascially means that you are trying to access an index in an array that doesn't exist. In theis case, you are trying to access "paqnr" in the array "$_GET".
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\apache\htdocs\retrieve.php on line 33
The reason you are getting this is because the query you are supplying isn't correct, this is due to the reason described above.
Fix the first problem, and in turn you fix the second.
also, in the address bar, when you submit the form, can you see Paqnr=(the value you eneterd)?
Mark
Posted: Mon Jan 26, 2004 9:32 am
by ol4pr0
the code like it was before my messing around with it. and how it is Now!
Code: Select all
<?
require ("include/styles.php");
// formulario, solo pone el codigo paquete
ECHO <<<HTML
<html>
<head>
<body class="v">
<form name="retrieve" action={$_SERVER['PHP_SELF']} method="GET" class="center">
<table width="250" border="0" class="center" cellpading="3">
<tr>
<td>
<div class="center"><b>Required information?</b></div>
</td>
</tr>
Enter Paq. number:<input type="text" name="Paqnr">
<br>
<Div class="r"><input name="submit" type="submit" ID="Paqnr" WIDTH="5"
CLASSID="CLSID:B6FC3A14-F837-11D0-9CC8-006008058734"></div>
HTML;
// connectarse con el base datos.
$dbase ="home";
$tablename ="pongo";
$connect= mysql_connect('localhost')
or die("Cant connect: " .mysql_error());
// consultar el tabla pongo ( cambiar cuando listo )
$query = "SELECT * FROM pongo WHERE Paqnr='" .$_GET['Paqnr']. "'";
$result = mysql_query($query);
if($row = mysql_fetch_array($result))
{
echo "<tr class="V"> ";
echo "<td width="100" class="v"> ";
echo "<div class="r">Numero Paquete</div> ";
echo "</td>\n ";
echo "<td width="150"> ";
echo $row['Paqnr'];
echo " </td>\n ";
AND SO ON
}
?>
same errors.
Posted: Mon Jan 26, 2004 9:36 am
by JayBird
i don't see you selecting the DB anywhere.
Try inserting this line before your query
Code: Select all
mysql_select_db($dbase, $connect);
Mark
Posted: Mon Jan 26, 2004 9:38 am
by ol4pr0
yea i have that line in top in my explorer
Posted: Mon Jan 26, 2004 9:41 am
by ol4pr0
Now i do feel like a complete idiot. Caramba..
mysql_select_db($dbase, $connect);
one more question
should this return a error
Code: Select all
echo "<div class="r">Peso Total</div> ";
echo "</td>\n ";
echo "<td width="150"> ";
echo $row['Peso_total']; // Peso_Total
echo " </td>\n ";
is it because of _ the underscore ?
never mind "Case intensive "

thanks
Posted: Mon Jan 26, 2004 9:43 am
by JayBird
glad its sorted
hopefully we ironed out a few other potential problems along the way too.
Mark