PHP脚本向一个表添加两行

时间:2014-05-09 20:59:06

标签: php mysql duplicates

以下代码在“autos”表中添加一个空行。

<? 
$Status = $_POST['Status'];
///All other variables are the same, just removed to make it more succinct ?>

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

 /////////////// LOOKS FOR DUPLICATE STOCK NUMBERS ///////////////
$check_stock = "SELECT * FROM autos WHERE StockNumber='$StockNumber'";
$checking_stock = mysql_query($check_stock);
if( mysql_num_rows($checking_stock) > 0 ) {echo "<p>Sorry, a vehicle with this Stock Number already exists. Please <a href='javascript:history.go(-1)'>Go Back</a> and try again.</p>";} 

/////////////// WRITE CAR TO DB ///////////////
  else {      

    //vehicle basics  
    $adding_vehicle = "INSERT INTO autos VALUES 
('$Status',
 '$StockNumber',
 '$VIN',
 '$Year',
 '$Make',
 '$Model',
 '$Colour',
 '$KM',
 '$PurchaseDate',
 '$Seller',
 '$PurchasePrice',
 '$Creator',
 '$Today',
 '')";
mysql_query($adding_vehicle);   
    //extended info
$add_ext = "INSERT INTO autos_notes VALUES ('$StockNumber', '$TrimLevel', '$Transmission', '$ListPrice', '$Notes', '$Disclosures', '')";
mysql_query($add_ext);  

/////////////// UPLOADS FILES ///////////////

//E-TEST
 $target = "e-test/"; 
 $target= $target. $StockNumber .'/'; 

 if (!mkdir($target, 0777, true)) {
}
 $target = $target . basename( $_FILES['etest_pdf']['name']) ; 
 $ok=1; 
 if(move_uploaded_file($_FILES['etest_pdf']['tmp_name'], $target)) 
 { 
$file_type = "E-Test";
$etest_add = "INSERT INTO upload_changes VALUES ('$Type', '$StockNumber', '$file_type', '$Etest', '')";
mysql_query($etest_add);     
 echo "The file ". basename( $_FILES['etest_pdf']['name']). " has been uploaded.<br>";
 } 
 else {
 echo "No e-test file was uploaded.<br/>";
 } 
//CARPROOF
 $target_cp = "carproof/";
 $target_cp= $target_cp. $StockNumber .'/'; 

 if (!mkdir($target_cp, 0777, true)) {
} 
 $target_cp = $target_cp . basename( $_FILES['carproof_pdf']['name']) ; 
 $ok=1; 
 if(move_uploaded_file($_FILES['carproof_pdf']['tmp_name'], $target_cp)) 
 {
$file_type = "CarProof";
$cp_add = "INSERT INTO upload_changes VALUES ('$Type', '$StockNumber', '$file_type', '$CarProof', '')";
mysql_query($cp_add);    
 echo "The file ". basename( $_FILES['carproof_pdf']['name']). " has been uploaded.<br>";
 } 
 else {
 echo "No CarProof Added.<br/>";
 } 

 //PURCHASE ORDER
 $target_po = "purchases/";
 $target_po= $target_po. $StockNumber .'/'; 

 if (!mkdir($target_po, 0777, true)) {
} 
 $target_po = $target_po . basename( $_FILES['purchase_order']['name']) ; 
 $ok=1; 
 if(move_uploaded_file($_FILES['purchase_order']['tmp_name'], $target_po)) 
 {
$file_type = "Purchase Order";
$po_add = "INSERT INTO upload_changes VALUES ('$Type', '$StockNumber', '$file_type', '$purchase_order', '')";
mysql_query($po_add);    
 echo "The file ". basename( $_FILES['purchase_order']['name']). " has been uploaded.<br>";
 } 
 else {
 echo "No Purchase Order Added.<br/>";
 } 
?>

只有插入到“autos”表中才能执行此操作,但我包含了其余的代码,以查看是否存在我错过的内容。我已经更改了$ adding_vehicle变量的名称,所以它肯定不会被多次调用。最后一个字段是auto_increment。

提前致谢!我知道这一点,所以我确信代码可以使用一些工作。

1 个答案:

答案 0 :(得分:0)

请验证您的表单方法是否为POST,否则不会填充超全局$ _POST。

调试或创建print_r($_POST)只是为了验证数组是否已填充,即超全局 $_REQUEST也填充了POST值。   您的autos表应该有一个主键(比如stocknumber或VIN plus model),因此不会插入重复或空值。您还应该在插入数据之前过滤和验证数据。