[英]How to update multiple rows in one table (mysql, php, pdo)
the thing is when i left text area blank, i want it to delete that entry 问题是当我将文本区域留空时,我希望它删除该条目
I have a table in which i want to update primary key (RegNo) and (Name) 我有一个要在其中更新主键(RegNo)和(Name)的表
Table students
(Primary Key) (Foreign Key)
RegNo Name Passwd ProjectID
f12345 Ali 345 1
f12346 Chris 346 1
f12347 Ameer 347 1
I am tried few ways, 我尝试了几种方法,
$names = [
['reg'=> $_POST['s1_id'], 'name'=> $_POST['s1_name']],
['reg'=> $_POST['s2_id'], 'name'=> $_POST['s2_name']],
['reg'=> $_POST['s3_id'], 'name'=> $_POST['s3_name']]
];
$query="update students SET Name=:Name WHERE RegNo=:reg And
ProjectID='$id'";
foreach ( $names as $name)
{
try
{
$stmt = $conn->prepare( $query );
$stmt->bindParam(':Name', $name['name']);
$stmt->bindParam(':reg', $name['reg']);
$result = $stmt->execute();
$msg = "Record updated";
//header("location:adminhome.php");
}
catch(PDOException $ex)
{
$msg = $ex -> getMessage();
}
}
Through this way i was able to update Name column only. 通过这种方式,我只能更新“名称”列。 How i can update both RegNo and Name. 我如何更新RegNo和Name。 I am new to back-end Programming. 我是后端编程的新手。 Don't know how to achieve this. 不知道如何实现这一目标。
Something like this? 像这样吗 I only added , RegNo=:reg
this inside the SET
because reg is already bound I think this is all you need to add. 我只在SET
添加了, RegNo=:reg
,因为reg已经绑定了,我认为这就是您需要添加的全部。
I updated my answer because it will never match when you send an updated RegNo with your data structure. 我更新了我的答案,因为当您发送带有数据结构的更新的RegNo时,它将永远不会匹配。 So you should send two RegNo, the old one, and the new one. 因此,您应该发送两个RegNo,旧的和新的。
For deleting, I define two queries, one for delete and one for the update. 对于删除,我定义了两个查询,一个查询用于删除,一个查询用于更新。 Before we update the results I check inside the foreach
if the new Students ID ( $name['reg_set']
) is empty, if it is we run the $query_delete
otherwise we will run $query_update
在更新结果之前,我在foreach
检查新的学生ID( $name['reg_set']
)是否为空,如果是,则运行$query_delete
否则将运行$query_update
$names = [
['reg'=> $_POST['s1_id'], 'reg_set'=> $_POST['s1_id_set'], 'name'=> $_POST['s1_name']],
['reg'=> $_POST['s2_id'], 'reg_set'=> $_POST['s2_id_set'], 'name'=> $_POST['s2_name']],
['reg'=> $_POST['s3_id'], 'reg_set'=> $_POST['s3_id_set'], 'name'=> $_POST['s3_name']]
];
$query_delete="DELETE FROM students WHERE RegNo=:reg And ProjectID='$id'";
$query_update="UPDATE students SET Name=:Name, RegNo=:reg_set WHERE RegNo=:reg And
ProjectID='$id'";
foreach ( $names as $name)
{
try
{
if(empty($name['reg_set'])){
$stmt = $conn->prepare( $query_delete );
$stmt->bindParam(':Name', $name['name']);
$stmt->bindParam(':reg', $name['reg']);
$result = $stmt->execute();
$msg = "Record deleted";
}else{
$stmt = $conn->prepare( $query_update );
$stmt->bindParam(':Name', $name['name']);
$stmt->bindParam(':reg', $name['reg']);
$stmt->bindParam(':reg_set', $name['reg_set']);
$result = $stmt->execute();
$msg = "Record updated";
}
//header("location:adminhome.php");
}
catch(PDOException $ex)
{
$msg = $ex -> getMessage();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.