Jump to content

[SOLVED] can anyone tell me why this isn't working please


shadiadiph

Recommended Posts

this page has a form the url has carried the id=3 for cid but doesn't display in the page what is wrong with my coding please?

 

???

 

<?
$cid  = $_GET["cid"];
if($cid==""){
$sql = "SELECT * FROM tblcatdetails where intCatID='$cid'"; 
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$cid = $row["intCatID"];
$category = $row["category"];
}}
?>

<table class="category" >
<form name="addcategory" method="post" action="subcatsubmit.php" >
<input type="hidden" name="adminlogged" value="<?=$row["vcUserID"]?>" />
<input type="hidden" name="loggedid" value="<?=$row["intLoginID"]?>" />
<input type="hidden" name="catid" value="<?=$row["intCatID"]?>" />
<tr><td>CATEGORY NAME</td></tr>
<tr><td><input type="text" name="category" value="<?=$row["category"]?>" /></td></tr>
<tr><td>SUB CATEGORY NAME</td></tr>
<tr><td><input type="text" name="subcategory" value="" /></td></tr>
<tr><td colspan="2"> </td></tr>
<tr><td colspan="2">
<input class="button" name="submit" type="submit"  value="Preview" /> 
<input class="button" type="reset" name="reset" value="Reset Form" />
</td></tr>
</table>
</form> 

yes i changed it to just to get the value of cid which has been carried the url browser bar says

 

http://asiapacificsolutions.net/hmt/secure/admin/category/addsubcategory.php?cid=3

so the value 3 is there but $cid=$_GET["cid"] doesn't set any value for it I don't get it?

The html source code says all the val;ues in the form ="" nothing??

 


<?
$cid  = $_GET["cid"];
$sql = "SELECT * FROM tblcatdetails where intCatID='$cid'"; 
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$cid = $row["intCatID"];
$category = $row["category"];
}
?>

<table class="category" >
<form name="addcategory" method="post" action="subcatsubmit.php" >
<input type="hidden" name="adminlogged" value="<?=$row["vcUserID"]?>" />
<input type="hidden" name="loggedid" value="<?=$row["intLoginID"]?>" />
<input type="hidden" name="catid" value="<?=$row["intCatID"]?>" />
<tr><td>CATEGORY NAME</td></tr>
<tr><td><input type="text" name="category" value="<?=$row["category"]?>" /></td></tr>
<tr><td>SUB CATEGORY NAME</td></tr>
<tr><td><input type="text" name="subcategory" value="" /></td></tr>
<tr><td colspan="2"> </td></tr>
<tr><td colspan="2">
<input class="button" name="submit" type="submit"  value="Preview" /> 
<input class="button" type="reset" name="reset" value="Reset Form" />
</td></tr>
</table>
</form> 

I don't know if this makes a difference but I always use single quotes. Also I would not use short_open_tags either. try this:

 

<?php
$cid  = $_GET['cid'];
$sql = "SELECT * FROM tblcatdetails WHERE intCatID='$cid'"; 
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$cid = $row['intCatID'];
$category = $row['category'];
}
?>

<table class="category" >
<form name="addcategory" method="post" action="subcatsubmit.php" >
<input type="hidden" name="adminlogged" value="<?=$row['vcUserID']?>" />
<input type="hidden" name="loggedid" value="<?=$row['intLoginID']?>" />
<input type="hidden" name="catid" value="<?=$row['intCatID']?>" />
<tr><td>CATEGORY NAME</td></tr>
<tr><td><input type="text" name="category" value="<?=$row['category']?>" /></td></tr>
<tr><td>SUB CATEGORY NAME</td></tr>
<tr><td><input type="text" name="subcategory" value="" /></td></tr>
<tr><td colspan="2"> </td></tr>
<tr><td colspan="2">
<input class="button" name="submit" type="submit"  value="Preview" /> 
<input class="button" type="reset" name="reset" value="Reset Form" />
</td></tr>
</table>
</form> 

should only be one result intCatID 3 but could be different if a different object is selected on the page the item is selected on so i can just give $cid the value of 3 wouldn't work.

 

I added this

 

ini_set ("display_errors", "1");

error_reporting(E_ALL);

 

showed no errors but doesn't display anything still?

At the end of the while loop $row ends up containing nothing (i.e. it is false) - that is how it gets out of the loop.

 

You use the elements of $row after the while loop, at that point they will all equal nothing.

 

If you enabled full error reporting I would expect you to see the notice - index does not exist for all your form elements.

 

Change your code so that the form is populated with $row data inside the while loop.

Or he can keep it the way that it is and just take out the while loop. If you are only returning 1 result you can do it like this:

 

<?php
$cid  = $_GET['cid'];
$sql = "SELECT * FROM tblcatdetails WHERE intCatID='$cid'"; 
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$cid = $row['intCatID'];
$category = $row['category'];
}
?>

<table class="category" >
<form name="addcategory" method="post" action="subcatsubmit.php" >
<input type="hidden" name="adminlogged" value="<?=$row['vcUserID']?>" />
<input type="hidden" name="loggedid" value="<?=$row['intLoginID']?>" />
<input type="hidden" name="catid" value="<?=$row['intCatID']?>" />
<tr><td>CATEGORY NAME</td></tr>
<tr><td><input type="text" name="category" value="<?=$row['category']?>" /></td></tr>
<tr><td>SUB CATEGORY NAME</td></tr>
<tr><td><input type="text" name="subcategory" value="" /></td></tr>
<tr><td colspan="2"> </td></tr>
<tr><td colspan="2">
<input class="button" name="submit" type="submit"  value="Preview" /> 
<input class="button" type="reset" name="reset" value="Reset Form" />
</td></tr>
</table>
</form> 

mm I took the loop out a while ago and got it working now this is how still don't get why it didn't like the <?=$row[ usually it worls fine.

this is how it ended up. Thanks for the input guys. :)

 

<table class="category" >
<form name="addcategory" method="post" action="subcatsubmit.php" >
<input type="hidden" name="adminlogged" value="<?=$row["vcUserID"]?>" />
<input type="hidden" name="loggedid" value="<?=$row["intLoginID"]?>" />
<?
$cid  = $_GET["cid"];
$sql = "SELECT * FROM tblcatdetails where intCatID='$cid'"; 
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$cid = $row["intCatID"];
$category = $row["category"];
}
?>
<tr><td>CATEGORY NAME</td></tr>
<tr><td><input type="text" name="category" value="<?=$category?>" /></td></tr>
<tr><td>SUB CATEGORY NAME</td></tr>
<tr><td><input type="text" name="subcategory" value="" /></td></tr>
<tr><td colspan="2"> </td></tr>
<tr><td colspan="2">
<input type="hidden" name="cid" value="<?=$cid?>" />
<input class="button" name="submit" type="submit"  value="Preview" /> 
<input class="button" type="reset" name="reset" value="Reset Form" />
</td></tr>
</table>
</form> 

 

I had to move the php down the form as it was already calling php from the head without moving it down it cancelled out the first bit of php in my header.

 

 

When you do a mysql_fetch_assoc it extracts the data then increments the pointer to the SQL table.  The while loop keeps running until $row is false i.e. there is no data left to extract and so $row is empty.

 

So you can't use $row after the while loop.

 

In this situation you don't need a while loop anyway because you only want one row (As ngreenwood has said!).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.