英文:
I have a problem with updating a row or a single PHP-PDO data
问题
我无法完全理解这个问题。
我正在创建一个CRUD函数,包括插入和删除学生的功能。我在更新方面遇到了问题。它给我带来了一个错误:SQLSTATE[HY093]:无效的参数数量:绑定变量的数量与标记的数量不匹配等等等等。
我尝试使用PDO::PARAM_INT,但它总是报告相同的错误。
<?php
include 'dbconnection.php';
//获取用户ID
$userid=intval($_GET['id']);
$sql = "SELECT id, nome, cognome, idlezione FROM allievi WHERE id=:uid";
//准备查询:
$query = $dbconnection->prepare($sql);
//绑定参数
$query->bindParam(':uid',$userid,PDO::PARAM_STR);
//执行查询:
$query->execute();
//将从数据库中提取的数据(在上一步中)分配给变量。
$results=$query->fetchAll(PDO::FETCH_OBJ);
//用于序列号初始化
$cnt=1;
if($query->rowCount() > 0)
{
//如果查询返回至少一个记录,我们可以在foreach循环中打印记录:
foreach($results as $result)
{
$Nome = $result->nome;
$Cognome = $result->cognome;
$Idlezione = $result->idlezione;
}
}
?>
<?php
if(isset($_POST['submit']))
{
//获取用户ID
$userid=intval($_GET['id']);
//设置值
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$idlezione = $_POST['idlezione'];
//更新查询
$sql="UPDATE alunni SET nome=:nome, cognome=:cognome, idlezione=:idlezione WHERE id=:uid";
//准备执行查询
$query = $dbconnection->prepare($sql);
//绑定参数
$query->bindParam(':nome',$nome,PDO::PARAM_STR);
$query->bindParam(':cognome',$cognome,PDO::PARAM_STR);
$query->bindParam(':idlezione',$idlezione,PDO::PARAM_STR);
//执行查询
$query->execute();
//执行消息
echo "<script>alert('学生成功修改!');</script>";
//重新定向
echo "<script>window.location.href='allievi_tab.php'</script>";
}
?>
链接到Bootstrap的样式表:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
容器和表单部分:
<div class="container">
<div class="row centered-form">
<div class="col-xs-12 col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">修改学生信息</h3>
</div>
<div class="panel-body">
<form method="POST">
<div class="form-group">
<label for="nome">姓名</label>
<input type="text" name="nome" id="nome" class="form-control input-sm" placeholder="姓名" required="" value="<?php echo $Nome;?>">
</div>
<div class="form-group">
<label for="cognome">姓</label>
<input type="text" name="cognome" id="cognome" class="form-control input-sm" placeholder="姓" required="" value="<?php echo $Cognome; ?>">
</div>
<div class="form-group">
<label for="idlezione">课程ID</label>
<input type="number" min="1" max="10" name="idlezione" id="idlezione" class="form-control input-sm" placeholder="输入课程编号" required="" value="<?php echo $Idlezione;?>">
</div>
<input type="submit" name="submit" value="更新" class="btn btn-info btn-block">
</form>
</div>
</div>
</div>
</div>
</div>
样式:
<style type="text/css">
body{
background-color: #fff;
}
.centered-form{
margin-top: 60px;
}
.centered-form .panel{
background: rgba(255, 255, 255, 0.8);
box-shadow: rgba(0, 0, 0, 0.3) 20px 20px 20px;
}
</style>
表格的结构:
ID P.K. A.I.
NAME VARCHAR(25)
SURNAME VARCHAR(25)
IDLESSON INT(11)
英文:
I can't fully understand the problem.
I am creating a CRUD is the function pupil insert, delete as well. I'm having trouble with the update. It brings me: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens etc. etc.
I tried entering PDO::PARAM_INT but it always reports the same thing.
<?php
include 'dbconnection.php';
//Ottieni l'ID utente
$userid=intval($_GET['id']);
$sql = "SELECT id, nome, cognome, idlezione FROM allievi WHERE id=:uid";
//Prepara la query:
$query = $dbconnection->prepare($sql);
//Associa i parametri
$query->bindParam(':uid',$userid,PDO::PARAM_STR);
//Esegui la query:
$query->execute();
//Assegna i dati estratti dal database (nel passaggio precedente) a una variabile.
$results=$query->fetchAll(PDO::FETCH_OBJ);
//Per l'inizializzazione del numero di serie
$cnt=1;
if($query->rowCount() > 0)
{
//Nel caso in cui la query abbia restituito almeno un record, possiamo echo i record all'interno di un ciclo foreach:
foreach($results as $result)
{
$Nome = $result->nome;
$Cognome = $result->cognome;
$Idlezione = $result->idlezione;
}
}
?>
<?php
if(isset($_POST['submit']))
{
//Ottieni l'ID utente
$userid=intval($_GET['id']);
//Imposta i valori
$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$idlezione = $_POST['idlezione'];
//Query di aggiornamento
$sql="UPDATE alunni SET nome=:nome, cognome=:cognome, idlezione=:idlezione WHERE id=:uid";
//Prepara la query per l'esecuzione
$query = $dbconnection->prepare($sql);
//Associa i parametri
$query->bindParam(':nome',$nome,PDO::PARAM_STR);
$query->bindParam(':cognome',$cognome,PDO::PARAM_STR);
$query->bindParam(':idlezione',$idlezione,PDO::PARAM_STR);
//Esegui la query
$query->execute();
//Messaggio di esecuzione
echo "<script>alert('Alunno modificato con successo!');</script>";
//Re-indirizzo
echo "<script>window.location.href='allievi_tab.php'</script>";
}
?> -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<div class="container">
<div class="row centered-form">
<div class="col-xs-12 col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Modifica i dati dell'alunno</h3>
</div>
<div class="panel-body">
<form method="POST">
<div class="form-group">
<label for="nome">Nome</label>
<input type="text" name="nome" id="nome" class="form-control input-sm" placeholder="Nome" required="" value="<?php echo $Nome;?>">
</div>
<div class="form-group">
<label for="cognome">Cognome</label>
<input type="text" name="cognome" id="cognome" class="form-control input-sm" placeholder="Cognome" required="" value="<?php echo $Cognome; ?>">
</div>
<div class="form-group">
<label for="idlezione">Id Lezione</label>
<input type="number" min="1" max="10" name="idlezione" id="idlezione" class="form-control input-sm" placeholder="Inserisci il numero della lezione" required="" value="<?php echo $Idlezione;?>">
</div>
<input type="submit" name="submit" value="Update" class="btn btn-info btn-block">
</form>
</div>
</div>
</div>
</div>
</div>
<style type="text/css">
body{
background-color: #fff;
}
.centered-form{
margin-top: 60px;
}
.centered-form .panel{
background: rgba(255, 255, 255, 0.8);
box-shadow: rgba(0, 0, 0, 0.3) 20px 20px 20px;
}
</style>
The table is like this:
ID P.K. A.I.
NAME VARCHAR(25)
SURNAME VARCHAR(25)
IDLESSON INT(11)
答案1
得分: 0
你有4个要绑定的变量,你缺少:uid
的绑定。
英文:
You have 4 variables to bind, you are missing a binding for :uid
$query->bindParam(':uid', $userid, PDO::PARAM_INT);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论