我做了个简单的测试,数据库中只包含一个data表,有以下字段(见图)。
欲实现的功能是:在页面表单中将用户输入的数据插入Name字段,ID字段为自动编号,Time为当前时间,全部代码如下:
<?php require_once('Connections/test.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO data (name) VALUES (%s)",
GetSQLValueString($_POST['textfield'], "text"));
mysql_select_db($database_test, $test);
$Result1 = mysql_query($insertSQL, $test) or die(mysql_error());
}
mysql_select_db($database_test, $test);
$query_conn = "SELECT * FROM `data`";
$conn = mysql_query($query_conn, $test) or die(mysql_error());
$row_conn = mysql_fetch_assoc($conn);
$totalRows_conn = mysql_num_rows($conn);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test</title>
</head>
<body>
<form name="form1" method="OST" action="<?php echo $editFormAction; ?>">
<p>
<input type="text" name="textfield">
</p>
<p>
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit" value="重置">
</p>
<input type="hidden" name="MM_insert" value="form1">
</form>
</body>
</html>
<?php
mysql_free_result($conn);
?>
但提交后查看数据库中Time的值还是为0000000……
但是如果将
$insertSQL = sprintf("INSERT INTO data (name) VALUES (%s)",
GetSQLValueString($_POST['textfield'], "text"));
改为
$insertSQL = sprintf("INSERT INTO data (name,time) VALUES (%s,now())",
GetSQLValueString($_POST['textfield'], "text"));
即可获得当前系统时间。
我是开始学PHP和Mysql的,用过Asp+Access,在Access中有个Now()函数,插入一条记录后可以自动获取当前时间,而不需要在代码中进行定义,我想知道Mysql有没有类型的功能?
以上代码由DW生成,可能结构有些乱。 |