[英]Update one database table column with different values
I want to update table with max three value and minimum with two values.How it would be possible to update table. 我想用最大三个值更新表,用两个值最小化表。如何更新表。 I Get values from form, there are three fields for three students.
我从表格中获取价值,三个学生可以使用三个字段。 Now all of them having same value when i update them in the database.
现在,当我在数据库中更新它们时,它们都具有相同的值。 Now i am trying this why.
现在,我正在尝试为什么。 Dont know it works or not See my page
不知道它是否有效查看我的页面
$s1_name=$_POST['s1_name'];
$s2_name=$_POST['s2_name'];
$s3_name=$_POST['s3_name'];
$query="update students SET Name=:Name
WHERE ProjectID='$id'
";
try
{
$stmt = $conn->prepare( $query );
$stmt->bindParam(':Name', $s1_name);
$stmt->bindParam(':Name', $s2_name);
$stmt->bindParam(':Name', $s3_name);
$result = $stmt->execute();
$msg = "Record updated";
}
catch(PDOException $ex)
{
$msg = $ex -> getMessage();
}
}
It does not work this way. 这种方式行不通。 The way you are doing it will result in the query only updating it for
$s3_name
. 您执行此操作的方式将导致查询仅将其更新为
$s3_name
。
You will have to do your try/catch statement for each query: 您将必须对每个查询执行try / catch语句:
<?php
$names = [$_POST['s1_name'], $_POST['s2_name'], $_POST['s3_name']];
$query = "update students SET Name=:Name WHERE ProjectID='$id'";
foreach ($names as $name) {
try
{
$stmt = $conn->prepare($query);
$stmt->bindParam(':Name', $name);
$result = $stmt->execute();
$msg = "Record updated";
}
catch(PDOException $ex)
{
$msg = $ex -> getMessage();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.