PHP PDO参数号无效

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (92)

下面是我的代码,我无法解决这个错误,我确实想把我所有的应用程序放在这里,但它太长了我不能这样我把它放在驱动器我只知道编程的基本和我的应用程序不甚至工作,所以没有什么可担心的https://drive.google.com/open?id=1n6q9PwPBXpJd1tmgR3nfXg_Caeba8g_g这是我的完整应用程序我知道你不需要所有的代码只是相关但我不知道什么是错的所以我不知道什么是相关的我会在你帮助时删除它,如果有必要的话谢谢大家。

<?php

 //link files

require 'db.php';
$message = '';
//get user input from the form

if (isset ($_POST['placa'])  &&  isset ($_POST['serialcarroria'])  &&  isset ($_POST['serialchasis'])  && isset ($_POST['serialmotor']) &&
 isset ($_POST['marca'])  &&  isset ($_POST['modelo'])  &&  isset ($_POST['year'])  && 
 isset ($_POST['color'])  &&  isset ($_POST['tipo'])  &&  isset ($_POST['uso'])  &&
 isset ($_POST['nropuestos'])  &&  isset ($_POST['nroejes'])  &&  isset ($_POST['capcarga'])  &&
 isset ($_POST['servicio']) ) {

 $placa = $_POST['placa'];
$serialcarroria = $_POST['serialcarroria'];
$serialchasis = $_POST['serialchasis'];
$serialmotor = $_POST['serialmotor'];
$marca = $_POST['marca'];
$modelo = $_POST['modelo'];
$year = $_POST['year'];
$color = $_POST['color'];
$tipo = $_POST['tipo'];
$uso = $_POST['uso'];
$nropuestos = $_POST['nropuestos'];
$nroejes = $_POST['nroejes'];
$capcarga = $_POST['capcarga'];
$servicio = $_POST['servicio'];  
      //SQL - add data to database
 $sql = 'INSERT INTO vehiculo(placa,serialcarroria,serialchasis,serialmotor,marca,modelo,year,color,tipo,uso,nropuestos,nroejes,capcarga,servicio) VALUES(:placa,:serialcarroria,:serialchasis,:serialmotor,:marca,:modelo,:year,:color,:tipo,:uso,:nropuestos,:nroejes,:capcarga,:servicio)';




  $statement = $connection->prepare($sql); //////<-PROBLEM ON THIS LINE//////
  if ($statement->execute([':placa' => $placa, ':serialcarroria' => $serialcarroria , ':serialchasis' => $serialchasis , ':serialmotor' => $serialmotor, ':marca' => $marca, ':modelo' => $modelo,':year' => $year, ':color' => $color, ':tipo ' => $tipo ,':uso ' => $uso  , ':nropuestos ' => $nropuestos , ':nroejes  ' => $nroejes  , ':capcarga ' => $capcarga  , ':servicio ' => $servicio]))  {
    $message = 'data inserted successfully';
  }

else {
  $message = 'Sorry, there has been a problem inserting your details..';
}

}
提问于
用户回答回答于

请尝试下面的代码,看看它是否有效。让我知道你得到了什么错误

在你<form>刚刚放置一个名为id的隐藏文本框,如下所示:

<form>
    <input type="hidden" name="id">
</form>

然后在你的PHP中执行以下操作:

<?php

$host = "localhost";
$user = "db_user";
$pass = "db_pass";
$db = "db_name";
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
    $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ];
    $connection = new PDO($dsn, $user, $pass, $opt);

    if (isset($_POST['placa'])) {
        //I don't see a reason to have all of those isset...just make it so that there's one field that 
        //is absolutely required and cannot be left empty and work from there.

        $statement = $connection->prepare("INSERT INTO vehiculo (id, placa, serialcarroria, serialchasis, serialmotor, marca, modelo, year, color, tipo, uso, nropuestos, nroejes, capcarga, servicio)
        VALUES (:id, :placa, :serialcarroria, :serialchasis, :serialmotor, :marca, :modelo, :year, :color, :tipo, :uso, :nropuestos, :nroejes, :capcarga, :servicio)");
        $statement->bindParam(':id', $id);
        $statement->bindParam(':placa', $placa);
        $statement->bindParam(':serialcarroria', $serialcarroria);
        $statement->bindParam(':serialchasis', $serialchasis);
        $statement->bindParam(':serialmotor', $serialmotor);
        $statement->bindParam(':marca', $marca);
        $statement->bindParam(':modelo', $modelo);
        $statement->bindParam(':year', $year);
        $statement->bindParam(':color', $color);
        $statement->bindParam(':tipo', $tipo);
        $statement->bindParam(':uso', $uso);
        $statement->bindParam(':nropuestos', $nropuestos);
        $statement->bindParam(':nroejes', $nroejes);
        $statement->bindParam(':capcarga', $capcarga);
        $statement->bindParam(':servicio', $servicio);

        $id = $_POST['id'];
        $placa = $_POST['placa'];
        $serialcarroria = $_POST['serialcarroria'];
        $serialchasis = $_POST['serialchasis'];
        $serialmotor = $_POST['serialmotor'];
        $marca = $_POST['marca'];
        $modelo = $_POST['modelo'];
        $year = $_POST['year'];
        $color = $_POST['color'];
        $tipo = $_POST['tipo'];
        $uso = $_POST['uso'];
        $nropuestos = $_POST['nropuestos'];
        $nroejes = $_POST['nroejes'];
        $capcarga = $_POST['capcarga'];
        $servicio = $_POST['servicio'];

        if ($statement->execute()) {
            echo 'data inserted successfully';
        } else {
            echo 'Sorry, there has been a problem inserting your details..';
        }
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}

$connection = null;
?>

扫码关注云+社区

领取腾讯云代金券