在一个PHP中更新多行 [英] Update Multiple Rows in one PHP

查看:99
本文介绍了在一个PHP中更新多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个表中有三行三列

I have three rows with three columns in one table

id    type       value
1     gold       1000.00
2     silver     32.21
3     platinum   1500.00

我的页面上有一个表单,其中包含三个输入来填写更新后的值,并且正在使用ajax将这些值发送到update_values.php

I have a form on my page with three inputs to fill in the updated values and am using ajax to send those values to update_values.php

下面的代码有效,但是我很好奇是否有更有效/适当的方法来做到这一点.在此先感谢您的知识提升!

The code below works, but I am curious as to whether there is a more efficient / proper way of doing this. Thanks in advance for the knowledge-boost!

PHP

//Pull data from settings update
$gold=$_POST['gold'];
$silver=$_POST['silver'];
$plat=$_POST['plat'];

//Configure and Connect to the Database
include_once('creds.php');
include_once('db_conn.php');
mysqli_select_db($con,"metals");

//Insert Data into table & return status
$select="Select * FROM metals";
$query=mysqli_query($con,$select);

$update="UPDATE metals SET value = '".$gold."' WHERE id = 1";
$update2="UPDATE metals SET value = '".$silver."' WHERE id = 2";
$update3="UPDATE metals SET value = '".$plat."' WHERE id = 3";

$query=mysqli_query($con,$update);
$query2=mysqli_query($con,$update2);
$query3=mysqli_query($con,$update3);
if($query && $query2 && $query3){
   echo "Updated!";
}
else{ echo "An error occurred!"; };
mysqli_close($con);

推荐答案

为什么不使用金属名称作为密钥?

Why not use the metal name as your key?

$update="UPDATE metals SET value = '".$gold."' WHERE type = 'gold'";

您真的需要切换到已准备好的语句.您愿意接受SQL注入

And you really need to switch to prepared statements. You're wide open to SQL injection

这篇关于在一个PHP中更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