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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100