我有一个关于更新一行或单个PHP-PDO数据的问题

huangapple go评论84阅读模式
英文:

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.

&lt;?php 
include &#39;dbconnection.php&#39;;
//Ottieni l&#39;ID utente
$userid=intval($_GET[&#39;id&#39;]);
$sql = &quot;SELECT id, nome, cognome, idlezione FROM allievi WHERE id=:uid&quot;;
//Prepara la query:
$query = $dbconnection-&gt;prepare($sql);
//Associa i parametri
$query-&gt;bindParam(&#39;:uid&#39;,$userid,PDO::PARAM_STR);
//Esegui la query:
$query-&gt;execute();
//Assegna i dati estratti dal database (nel passaggio precedente) a una variabile.
$results=$query-&gt;fetchAll(PDO::FETCH_OBJ);
//Per l&#39;inizializzazione del numero di serie
$cnt=1;
if($query-&gt;rowCount() &gt; 0)
{
//Nel caso in cui la query abbia restituito almeno un record, possiamo echo i record all&#39;interno di un ciclo foreach:
foreach($results as $result)
{
$Nome = $result-&gt;nome;
$Cognome = $result-&gt;cognome;
$Idlezione = $result-&gt;idlezione;	
}
}
?&gt;
&lt;?php
if(isset($_POST[&#39;submit&#39;]))
{
//Ottieni l&#39;ID utente
$userid=intval($_GET[&#39;id&#39;]);
//Imposta i valori
$nome = $_POST[&#39;nome&#39;];
$cognome = $_POST[&#39;cognome&#39;];
$idlezione = $_POST[&#39;idlezione&#39;];
//Query di aggiornamento
$sql=&quot;UPDATE alunni SET nome=:nome, cognome=:cognome, idlezione=:idlezione WHERE id=:uid&quot;;
//Prepara la query per l&#39;esecuzione
$query = $dbconnection-&gt;prepare($sql);
//Associa i parametri
$query-&gt;bindParam(&#39;:nome&#39;,$nome,PDO::PARAM_STR);
$query-&gt;bindParam(&#39;:cognome&#39;,$cognome,PDO::PARAM_STR);
$query-&gt;bindParam(&#39;:idlezione&#39;,$idlezione,PDO::PARAM_STR);
//Esegui la query
$query-&gt;execute();
//Messaggio di esecuzione
echo &quot;&lt;script&gt;alert(&#39;Alunno modificato con successo!&#39;);&lt;/script&gt;&quot;;
//Re-indirizzo 
echo &quot;&lt;script&gt;window.location.href=&#39;allievi_tab.php&#39;&lt;/script&gt;&quot;;
}
?&gt; --&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;row centered-form&quot;&gt;
&lt;div class=&quot;col-xs-12 col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4&quot;&gt;
&lt;div class=&quot;panel panel-default&quot;&gt;
&lt;div class=&quot;panel-heading&quot;&gt;
&lt;h3 class=&quot;panel-title&quot;&gt;Modifica i dati dell&#39;alunno&lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&quot;panel-body&quot;&gt;
&lt;form method=&quot;POST&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;nome&quot;&gt;Nome&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;nome&quot; id=&quot;nome&quot; class=&quot;form-control input-sm&quot; placeholder=&quot;Nome&quot; required=&quot;&quot; value=&quot;&lt;?php echo $Nome;?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;cognome&quot;&gt;Cognome&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;cognome&quot; id=&quot;cognome&quot; class=&quot;form-control input-sm&quot; placeholder=&quot;Cognome&quot; required=&quot;&quot; value=&quot;&lt;?php echo $Cognome; ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;idlezione&quot;&gt;Id Lezione&lt;/label&gt;
&lt;input type=&quot;number&quot; min=&quot;1&quot; max=&quot;10&quot; name=&quot;idlezione&quot; id=&quot;idlezione&quot; class=&quot;form-control input-sm&quot; placeholder=&quot;Inserisci il numero della lezione&quot; required=&quot;&quot; value=&quot;&lt;?php echo $Idlezione;?&gt;&quot;&gt;
&lt;/div&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Update&quot; class=&quot;btn btn-info btn-block&quot;&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;style type=&quot;text/css&quot;&gt;
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;
}
&lt;/style&gt;

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-&gt;bindParam(&#39;:uid&#39;, $userid, PDO::PARAM_INT);

huangapple
  • 本文由 发表于 2023年2月14日 19:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定