简体   繁体   中英

php - insert record into SQL database

hi there i am using the following php code to insert a record into a SQL database... but the code doesnot work..

<?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";
 }

?>

note that the id is an auto_increment number..... and the config.php file code is..

<?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());

?>

how do i solve this (it echos failed to add record) problem... any help would be appreciated.. thanks in advance... :)

What I see is that you don't use quotes around your values, when they are not numeric values.

Try:

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

请尝试:

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

尝试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM