0

任何指针将不胜感激 - 谢谢!

我收到“列计数与第 1 行的值计数不匹配”我猜这是因为我返回的数据数组未包含在 ()、()、()、..?这怎么可能实现?

<?php 

// open the database connection
require('connection.php');

// build the string to populate the database

if (isset($_POST['submit'])) {
 $buffer = "";
 $returnedData = $_POST['data'];

 // escape

 $returnedData = mysqli_real_escape_string($connection, $returnedData);

 foreach ( $returnedData as $data )

 {
    $buffer .= $data['week_date'];
    $buffer .= $data['crew_chief']; 
    $buffer .= $data['monday_crew'];
    $buffer .= $data['tuesday_crew'];
    $buffer .= $data['wednesday_crew'];
    $buffer .= $data['thursday_crew'];
    $buffer .= $data['friday_crew'];
    $buffer .= $data['saturday_crew'];
    $buffer .= $data['sunday_crew'];
    $buffer .= $data['instrument'];
    $buffer .= $data['monday_location'];
    $buffer .= $data['tuesday_location'];
    $buffer .= $data['wednesday_location'];
    $buffer .= $data['thursday_location'];
    $buffer .= $data['friday_location'];
    $buffer .= $data['saturday_location'];
    $buffer .= $data['sunday_location'];
    $buffer .= ", ";
}

 $buffer = rtrim($buffer, ", ");


} // end if


// perform the database insert

$insertQuery = "INSERT INTO log_dates (";
$insertQuery .= " week_date, crew_chief, monday_crew, tuesday_crew, wednesday_crew, thursday_crew, friday_crew, saturday_crew, sunday_crew, instrument, monday_location, tuesday_location, wednesday_location, thursday_location, friday_location, saturday_location, sunday_location";
$insertQuery .= ") VALUES (";
$insertQuery .= "'{$returnedData}'";
$insertQuery .= ")";

$result = mysqli_query($connection, $insertQuery);



// redirect on finish

if ($result) {
    header("Location: http://www..com/");
} else {
    die("Database query failed. " . mysqli_error($connection));
} 

 // close database connection
 mysqli_close($connection);

 ?>
4

1 回答 1

4

您的代码几乎没有意义。

$returnedData = $_POST['data'];

一切都很好,花花公子,但你这样做了

$returnedData = mysqli_real_escape_string($connection, $returnedData);

它返回一个STRING。然后,您获取该字符串并尝试将其视为一个数组

foreach ( $returnedData as $data )

并从中构建另一个字符串。

然后你尝试一个 SQL 查询......但完全忽略你构建的这个新字符串

$insertQuery = "INSERT INTO log_dates (";
[[...snip...]]
$insertQuery .= "'{$returnedData}'";
$insertQuery .= ")";

使用这个现在损坏的前数组,即现在只是一个字符串。

例如,您正在构建一个看起来像的查询

INSERT INTO log_dates (long list of fields)
VALUES (a single value here)

您正在使用 mysqli ...没有理由不使用带有占位符的准备好的语句。没有必要自己尝试逃避自己的价值观,尤其是因为无论如何您都在做这部分完全错误的事情。

于 2013-07-08T17:56:32.483 回答