PHP:在单个查询中更新多个MySQL字段 [英] PHP: Update multiple MySQL fields in single query

查看:146
本文介绍了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字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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