php - Codeigniter XML 数据馈送到多个(MYSQL 表)

标签 php mysql codeigniter

天哪..这对我来说是一个相当头疼的问题。

我正在尝试获取变量SQL1SQL2SQL3来更新不同的mysql表..但是到底如何>SQL2SQL3 的“product_id”字段与 SQL1 关联吗?

请参阅SQL2SQL3中插入部分的'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();
    } 

最佳答案

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 = '插入成功'; 结束

关于php - Codeigniter XML 数据馈送到多个(MYSQL 表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48057395/

相关文章:

php - Yii的CJuiDatepicker点击日历后如何填充其他值

php - 按日期范围获取列的总和

php - 尝试调试 PHP Seg 错误

php - 使用 PHPUnit 测试 Guzzle 调用时无法找到包装器

php - Codeigniter 图像调整大小仅调整一张图像的大小

php - 使用 CodeIgniter 备份 MySQL 数据库

php - 如何在同一个 mysql 查询中使用 SELECT+INSERT?

c# - 身份(在数据库创建时)错误指定的 key 太长

mysql - 在 where 子句中使用 Select left

mysql - mysql问题中的IF运算符