codeigniterxml数据馈送到多个(mysql表)

jgovgodb  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(182)

天哪。。这让我很头疼。
我试图让变量sql1、sql2和sql3更新不同的mysql表。。但是sql2和sql3的“产品id”字段究竟如何与sql1关联呢?
请参见sql2和sql3中插入部分中的“id”。。我不知道如何在不生成更难看的代码的情况下替换和获取这些生成的数据。
另外,如果你们有谁知道如何让这更容易,请帮助一个家伙在这里。。我觉得这很脏。。当然有更好的方法:(

// DB Settings
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// Load data from XML file
$xml = simplexml_load_file("Datafeed.xml");

// Capture XML Data 
foreach($xml->children() as $product) { 

    // Change stock wording
    $stock = $product->stock;
    if ($stock == 'yes') {
        $stock = 'on';
    } else {
        $stock = 'off'; }

// Convert XML data and insert into MYSQL

        $sql = "INSERT INTO testshop_products (product_id, product_name, product_type, product_price, product_status) VALUES ("echo $product->code;", "echo $product->cat;", "echo $product->price;","echo $stock;")";

        $sql2 = "INSERT INTO testshop_product_details (product_id, product_color, product_image, details_status) VALUES ('id', "Generating...", "echo $product->img;","echo $stock;")";

        $sql3 = "INSERT INTO testshop_spesifications (product_id, specs_meta, specs_details, specs_slug, specs_status) VALUES ('id', General, "echo $product->img;", general, "echo $stock;")"; 

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
        $conn->close();

        if ($conn->query($sql2) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
        $conn->close();

        if ($conn->query($sql3) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
        $conn->close();
    }
y0u0uwnf

y0u0uwnf1#

write store procedure and inset all records and all table it will find.

store procedure like this
CREATE  PROCEDURE `addJobApplicant`(IN XMLINPUT 
text, OUT vresult VARCHAR(100))
BEGIN

DECLARE iCounter INT DEFAULT 1;
DECLARE maxCount INT;
SET vcompID=ExtractValue(XMLINPUT, '/ROOT/HEADER/COMPANYID[$iCounter]');
SET vjpID=ExtractValue(XMLINPUT, '/ROOT/HEADER/JOBID[$iCounter]');

insert command here

set vresult='成功插入';结束

相关问题