...

View Full Version : having trouble trying to INSERT information into my table...



DiarYofaMadmaN
05-20-2004, 04:18 AM
<?PHP
include 'dbconnect-include.php';

$TableName = maxforcepc;

$link = mysql_connect ($Host, $User, $Password);
mysql_select_db($DBName, $link);

$Query = "INSERT INTO products (Cat_ID, Sub_Cat_ID, Product_Number, Product, Man_ID, Descriptions, In_Stock, Show_Quanity, Quanity, Cost, Sell_Price, Taxable, Tax_Price, Dementions, Weight) VALUES ('$_POST['Category']', '$_POST['Subcategory']', '$_POST['ProductNumber']', '$_POST['ProductName']', '$_POST['Manufacturers']', '$_POST['Descriptions']', '$_POST['InStockCheck']', '$_POST['ShowQCheck']', '$_POST['Quanity']', '$_POST['OurCost']', '$_POST['SellPrice']', '$_POST['TaxableCheck']', '$_POST['TaxPrice']', '$_POST['Dementions']', '$_POST['Weight']')";
$Result = mysql_query ($Query) or die (mysql_error());

$validate = array('Category' , 'Subcategory' , 'ProductNumber' , 'ProductName' , 'Manufacturers' , 'Descriptions' , 'Quantity' , 'OurCost' , 'SellPrice' , 'TaxPrice' , 'Dementions' , 'Weight') ;
$numbers = array('ProductNumber' , 'Quanity' , 'OurCost' , 'SellPrice' , 'TaxPrice' , 'Dementions' , 'Weight');

foreach($validate as $k=> $v) {
if( ( empty( $_POST[$k] ) ) && ( in_array( $v, $numbers ) ) ) {
$errs[] = "Field ".$v." is required to be filled out in numberic form.";
}elseif( empty( $_POST[$k] ) ) {
$errs[] = "Field ".$v." is required to be filled out.";
}
}

if( isset( $errs ) ){
echo implode( '<br />' , $errs );
}else{
//insert code here to INSERT information to database
}

?>
The error checking to make sure nothing is left out works great until I added the query statement or something... I'm not really too sure what the problem is here. A quick explaination of what It's doing is: Gets the variables from the form as you can tell by the sql query statement, checks for anything blank if there isn't then execute the query statement which I'm not too sure how to do that. Any help would be greatly appreciated thanks :-)

-peace

SDP2006
05-20-2004, 04:35 AM
Try this
<?PHP
include 'dbconnect-include.php';

$TableName = "maxforcepc";

$link = mysql_connect ($Host, $User, $Password);
mysql_select_db($DBName, $link);

$Query = "INSERT INTO `products`(`Cat_ID`,`Sub_Cat_ID`,`Product_Number`,`Product`,`Man_ID`,`Descriptions`,`In_Stock`,`Show _Quanity`,`Quanity`,`Cost`,`Sell_Price`,`Taxable`,`Tax_Price`,`Dementions`,`Weight`) VALUES ('{$_POST['Category']}','{$_POST['Subcategory']}','{$_POST['ProductNumber']}', '{$_POST['ProductName']}','{$_POST['Manufacturers']}','{$_POST['Descriptions']}','{$_POST['InStockCheck']}','{$_POST['ShowQCheck']}','{$_POST['Quanity']}','{$_POST['OurCost']}','{$_POST['SellPrice']}','{$_POST['TaxableCheck']}','{$_POST['TaxPrice']}','{$_POST['Dementions']}','{$_POST['Weight']}')";
$Result = mysql_query($Query) or die(mysql_error());

$validate = array('Category' , 'Subcategory' , 'ProductNumber' , 'ProductName' , 'Manufacturers' , 'Descriptions' , 'Quantity' , 'OurCost' , 'SellPrice' , 'TaxPrice' , 'Dementions' , 'Weight') ;
$numbers = array('ProductNumber' , 'Quanity' , 'OurCost' , 'SellPrice' , 'TaxPrice' , 'Dementions' , 'Weight');

foreach($validate as $k=> $v) {
if( ( empty( $_POST[$k] ) ) && ( in_array( $v, $numbers ) ) ) {
$errs[] = "Field ".$v." is required to be filled out in numberic form.";
}elseif( empty( $_POST[$k] ) ) {
$errs[] = "Field ".$v." is required to be filled out.";
}
}

if( isset( $errs ) ){
echo implode( '<br />' , $errs );
}else{
//insert code here to INSERT information to database
}

?> Sometimes, MySQL can be picky about entering data, so i added a ` to all the field names and { } around the variables being entered.

Hope that works.

Stevie



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum