PHP不会将* some *的字符串写入mysql

时间:2011-11-21 03:21:43

标签: php mysql

基本上将字符串写入数据库中的条目。

字符串从$ firsthalf和$ secondhalf连接。 $ secondhalf正在从文件上传表单中提取

var_dump()重复说两个变量在mysql语句之前和之后都运行得很好。然而它只写了连接字符串的$ firsthalf部分。我会假设$ secondhalf只是null,但根据var_dump(),它不是。

这是代码。

<?php
$firsthalf="0000";
$secondhalf=($_FILES['file']['name']);
var_dump($secondhalf); //success!
$TheFileName = $firsthalf.$secondhalf;
var_dump($TheFileName); //success!

/* Write to db */
$table = "category_images";
mysql_connect($dbhost,$dbuser,$dbpassword) or die("Unable to connect to database");
@mysql_select_db($dbname) or die("Unable to select
database $dbname");
$sqlquery = "INSERT INTO $table
VALUES('$category_id','$TheFileName')";
$results = mysql_query($sqlquery);
mysql_close($link);
/* End write to database */

//Database reads the category number and the $firsthalf. 
//Magically, the $secondhalf goes disappears!

var_dump($TheFileName); //success, it's still good…wtf?

?>

<form id="Upload" action="./" enctype="multipart/form-data" method="post"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="240000"> 
    <input id="file" type="file" name="file"> 
    <input id="submit" type="submit" name="submit" value="Upload me!"> 
</form>

1 个答案:

答案 0 :(得分:0)

文件名是否包含任何特殊字符?

将第二行替换为:

$secondhalf=mysql_real_escape_string($_FILES['file']['name']);

这将确保您的文件名已针对查询正确格式化

同样Dagon指出:确保数据库中的字段类型正确且长度合适。