简体   繁体   中英

PHP PDO Invalid parameter number

Below is my code, I am not able to resolve this error , i did wanna put all my app here but its too long and i couldn't so i put it in drive i just know the basic of programing and my app don't even work at all so there nothing to worry about https://drive.google.com/open?id=1n6q9PwPBXpJd1tmgR3nfXg_Caeba8g_g that's my full app i know you don't need all the code just the relevant but i don't know what's wrong so i don't know what's relevant i'll remove this when you help if it's necessary thanks you all.

<?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..';
}

}

Try my code below to see if it works. Let me know what errors you get

In your <form> just place a hidden textbox with a name of id like so:

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

And then in your PHP do this:

<?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;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM