duannaxin9975 2018-03-22 16:37
浏览 62
已采纳

如何更改php代码以正确绑定参数以将多个实例插入到mysql表中

After learning how to setup a environment with html, js, ajax, php and mysql I'm able to insert single instances into a table.

I'm trying to get it working with multiple entries into the table for quite a while now. I was reading several documentation pages and recommendations on here but couldn't really figure out what I'm missing.

To clarify: What I'm trying to do is to insert 2 of the same entity. I input SafetyStock and LotSize of the Material Pack thereupon, insert another Material named PET with different

There is no problem with getting the variables from ajax but i don't get the $stmt->bind_param line. What do I have to write there to make the code running?

I tried to just have two lines of stmt binding, but then it only wrote the last entry into the database. Another thing I did was execute the stmt and then overwrite the variables and then do execute again, wasn't working as well.

my actual insert.php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "scm";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// prepare and bind settingssupplychaincomponents
$stmt = $conn->prepare("INSERT INTO test1 (settingsSupplyChainComponentsId, settingsSupplyChainId, componentId, safetyStockW, lotSizeW) 
VALUES (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)");
$stmt->bind_param("iiiii", $settingsSupplyChainComponentsId, $settingsSupplyChainId, $pack, $sspack, $lspack); //Pack


// set parameters for components
$settingsSupplyChainComponentsId = '';
$settingsSupplyChainId  = $_POST['ssci'];

// set parameters for pack
$pack = 1;
$sspack = $_POST['sspack'];
$lspack = $_POST['lspack'];

// set parameters for PET
$pet = 2;
$sspet = $_POST['sspet'];
$lspet = $_POST['lspet'];


$values = array();

$stmt->execute($values);






echo "New records created successfully";

$stmt->close();
$conn->close();
?> 
  • 写回答

2条回答 默认 最新

  • duanhuang1967 2018-03-22 16:50
    关注

    you'll need to repeat the variables that should be inserted in both rows.

    $stmt->bind_param("iiiiiiiiii", 
        $settingsSupplyChainComponentsId, $settingsSupplyChainId, $pack, $sspack, $lspack, 
        $settingsSupplyChainComponentsId, $settingsSupplyChainId, $pet, $sspet, $lspet);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错