<?php
session_start();
$name=$_POST['title'];
$auther=$_POST['auther'];
$kind=$_POST['kind'];
$time=date("Y-m-d H:i:s");
$res=$_POST['res'];
$uploader=$_SESSION['name'];
/*echo $name;
echo $auther;
echo $kind;
echo $time;
echo $res;
echo $uploader;*/
$host="192.168.12.50";
$user="root";
$pwd="123456";
$db=mysql_connect($host, $user, $pwd) or die('Could not connect: ' . mysql_error());
/*if($db){
echo "1";
}else{
echo "0";
}*/
mysql_select_db("xujun", $db);
mysql_query("SET NAMES gb2312", $db);
$sql="insert into docu_info (name,auther,kind,creat_time,res,uploader) values ('$name','$auther','$kind','$time','$res','$uploader')";
mysql_query($sql,$db);
mysql_close($db);?>POST提交表单,注释部分都经过验证,输入的变量都有值,数据库也连接成功了,但是为什么插不进去呢?
session_start();
$name=$_POST['title'];
$auther=$_POST['auther'];
$kind=$_POST['kind'];
$time=date("Y-m-d H:i:s");
$res=$_POST['res'];
$uploader=$_SESSION['name'];
/*echo $name;
echo $auther;
echo $kind;
echo $time;
echo $res;
echo $uploader;*/
$host="192.168.12.50";
$user="root";
$pwd="123456";
$db=mysql_connect($host, $user, $pwd) or die('Could not connect: ' . mysql_error());
/*if($db){
echo "1";
}else{
echo "0";
}*/
mysql_select_db("xujun", $db);
mysql_query("SET NAMES gb2312", $db);
$sql="insert into docu_info (name,auther,kind,creat_time,res,uploader) values ('$name','$auther','$kind','$time','$res','$uploader')";
mysql_query($sql,$db);
mysql_close($db);?>POST提交表单,注释部分都经过验证,输入的变量都有值,数据库也连接成功了,但是为什么插不进去呢?
<form action="upload.php" method="POST" enctype="multipart/form-data" name="form1">
使用方法:
<p>书名:
<input type="text" name="title"></p>
<p>作者:
<input type="text" name="auther"></p>
<p>类别:
<input type="text" name="kind"></p>
<p>路径:
<input type="file" name="file"></p>
<p>说明:
<textarea name="res" ></textarea></p>
<input type="submit" name="Submit" value="提交">
<input name="scan" type="hidden" id="up" value="true">
</form>
改成mysql_query("SET NAMES utf8", $db)試試。
看看在不
可以这么用吗!把是这个mysql_query($sql);我无语!
试了下
可以这样用!
你可以把这句话先注掉试试
mysql_query("SET NAMES gb2312", $db);
$sql="insert into docu_info (name,auther,kind,creat_time,res,uploader) values ('$name','$auther','$kind','$time','$res','$uploader')";
mysql_query($sql,$db);
下面加一句输出错误的代码:echo mysql_error();
<form action="upload.php" method="POST" name="form1">
form设置了enctype="multipart/form-data" 属性后,就是2进制传输数据了 form里面的input的值传过去的不是以2进制的方式,所以$_post就得不到值了。
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
} $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
然后将你的原句$sql="insert into docu_info name,auther,kind,creat_time,res,uploader) values ('$name','$auther','$kind','$time','$res','$uploader')";
改为如下:
$sql = sprintf("insert into docu_info (name,auther,kind,creat_time,res,uploader) values (%s, %s, %s, %s, %s, %s,%s)",
GetSQLValueString($name, "text"),
GetSQLValueString($auther, "text"),
GetSQLValueString($kind, "text"),
GetSQLValueString($time, "date"),
GetSQLValueString($res, "text"),
GetSQLValueString($uploader, "text"));
请一定要核对一下类型是否与数据库中字段一致.