Jump to content

form validation showing errors but even after showing error its inserting data into database


srinivasa

Recommended Posts

//here is the code

 

 

<?php

session_start();

?>

 

<html>

<head><title>Add Product</title>

<!--FOR ADD-PRODUCT FORM-->

<script type="text/javascript" src="ADD-PRODUCT.js"></script>

<link rel = stylesheet href = "design.css" type = "text/css">

<!--category selection-->

<script type = "text/javascript">

function showSelectedIndex(){

i = document.product.firstbox.selectedIndex;

cat_val=document.product.firstbox.options.value;

self.location = 'add-product.php?firstbox='+cat_val;

}

</script>

</head>

 

<body>

<!--CONNECT DATABASE-->

<?php include('connect-db.php'); ?>

<!--topmost-->

<table width = "100%" height = "10%">

<tr>

<td style="font-family:impact;color:gray;font-size:1.5pc;text-indent:80px">THE PRODUCT STORE</td>

<td style="font-family:verdana;font-size:.8pc;font-weight:bold;text-align:right;color:gray"><a href = logout.php>Logout</a></td>

</tr>

</table><br>

<!--middle-->

<table width = "100%" height = "100%">

<tr valign = "top">

<td div class = "left"></div></td>

<!--content-->

<td div class = "middle">

<table width = 100%><tr>

<td style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 50%>ADD PRODUCT:</td>

<td style="font-family:verdana;color:gray;font-size:0.7pc;text-align:right" width = 32%><a href = add-product.php>Add Products</a>   |</td>

<td style="font-family:verdana;color:gray;font-size:0.7pc;text-align:right" width = 18%><a href = product-display.php>View Products</a></td>

</tr></table><br>

<!--FORM STARTS HERE------------------------------------------------>

<form name = "product" action ='' method = 'post' enctype="multipart/form-data">

<table width = 100% cellpadding = 5>

<!--product category-->

<tr>

<td style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 50%>Product Category:</td>

<td width = 50%>

<?php

$ddone = @mysql_query("SELECT cat_id,cat_name FROM category");?>

<select name = 'firstbox' onChange = 'showSelectedIndex()'>

<option> - - - - - - Select Category - - - - - - </option>

<?php

while($row = mysql_fetch_assoc($ddone)){

$catid = $row['cat_id'];

$catname = $row['cat_name'];

echo "<option value = \"$catid\"> $catname </option>";

}

echo "</select>";

?>

</td>

</tr>

 

<!--product sub-category-->

<tr>

<td style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 50%>Product Sub-category:</td>

<td>

<?php

@$cat = $_GET['firstbox'];

?>

<!--TO HOLD CATEGORY SELECTED DATA-->

<script language="JavaScript">

var objlen=document.product.firstbox.length;

var selected_cat="<?php echo $cat;?>";

for(var i=0;i < objlen;i++){

if (document.product.firstbox.options.value == selected_cat){

document.product.firstbox.options.selected=true;

break;

}

}

</script>

<?php

$ddtwo = @mysql_query("SELECT * FROM subcategory WHERE cat_id = $cat");?>

<select name = 'subcat'>

<?php

echo "<option> - - - - - - - Sub-Category - - - - - - - </option>";

while($now = mysql_fetch_assoc($ddtwo)){

$subcatid = $now['subcat_id'];

$subcatname = $now['subcat_name'];

echo "<option value = \"$subcatid\">$subcatname</option>";

}

echo "</select>";

?>

</td>

</tr>

 

<!--product name-->

<tr>

<td style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 50%>Product Name:</td>

<td><input type="text" id="pn" name = "pn" value = "" size = 30></td>

</tr>

 

<!--product price-->

<tr>

<td style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 50%>Product Price:</td>

<td><input type="text" id="pp" name = "pp" value = "" size = 30></td>

</tr>

 

<!--product description-->

<tr>

<td valign =top style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 50%>Product Description:</td>

<td><textarea cols="23" rows="5" id="pd" name = "pd" value = ""></textarea></td>

</tr>

</table>

 

<!--product image-->

<table cellpadding = 3>

<tr>

<td style="font-family:verdana;color:gray;font-size:.8pc;font-weight:bold;"width = 51%>Product Image:</td>

