Page 1 of 1

Can't see what's wrong

Posted: Tue Dec 19, 2006 9:11 pm
by davidppppppppppp
When I first access this page it displays all the website links in the database. Next to each link name is an edit link so that I can make changes to that particular website link. The edit link is to this same page - editlink.php.

When I clink on the edit link it is suppose to open up a form with the particulars of that entry already populating the form fields. Here I should be able to make whatever changes and click submit to update the database.

But I'm getting empty fields with this error message:

Code: Select all

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hsphere/local/home/... editlink.php on line 74
I have another script almost exactly like this one and it works fine, so I don't understand why I'm getting this fetch_array error.

What's wrong here?

Here is line 74 (at bottom) as it appears in the qyuery string:

Code: Select all

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") 
{ 
if (!isset($_POST["submit"])) 
{ 
$id = $_GET["id"]; 
$sql = "SELECT * FROM links WHERE id=$id"; 
$result = mysql_query($sql); 
$myrow = mysql_fetch_array($result); 
?>
You can see it in the whole script below.

Code: Select all

<?php

include("dbinfo.inc.php");

$conn = mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$database",$conn)  or die(mysql_error()); 


if(!isset($cmd)) 
{ 
//display all the links 
$result = mysql_query("select * from links order by id"); 

//run the while loop that grabs all the links 
while($r = mysql_fetch_array($result)) 
{

//grab the items to be displayed 

$id=$r["id"];
$name=$r["name"];
$link=$r["link"];

?>

<!--<tr><td width="5%" align=right><font face=arial size=2><b>Id #</b>:&nbsp;&nbsp;</font></td><td width="80%"><font face=arial size=2>$id</font></td></tr>--> 
<tr>
<td width="25%"><font face=arial size=2>
<b>Name of Website</b>:&nbsp;&nbsp;</font></td>
<td width="40%"><font face=arial size=2><? echo "$name"; ?></font></td>

<td width="30%" ><font face=arial size=2><a href='editlink.php?cmd=edit&id=$id'>Edit</a></font></td>
</tr>
<tr><td colspan=3 style="border-top:1px solid #666666"><font face=arial size=2>&nbsp;</font></td></tr>

<?php
} 
} 
?>

</table> 


<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") 
{ 
if (!isset($_POST["submit"])) 
{ 
$id = $_GET["id"]; 
$sql = "SELECT * FROM links WHERE id=$id"; 
$result = mysql_query($sql); 
$myrow = mysql_fetch_array($result); 

?>

<center>
<table width="95%" cellspacing=0 cellpadding=3 border=0>
<tr>
<td>
<form action="editlink.php" method="post"> 

<input type=hidden name="id" value="<?php echo $myrow['id'] ?>"> 

Name of Website:<br /> 
<INPUT TYPE="TEXT" NAME="name" VALUE="<?php echo $myrow['name'] ?>" SIZE=65><br /><br /> 

Link:<br />
<INPUT TYPE="TEXT" NAME="link" VALUE="<?php echo $myrow['link'] ?>" SIZE=65><br /><br />

<input type="hidden" name="cmd" value="edit"> 

<input type="submit" name="submit" value="submit"> 

</form> 
</td>
</tr>
</table>
</center>

<?php } ?>


<?php

if ($_POST["$submit"]) 
{ 
$name = $_POST['name']; 
$link = $_POST['link'];

$sql = "UPDATE links SET name='$name', link='$link' WHERE id=$id"; 

$result = mysql_query($sql); 
echo "<br /><br /><p align=\"center\"><span style=\"color:red\">Link updated.</span></p><br /><br />";

echo "<p align=\"center\"><a href=\"editlink.php\">Edit another Website Link</a></p><br /><br />";
 
  }
}

?>

Posted: Tue Dec 19, 2006 9:32 pm
by John Cartwright
adding some standard debugging code will allow us to see what happened.

Code: Select all

$sql = "SELECT * FROM links WHERE id=$id";
$result = mysql_query($sql) or die('SQL code "'.$sql.'" failed because error "'.mysql_error());
Adding some var_dump()'s of variables in your script wouldn't hurt, to make sure all your variables are accounted for. You should also set error_reporting(E_ALL); to see if you have any undefined variables or other notices.

Posted: Tue Dec 19, 2006 9:45 pm
by feyd
$submit?

Posted: Tue Dec 19, 2006 9:55 pm
by DrTom
You try to use $_GET['id'] when the form that contains id uses the post method

try $_POST['id']