PHP / MYSQL如何插入/更新多个表

时间:2011-05-29 22:01:52

标签: php mysql

我正在尝试使用表单更新数据库,但它没有添加项目或更新。删除项目有效,但就是这样。我做错了什么,我该怎么做才能解决这个问题?

enter image description here 非常感谢您的帮助。

INSERT ITEM PAGE

  <?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['PART_DESC'])) {


             $PART_DESC = $row["PART_DESC"];
             $SERIAL_NUM = $row["SERIAL_NUM"];
             $RACK_NUM = $row["RACK_NUM"];
             $PART_TYPE_ID = $row["PART_TYPE_ID"];
             $PART_TYPE_DESC = $row["PART_TYPE_DESC"];
    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("SELECT PART_ID FROM PART WHERE ='$PART_ID' LIMIT 1");
    $productMatch = mysql_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    $sql = mysql_query("INSERT INTO PART (PART_DESC, SERIAL_NUM, RACK_NUM, PART_TYPE_DESC, LOCATION) 
        VALUES('$PART_DESC','$SERIAL_NUM','$RACK_NUM','$PART_TYPE_ID','$PART_TYPE_DESC',now() )") or die (mysql_error());
     $pid = mysql_insert_id();

    exit();
}

?>

<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>

编辑项目页

<?php 
// Gather this product's full information for inserting automatically into the edit form below on page
if (isset($_GET['pid'])) {
    $targetID = $_GET['pid'];
    $sql = mysql_query("SELECT PART_ID, PART_DESC, SERIAL_NUM, RACK.RACK_NUM, PART.PART_TYPE_ID, PART_TYPE_DESC, LOCATION
FROM PART
INNER JOIN PART_TYPE ON PART.PART_TYPE_ID = PART_TYPE.PART_TYPE_ID
INNER JOIN RACK ON RACK.RACK_NUM = PART.RACK_NUM
WHERE PART_ID='$targetID' LIMIT 1");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {
        while($row = mysql_fetch_array($sql)){ 

             $id = $row["PART_ID"];
             $PART_DESC = $row["PART_DESC"];
             $SERIAL_NUM = $row["SERIAL_NUM"];
             $RACK_NUM = $row["RACK_NUM"];
             $PART_TYPE_ID = $row["PART_TYPE_ID"];
             $PART_TYPE_DESC = $row["PART_TYPE_DESC"];
             $LOCATION = $row["LOCATION"];
        }
    } else {
        echo "Sorry dude that crap dont exist.";
        exit();
    }
}
?>
<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['PART_DESC'])) {

    $pid = mysql_real_escape_string($_POST['thisID']);

             $PART_DESC = $row["PART_DESC"];
             $SERIAL_NUM = $row["SERIAL_NUM"];
             $RACK_NUM = $row["RACK_NUM"];
             $PART_TYPE_ID = $row["PART_TYPE_ID"];
             $PART_TYPE_DESC = $row["PART_TYPE_DESC"];
             $LOCATION = $row["LOCATION"];

    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("UPDATE PART SET PART_DESC='$PART_DESC', SERIAL_NUM='$SERIAL_NUM', PART.PART_TYPE_DESC='$PART_TYPE_DESC', RACK.RACK_NUM='$RACK_NUM', LOCATION='$LOCATION' 
    INNER JOIN PART_TYPE ON PART.PART_TYPE_ID = PART_TYPE.PART_TYPE_ID
INNER JOIN RACK ON RACK.RACK_NUM = PART.RACK_NUM WHERE PART.PART_ID='$pid'");
    header("location: inventory.php"); 
    exit();
}

&GT;

1 个答案:

答案 0 :(得分:1)

这段代码看起来很奇怪:

INSERT INTO PART 
  (PART_DESC, SERIAL_NUM, RACK_NUM, PART_TYPE_DESC, LOCATION) 
VALUES
('$PART_DESC','$SERIAL_NUM','$RACK_NUM','$PART_TYPE_ID','$PART_TYPE_DESC',now())"

parts表只有字段:

 part_id
 , part_desc
 , serial_num
 , rack_num
 , part_type_id

所以这些是你可以插入的唯一字段。外键不会神奇地将插页分散到您必须自己完成的多个表中 外键会阻止您在表格中插入悬空数据(不链接到任何内容的FK),即IT。

如何通过一次插入仍然可以做到你想要的东西
如果您一次完成插入操作,(我推荐)执行以下操作:
创建一个blackhole表,其中包含您要插入的所有字段 在黑洞表上创建一个after insert trigger并在其中执行插入逻辑 这将自动将所有插入逻辑放在一个事务中,因此即使您使用的是MyISAM表,您仍然可以进行ACID安全交易: - )。

/*create the blackhole table, adjust field types to match your tables*/
CREATE TABLE  `test`.`bhpartsinsert` (
  `part_desc` varchar(255) NOT NULL,
  `serial_num` varchar(45) NOT NULL,
  `part_type_desc` varchar(45) NOT NULL,
  `location` varchar(45) NOT NULL,
  PRIMARY KEY (`part_type_desc`)
) ENGINE=BLACKHOLE DEFAULT CHARSET=latin1;

DELIMITER $$

CREATE TRIGGER ai_bhparts_insert_each AFTER INSERT 
  ON bhpartsinsert FOR EACH ROW
BEGIN
  DECLARE mypart_type_id INTEGER;
  DECLARE myrack_num INTEGER;

  SELECT part_type_id INTO mypart_type_id FROM part_type 
    WHERE part_type_desc = new.part_type_desc LIMIT 1;
  IF mypart_type_id IS NULL THEN BEGIN 
    INSERT INTO part_type VALUES (null, new.part_type_desc);
    SELECT LAST_INSERT_ID() INTO mypart_type_id;
  END; END IF;

  SELECT rack_num INTO myrack_num FROM rack 
    WHERE location = new.location LIMIT 1;
  IF myrack_num IS NULL THEN BEGIN
    INSERT INTO rack VALUES (null, new.location);
    SELECT LAST_INSERT_ID() INTO myrack_num;
  END; END IF;

  INSERT INTO part VALUES 
    (null, new.part_desc, new.serial_num, myrack_num, mypart_type_id);
END $$

DELIMITER ;

如何运作
无论你插入黑洞表中的是什么都会被遗忘,触发器会被解雇 所以我做了一个触发器,每次插入每一行后都会触发 触发器确保机架中有一个位置,并确保有一个part_type,然后在零件表中正确插入。
我假设您的主键字段是自动递增整数。如果您插入null MySQL将为您创建一个独特的PK 仅使用null0有时可行,有时不依赖于您的配置,null始终有效

BTW,请不要使用全部大写字母作为你的字段名称,如果你已经习惯了SQL语法,其中KEYWORDS都是大写的,这会让你头疼吗