<td width = 35% align = left><input type="file" id="pi" name = "pi" value = ""></td><td></td>

</tr>

<tr><td></td><td align = right><br><input type = 'submit' name = 'submit' value = 'Submit'></td></tr>

</table>

 

</form>

 

</div>

</td>

<!--FORM ENDS HERE-->

 

<div class = error>

<?php

// check if the form has been submitted. If it has, start to process the form and save it to the database

if (isset($_POST['submit']))

{

/* get form data, making sure it is valid

$prodcat = mysql_real_escape_string(htmlspecialchars($_POST['firstbox']));

$prodsubcat = mysql_real_escape_string(htmlspecialchars($_POST['subcat']));

$prodname = mysql_real_escape_string(htmlspecialchars($_POST['pn']));

$prodprice = mysql_real_escape_string(htmlspecialchars($_POST['pp']));

$proddesc = addslashes($_POST['pd']);

@$prodimage = mysql_real_escape_string(htmlspecialchars($_POST['pi']));

$proddate = date("d/m/Y");

*/

$prodcat = $_POST['firstbox'];

$prodsubcat = $_POST['subcat'];

$prodname = $_POST['pn'];

$prodprice = $_POST['pp'];

$proddesc = $_POST['pd'];

@$prodimage = $_POST['pi'];

$proddate = date("d/m/Y");

 

//PRODUCT CATEGORY

if ($prodcat != '- - - - - - Select Category - - - - - -')

{

$msg = " ";

}

else

{

echo"Please  Select  Product  Category!!<br>";

}

 

//PRODUCT SUBCATEGORY

if ($prodsubcat != '- - - - - - - Sub-Category - - - - - - -')

{

$msg = " ";

}

else

{

echo"Please  Select  Product  Sub-Category!!<br>";

}

 

 

// PRODUCT NAME

if (preg_match('/^[A-Za-z]{3,20}$/',$prodname))

{

$msg = " ";

}

elseif(empty($prodname))

{

echo"Please  enter  ProductName,  its  empty!!<br>";

}

else

{

$prodname = NULL;

echo 'Enter  valid  ProductName.<br>';

}

 

// PRODUCT PRICE

if (preg_match('/^[0-9]{2,10}$/',$prodprice))

{

$msg = " ";

}

elseif(empty($prodprice))

{

echo"Please  enter  ProductPrice,  its  empty!!<br>";

}

else

{

$prodprice = NULL;

echo 'Enter  valid  ProductPrice.<br>';

}

 

//PRODUCT DESCRIPTION

if (preg_match('/^[\w\n\t\s\r]{2,50}$/',$proddesc))

{

$msg = " ";

}

elseif(empty($proddesc))

{

echo"Please  enter  your  ProductDescription,  its  empty!!<br>";

}

else

{

$proddesc = NULL;

echo 'Enter  your  ProductDescription.<br>';

}

 

//PRODUCT IMAGE validation yet to be done

echo "</div>";

 

// save the data to the database

mysql_query("INSERT products SET product_name='$prodname',subcat_id='$prodsubcat',price='$prodprice',description='$proddesc',product_image='$prodimage',uploaded_date='$proddate' ")

or die(mysql_error());

}

?>

 

<td div class = "right"></div></td>

</tr>

</body>

</html>

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//here is my table

-- --------------------------------------------------------

--

-- Table structure for table `products`

--

CREATE TABLE IF NOT EXISTS `products` (

`product_id` int(11) NOT NULL auto_increment,

`product_name` varchar(150) NOT NULL,

`subcat_id` int(11) NOT NULL,

`price` varchar(20) NOT NULL,

`description` text NOT NULL,

`product_image` varchar(200) NOT NULL,

`uploaded_date` varchar(50) NOT NULL,

PRIMARY KEY (`product_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

 

 

plz anybody help me..........

Link to comment
Share on other sites

You might want to do something like this...

 

if (isset($_POST['submit']))
{ 
   // get form data
   // set a boolean "$no_errors" to true
   // check for errors. If you find any errors, set $no_errors to false
   // only insert data into the database if $no_errors == true
}

 

This would stop the data from inserting if you have errors in the input.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...