Jump to content

Running a preliminary sql check before processing further


s_ainley87

Recommended Posts

Hello,

 

I am trying to create a form where a use will enter a category in to a form that creates a product in a mysql table, i am hoping that someone could explain to me how i can run a check that the category entered already exists in thr category table?

 

This is the code so far, basically it checks there is data and sets a variable,

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Product added redirecting....</title>
</head>

<body>
<?php
require_once ('include/mysql_connect.php'); // Connect to the database.
if ($_POST[op] != "yes"){ // Check if the form has been submitted.

if (!empty($_POST['Prodname'])) {
	$pn = escape_data($_POST['Prodname']);
} else {
	$pn = FALSE;
	echo '<p><font color="red">Please enter the product\'s name!</font></p>';
}

if (!empty($_POST['Catname'])) {
	$cn = escape_data($_POST['Catname']);
} else {
	$cn = FALSE;
	echo '<p><font color="red">Please enter the product\'s category name!</font></p>';
}
// Check for an image.
if (is_uploaded_file ($_FILES['image']['tmp_name'])) {
	if (move_uploaded_file($_FILES['image']['tmp_name'], "uploads/{$_FILES['image']['name']}")) { // Move the file over.

		echo '<p>The file has been uploaded!</p>';

	} else { // Couldn't move the file over.
		echo '<p><font color="red">The file could not be moved.</font></p>';
		$i = FALSE;
	}
	$i = $_FILES['image']['name'];
} else {
	$i = FALSE;
}

// Check for a price.
if (is_numeric($_POST['Prodprice'])) {
	$p = $_POST['Prodprice'];
} else {
	$p = FALSE;
	echo '<p><font color="red">Please enter the products\'s price!</font></p>';
}

// Check for a description (not required).
if (!empty($_POST['Proddesc'])) 
{
$d = escape_data($_POST['Proddesc']);
} 
	else 
	{
	$d = '<i>No description available.</i>';	
	} 

if(!isset($pn) || !isset($cn) || !isset($d) || !isset($p) ||!isset($i) )
{
    echo "Somthing is not working";
}
elseif (isset($pn) || isset($cn) || isset($p) || isset($d) ||isset($i) )
{

	// Add the product to the database.

	$query = "INSERT INTO product (product_name, category_name, product_price, product_desc, image_name) VALUES ('$pn','$cn', '$p','$d','$i')";
	if (mysql_query ($query,$dbc)) 
	{ // Worked.
		echo "<script> window.alert('Product added you will now be sent back to the hub')</script>";
	echo "<script type=\"text/javascript\">
	window.location = \"adminhub.php\"
	</script>";
	} 
		else 
		{ // If the query did not run OK.
		echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>'; 
		}

} 
			else 
			{ // Failed a test.
			echo '<p><font color="red">Please click <a href =\"products.php\">back</a> and try again.</font></p>';
			}

				} 
?>
</body>
</html>

 

Thanks

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.