PHP: Update multiple MySQL fields in single query(PHP:在单个查询中更新多个 MySQL 字段)
问题描述
我基本上只是想更新表中的多个值.解决此问题的最佳方法是什么?这是当前代码:
I am basically just trying to update multiple values in my table. What would be the best way to go about this? Here is the current code:
$postsPerPage = $_POST['postsPerPage'];
$style = $_POST['style'];
mysql_connect ("localhost", "user", "pass") or die ('Error: ' . mysql_error());
mysql_select_db ("db");
mysql_query("UPDATE settings SET postsPerPage = $postsPerPage WHERE id = '1'") or die(mysql_error());
我想包含的另一个更新是:
The other update I want to include is:
mysql_query("UPDATE settings SET style = $style WHERE id = '1'") or die(mysql_error());
谢谢!
推荐答案
使用逗号分隔添加多个列:
Add your multiple columns with comma separations:
UPDATE settings SET postsPerPage = $postsPerPage, style= $style WHERE id = '1'
但是,您没有清理您的输入?这意味着任何随机黑客都可能破坏您的数据库.看到这个问题:什么是清理用户输入的最佳方法用 PHP 吗?
However, you're not sanitizing your inputs?? This would mean any random hacker could destroy your database. See this question: What's the best method for sanitizing user input with PHP?
另外,样式是数字还是字符串?我假设一个字符串,所以它需要被引用.
Also, is style a number or a string? I'm assuming a string, so it would need to be quoted.
这篇关于PHP:在单个查询中更新多个 MySQL 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP:在单个查询中更新多个 MySQL 字段


基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01