I'm stuck with a code that gives me an error when executing the statement, I don't know if you can help me...
this is the php
if($_POST) {
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$sql_agregar = 'UPDATE usuarios SET nombre = pablo, apellidos = ramos WHERE usuarios.id = 7';
$sentencia_agregar = $pdo->prepare($sql_agregar);
$sentencia_agregar->execute(array($nombre,$apellidos));
header('location:index.php'); // Esto redirige la acción a la página actual
}
if($_GET) {
$id = $_GET['id'];
$sql_unico = 'SELECT * FROM usuarios WHERE id=7';
$gsent_unico = $pdo->prepare($sql_unico);
$gsent_unico->execute(array($id));
$resultado_unico = $gsent_unico->fetch();
}
Within this php I have a form to update the contact information of the profile that you have entered with your password.
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4"><?php ?></h1>
<div class="row justify-content-start mt-4">
<div class="col-6">
<div class="form-control">
<h3>Cambiar información de contacto</h3>
<form method="POST" class="mt-4 mb-4 col-lg-6">
<input class=" mb-3 form-control" type="text" name="nombre" placeholder="Nombre">
<input class="mb-3 form-control" type="text" name="apellidos" placeholder="Apellidos">
<input class="mb-3 form-control" type="email" placeholder="Correo electrónico">
<input class="mb-3 form-control" type="phone" placeholder="Teléfono">
<input name="id" type="hidden" value="<?php ?>">
<div class="col mt-3 text-center">
<button type="submit" class="btn btn-warning">Cambiar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
And when I update the profile data it tells me this errorFatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\phpCurso\phpyMySQL\form-sesion\profile.php:25 Stack trace: #0 C:\xampp\htdocs\phpCurso\phpyMySQL\form-sesion\profile.php(25): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\phpCurso\phpyMySQL\form-sesion\profile.php on line 25
This would be the connection.php file where the $pdo variable is saved.
<?php
$link = 'mysql:host=localhost;dbname=coloresphp';
$usuario = 'root';
$pass = '';
try {
$pdo = new PDO($link, $usuario, $pass);
// echo '<br><br>Conectado';
// foreach($pdo->query('SELECT * FROM `colores`') as $fila) {
// print_r($fila);
// }
} catch (PDOException $e) {
print "¡Error!: " . $e->getMessage() . "<br/>";
die();
}
I don't know if you can help me, because I've been stuck on the same thing for several hours.
The error:
occurs when the number of markers and parameters do not match in a prepared query.
In the code you are passing two pieces of data in the
execute()
, but there are no markers for that data in theUPDATE
.You should fix it by putting placeholders:
Or name markers, in which case the names must also go in the array: