php - 这个 PHP 和/或 MySQL 查询有什么问题?

标签 php mysql

我现在正在学习PHP和MySQL。我正在尝试一个简单的页面,我可以在其中提交一条短消息,并从服务器检索一条消息(对于这种特殊情况只是一个随机数)。

<?php

$success = false;

require_once '../../../phpIncludes/mysqlIncludes.php';
require_once '../../../phpIncludes/iphandler.php';
$creds = new MySQLLoginCredentials;
$con = $creds->ConnectToDB();

mysql_select_db("testDB", $con);

$userMsg = trim($_POST['msg']);
//The simple version for 128 Characters from the beginning of the string
$userMsg = substr($userMsg,0,128);
$userMsg = filter_var($userMsg, FILTER_SANITIZE_STRING,!FILTER_FLAG_STRIP_LOW);

$ip = encode_ip( $_SERVER['REMOTE_ADDR'] );

$time = time();

$returnMsg = "". rand() . "";

$userAgent = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
//Trim to 256 since that is largest db can hold
$userAgent = substr($userAgent,0,256);
$userAgent = filter_var($userAgent, FILTER_SANITIZE_STRING);

//Debug 
echo "Time : " . $time . "<br>"
. " IP: " . $ip . " | " . decode_ip($ip) . "<br>"
. " UserAgent: " . $userAgent . "<br>"
. " Msg: " . $userMsg . "<br>"
. " Return: " . $returnMsg . "<br>";

$sql = "INSERT INTO TestMessageTbl (TimeStamp, IPAddress, ClientInfo, IncMsg, OutMsg)
VALUES ('" . $time . "', " . $ip . ", " . $userAgent . ", " . $userMsg . ", " . $returnMsg .")";

$success = mysql_query($sql, $con);

if($success == false)
{
    echo "Error: " . mysql_error();
}

echo $returnMsg;

mysql_close($con);
?>

输出为:

<i>Time : 1356919336
IP: * | *
UserAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko)     Chrome/23.0.1271.97 Safari/537.11
Msg: 
Return: 743166102
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.' at line 2743166102</i>

表格如下所示:

UID bigint(20) unsigned, AUTO_INCREMENT
TimeStamp bigint(20) unsigned
IPAddress varchar(32) utf8_general_ci
ClientInfo varchar(256) utf8_general_ci
IncMsg varchar(128) utf8_general_ci
OutMsg varchar(128) utf8_general_ci

我从mysql手动转录,以防有拼写错误......

让我立即感到奇怪的是错误的行号非常大。这是怎么回事?

附注我知道就目前情况而言,$msg 将是空白的

最佳答案

用简单的引用包含您的所有值(value)观

$sql = "INSERT INTO TestMessageTbl (TimeStamp, IPAddress, ClientInfo, IncMsg, OutMsg)
VALUES ('" . $time . "', '" . $ip . "', '" . $userAgent . "', '" . $userMsg . "', '" . $returnMsg ."')";

关于php - 这个 PHP 和/或 MySQL 查询有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14096749/

相关文章:

MySQLCommand ExecuteReader 行大小太大,查询在 CLI 上工作正常

sql - 当某些天的数据丢失时,如何使用 Perl 从 mysql 中检索最新的 'XYZ' 的 10 天数据,即公共(public)假期

java - Android Volley - 在字符串上调用 .length() 时抛出空指针异常,但我没有在任何地方使用 .length()

php - 打印变量和文本时不带空格?

PHP 表单提交时 SQL 查询结果在空白表中?

php - 需要修复文件上传后停止页面重新加载的问题

php - 关于EDT和时差的一些问题

php - 自动部署 PHP 应用程序

php - 在 Phpmyadmin 中创建 Concat 触发器

mysql - sql 请求问题,多个 ID