gpt4 book ai didi

php - 使用 jQuery Ajax 和准备好的语句将数据插入 MySQL 表时出现问题

转载 作者:行者123 更新时间:2023-11-29 03:27:54 25 4
gpt4 key购买 nike

我有一个 JavaScript 数组,看起来像

var arrLoadDb = ["M15", 
"1,249.99",
"12,585.56",
"277,500.00",
"2,312.64",
"6,971.06",
"17,953.23",
"48,081.03",
"1,560.44",
"7000",
"Cost, Population, Energy, Footprint",
"Black-throated Gray Warbler",
"Yes",
"Balanced",
"2015/11/21",
6]

和一个 MySQL 表 models 类似

enter image description here

现在我正在使用这个 jQuery Ajax 将数据插入到 models 表中

$('#upload-model').on('click', function(e){

var serializedArr = JSON.stringify(arrLoadDb);
var upload = $.ajax({
type: "POST",
url: "assets/app/databaseSeeder.php",
data: {data:serializedArr},
cache: false
});

databaseSeeder.php 我有

<?php
session_start();
header('Content-type: application/json');
include 'config.php';
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
$data = json_decode( $_POST['data'] );
$sql = "INSERT INTO `models` (mName, roadLength, powerLineLength, penstock, insCapacity, annFirmEnergy, costEnergy, footprint, costPerYear, eTarget, rorPriorities, species, disturbance, slider, date, userID ) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare( $sql );
$stmt->bind_param( 'sssssssssssssssi',
$data[0],
$data[1],
$data[2],
$data[3],
$data[4],
$data[5],
$data[6],
$data[7],
$data[8],
$data[9],
$data[10],
$data[11],
$data[12],
$data[13],
$data[14],
$data[15]
);

$stmt->execute();
$stmt->close();
$conn->close();

但它不会在所有凭证都正确的情况下向数据库中插入任何东西

最佳答案

您的数据库列比您提交的数据多 1 个。确保在插入之前指定所有字段,或者插入语句指定要插入的字段

关于php - 使用 jQuery Ajax 和准备好的语句将数据插入 MySQL 表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33849198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com