当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Store all errors in one table

问题描述:

In order to create an overview of my site, I want to store every action that people make.

So I have this part made:

<?php

if($errors) {

foreach($errors as $error) {

$result = mysql_query("INSERT INTO deverror (id, ip, page, message, datum) VALUES (NULL, '".$ip."', 'http://scrshot.com/dev.php', '".$error."', '".$today."')", $connection);

}

}

?>

Now I have a problem. If I make an error, it makes 3 rows with the error screen:

How can I put all those "mistakes/errors" in one row, like this?

(Note: Yes, I know I'm using mysql and it's stupid.)

网友答案:

Implode your error array with a newline.

<?php 
if($errors){
    $allErrors = implode("\n", $errors);
    $result = mysql_query("INSERT INTO deverror (id, ip, page, message, datum) VALUES (NULL, '".$ip."', 'http://scrshot.com/dev.php', '".$allErrors."', '".$today."')", $connection);
} 
?>

Obviously you'd use mysqli/PDO in a live environment.

网友答案:
<?php 
    if($errors){
        $allErrors = "";
        foreach($errors as $error) {
            $allErrors .= $error."\n";
        }
        $result = mysql_query("INSERT INTO deverror (id, ip, page, message, datum) VALUES       (NULL, '".$ip."', 'http://scrshot.com/dev.php', '".$allErrors."', '".$today."')", $connection);
    } 
?>

A bit rusty on the php-side here but that should do it, concatenate all errors before passing them to your string.

BUT BE AWARE THIS COULD BE POTENTIALLY OPEN TO SQL-INJECTIONS. CONSIDER PREPARED STATEMENTS!

分享给朋友:
您可能感兴趣的文章:
随机阅读: