我在一张桌子上有三行三列
id type value
1 gold 1000.00
2 silver 32.21
3 platinum 1500.00
我的页面上有一个表单,其中包含三个输入来填写更新的值,并使用 ajax 将这些值发送到 update_values.php
下面的代码有效,但我很好奇是否有更有效/正确的方法来做到这一点。提前感谢您的知识提升!
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);