Jump to content

[SOLVED] need help in my edit form validation


pixeltrace

Recommended Posts

hi,

 

i need help. i have an edit form which as a validation code

to check if the new parent category/subcategory is not yet existing in the database.

 

apparently only my parent validation code is working, but my validation code for the subcategory is not working.

 

below is my current code

<?
session_start();

if (session_is_registered("username")){
$username = $_SESSION['username'];
$cid = $_GET['cid'];

include 'db_connect.php';
$query = mysql_query("SELECT parent, category_name, cid FROM engsoon_categories WHERE cid= '$cid'") or die(mysql_error()); 
$row = mysql_fetch_array( $query );

$category_name = $row["category_name"];
$parent = $row["parent"];

?>

<table width="100" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="6" align="left"><img src="../images/spacer.gif" width="6" height="10" /></td>
            <td width="100%"><table width="216" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td colspan="3" valign="top" bgcolor="#3261BB"><img src="images/spacer.gif" width="1" height="1" /></td>
              </tr>
              <tr>
                <td align="left" bgcolor="#3261BB" width="1"><img src="images/spacer.gif" width="1" height="1" /></td>
                <td width="214" valign="top">
			<form name="fileup" method="post" enctype="multipart/form-data" action="<? echo $PHP_SELF; ?>">
			<table width="515" border="0" cellspacing="2" cellpadding="3">
                    <tr>
                      <td colspan="4" valign="top"><img src="images/spacer.gif" width="10" height="1" /></td>
                    </tr>
                    <tr>
                      <td colspan="4" bgcolor="#999999" class="text2">EDIT PRODUCT CATEGORY</td>
                    </tr>
                    <tr>
                      <td colspan="4" valign="top"><img src="images/spacer.gif" width="10" height="10" /></td>
                    </tr>
                    <tr>
                      <td width="148" align="right" valign="top" class="text6"><? if ($parent == '0'){ echo "parent category :"; }else{ echo "category name  :"; } ?> </td>
                      <td> </td>
                      <td width="332" colspan="2" class="text7"><input name="category_name" type="text" size="40" value="<? echo "$category_name"; ?>" class="textfield"></td>
                    </tr>
				<? if ($parent ==0) {echo "";}else { ?>
                   <tr>
                      <td align="right" valign="top" class="text6">parent category  : </td>
                      <td width="1"> </td>
                      <td colspan="2"><select name="parent" class="textfield">
                        <option value="">-- select parent category --</option>
					<? 	

                        $_SESSION['fu'] = $parent;

					$uSql = "SELECT category_name, cid FROM engsoon_categories WHERE parent =0";
					$uResult = mysql_query($uSql, $connection);
					if(!$uResult){
					echo 'no data found';
					}else{
					while($uRow = mysql_fetch_array($uResult)){
						?>
                        <option value="<?= $uRow[1]?>" <?php if(($uRow[1] == $_SESSION['fu']))print 'selected';?>>
                          <?= $uRow[0]?>
                        </option>
                        <?
			}   
			}
			?>
                      </select></td>
                    </tr>
                    <tr>
                      <td align="right" valign="top" class="text6"> </td>
                      <td width="1"> </td>
                      <td colspan="2" class="text7">* select a parent category only for sub category items </td>
                    </tr>
				<? } ?>
                      <tr>
                        <td> </td>
                        <td> </td>
                        <td colspan="2"><input type="submit" value="submit" name="submit">
					<input type="hidden" name="username" value="<? echo "$username"; ?>">
					<input type=hidden value=http://www. name=redirect>
					<input type="hidden" value="<? echo "$cid"; ?>" name="cid" />
					<input type="hidden" value="<? echo "$category_name"; ?>" name="cname" />
					<input type="hidden" value="<? echo "$parent"; ?>" name="p" />

					</td>
                      </tr>
                      <tr>
                        <td> </td>
                        <td> </td>
                        <td colspan="2"> </td>
                      </tr>
                </table>
			</form>
			</td>
                <td align="right" bgcolor="#3261BB" width="1"><img src="images/spacer.gif" width="1" height="1" /></td>
              </tr>
              <tr>
                <td colspan="3" valign="top" bgcolor="#3261BB"><img src="images/spacer.gif" width="1" height="1" /></td>
              </tr>
            </table></td>
            <td width="6" align="right"><img src="../images/spacer.gif" width="6" height="10" /></td>
          </tr>
        </table>
<? 
//see if we have submited and that the files array has been set
if(($_POST["submit"]=="submit")){

$category_name = $_POST['category_name'];
$parent = $_POST['parent'];
$cname = $_POST['cname'];
$p = $_POST['p'];
//$category_name_cn = $_POST['category_name_cn'];
//$parent_cn = $_POST['parent_cn'];
$cid = $_POST['cid'];


if (($cname != "$category_name")&&($p != "$parent")){

//parent category verification english
$sql_parent_check = mysql_query("SELECT category_name FROM engsoon_categories WHERE parent = 0 AND category_name='$category_name'");
  $parent_check = mysql_num_rows($sql_parent_check);
	if($parent_check > 0){
		echo '<script language=javascript> alert("parent category already exist!");top.location = "categorymngr.php?id=3&cid='.$cid.'";</script>';
		unset($parent);
		exit();
	} 	 

//subcategory verification english
$sql_subcat_check = mysql_query("SELECT category_name FROM engsoon_categories WHERE parent = '$parent' AND category_name='$category_name'");
  $subcat_check = mysql_num_rows($sql_subcat_check);
	if($subcat_check > 0){
		echo '<script language=javascript> alert("subcategory item for this parent category already exist!");top.location = "categorymngr.php?id=3&cid='.$cid.'";</script>';
		unset($parent);
		exit();
	} 	 
}


$sql="UPDATE engsoon_categories SET category_name ='$category_name', parent='$parent' WHERE cid='$cid'";
	mysql_query($sql) or die("error:".mysql_error());

   echo '<script language=javascript> alert("category item has been updated!");top.location = "categorymngr.php?id=2";</script>';
}
}else{
echo "<font face=\"Arial\">You are not authorized to access this page ... Please <a href='../index.php'>Login</a></font>";
}
?>

 

 

and the code for my validation is starts at line 117.

is this code the simpliest way or is there a better way to validate it?

also, i dont know what is wrong with my code.

been checking this for 5 days already.

 

hope you could help me with this.

 

thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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