从教程中复制 PHP 代码会在我的计算机上显示通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3983329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying PHP code from a tutorial gives Notices on my computer
提问by Qcom
I have an ecommerce site based off of this tutorial.
我有一个基于本教程的电子商务网站。
Now, in the cart.php page, whenever someone updates the quantity and proceeds to click the Update Cart
button, they are greeted with the following notices:
现在,在cart.php 页面中,每当有人更新数量并继续单击Update Cart
按钮时,他们都会收到以下通知:
Notice: Array to string conversion in /home/aquadual/public_html/fiverrproject/plaincart/library/config.php on line 51
Notice: Array to string conversion in /home/aquadual/public_html/fiverrproject/plaincart/library/config.php on line 51
Notice: Array to string conversion in /home/aquadual/public_html/fiverrproject/plaincart/library/config.php on line 51
Unknown column 'A' in 'where clause'
Here is the code in the config.php
file affecting this notice:
这是config.php
影响此通知的文件中的代码:
if (!get_magic_quotes_gpc()) {
if (isset($_POST)) {
foreach ($_POST as $key => $value) {
**$_POST[$key] = trim(addslashes($value));**
}
}
if (isset($_GET)) {
foreach ($_GET as $key => $value) {
$_GET[$key] = trim(addslashes($value));
}
}
}
The actual line being line 51 in the whole config file:
实际的行是整个配置文件中的第 51 行:
$_POST[$key] = trim(addslashes($value));
回答by Ben Torell
You actually have two problems in this error.
您实际上在此错误中有两个问题。
The first part is that you are assuming every value in your $_POST
array is a string (given your use of trim()
and addslashes()
). This is not necessarily the case -- a value in that array could also be an array. The notices are telling you that, three times, you are trying to treat an array as if it were a string. However, these are non-fatal notices that should not directly cause your page to crash.
第一部分是您假设$_POST
数组中的每个值都是一个字符串(考虑到您使用trim()
and addslashes()
)。情况并非一定如此——该数组中的值也可以是数组。通知告诉您,您尝试将数组视为字符串来处理三次。但是,这些是非致命的通知,不应直接导致您的页面崩溃。
The second error is the last line, which is probably unrelated to the first three error lines (though it could be related indirectly... 0_0). This error is an error in a SQL query somewhere, causing a fatal error, which causes the PHP code to stop executing. It looks like you are trying to limit a SELECT or UPDATE statement based on a column that doesn't exist in the table you are querying. You should check your SQL code to make sure it's working correctly. This is probably not near line 51.
第二个错误是最后一行,它可能与前三个错误行无关(尽管它可能间接相关... 0_0)。此错误是某处 SQL 查询中的错误,导致致命错误,从而导致 PHP 代码停止执行。看起来您正试图根据您查询的表中不存在的列来限制 SELECT 或 UPDATE 语句。您应该检查您的 SQL 代码以确保它正常工作。这可能不在第 51 行附近。
回答by Sam
I like to code as following
我喜欢编码如下
if (!get_magic_quotes_gpc())
{
if (!empty($_GET))
{
$_GET = addslashes_deep($_GET);
}
if (!empty($_POST))
{
$_POST = addslashes_deep($_POST);
}
$_COOKIE = addslashes_deep($_COOKIE);
$_REQUEST = addslashes_deep($_REQUEST);
}
function addslashes_deep($value)
{
if (empty($value))
{
return $value;
}
else
{
return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
}
}
回答by Cesar
Well, first of all you should post also a print_r() of your $_POST var. Anyway, there is a case where you can have an array inside a $_POST['whatever']:
好吧,首先你还应该发布你的 $_POST var 的 print_r() 。无论如何,在某些情况下,您可以在 $_POST['whatever'] 中包含一个数组:
<input type="text" name="arr[]" value="a">
<input type="text" name="arr[]" value="b">
<input type="text" name="arr[]" value="c">
this will produce:
这将产生:
$_POST => Array(
'arr' => Array(
0 => 'a',
1 => 'b',
2 => 'c'
)
)
Afaik, it's the only case
Afaik,这是唯一的情况
回答by kayee
Hi I came across a similar problem using the same tutorial.
嗨,我在使用相同的教程时遇到了类似的问题。
I found this code ...
我找到了这个代码...
if (!get_magic_quotes_gpc()) { if (isset($_POST)) { foreach ($_POST as $key => $value) { $_POST[$key] = $value; } }
if (isset($_GET)) { foreach ($_GET as $key => $value) { $_GET[$key] = $value; } }
}
posted by @Chandraveer singh from this link ... error during addslashes() function in php
由@Chandraveer singh 发布从此链接... php 中的addslashes() 函数期间出错
and replaced it with the code found in the tutorial e.g. http://www.phpwebcommerce.com/source/library/config.php
并将其替换为教程中的代码,例如http://www.phpwebcommerce.com/source/library/config.php
and it solved my problem. Hope it will help you and anyone else using the same tutorial too.
它解决了我的问题。希望它也能帮助您和其他使用相同教程的人。