php - 将记录插入SQL数据库

时间:2013-12-08 20:31:31

标签: php sql

您好我在使用以下php代码将记录插入SQL数据库...但代码无效..

<?php
include_once("config.php");

$academicDate = $_POST['aDate'];
$academicDescription = $_POST['aDescription'];
$academicTitle = $_POST['aTitle'];

$sql = "INSERT INTO academicnews (id, newsDate, newsDescription, newsTitle) VALUES ('', $academicDate, $academicDescription, $academicTitle)";

 if (mysql_query($sql)) {

echo "Record Aded to Database. Hit OK to add more";

 }else{
     echo "Failed to add record to database";
 }

?>

请注意,id是auto_increment number .....并且config.php文件代码是..

<?php
########## MySql details (Replace with yours) #############
$username = "root"; //mysql username
$password = "s1j55b123456789"; //mysql password
$hostname = "localhost"; //hostname
$databasename = 'dominie'; //databasename

$connecDB = mysql_connect($hostname, $username, $password)or die('could not connect to database');
mysql_select_db($databasename,$connecDB) or die(mysql_error());

?>

我如何解决这个问题(它回声无法添加记录)问题...任何帮助都会受到赞赏...提前感谢... :)

3 个答案:

答案 0 :(得分:1)

我看到的是,当您的值不是数值时,不要在值周围使用引号。

尝试:

 $sql = "INSERT INTO academicnews (id, newsDate, newsDescription, newsTitle) VALUES ('%', '$academicDate', '$academicDescription', '$academicTitle')";

答案 1 :(得分:1)

请尝试一下:

 $sql = "INSERT INTO academicnews (newsDate, newsDescription, newsTitle) VALUES ('$academicDate', '$academicDescription', '$academicTitle')";

答案 2 :(得分:1)

试试这个

 $sql = "INSERT INTO academicnews (newsDate, newsDescription, newsTitle) VALUES ($academicDate, $academicDescription, $academicTitle)" ;