Insert not adding data to mysql
Moderator: General Moderators
Insert not adding data to mysql
Hi,
I've created the following table in a MySQL database.
CREATE TABLE news_table(
id int NOT NULL AUTO_INCREMENT,
title varchar(50),
author varchar(45),
date int,
shortstory text,
fullstory text,
PRIMARY KEY (id),
UNIQUE KEY (id)
);
When I try adding a record using my html form, no data is added to the database. I've checked that the submit button is named and that the form is using method="post".
NOTE: the code and html form are in the same file.
Can anyone help me ?
<?php
$relative_script_path = '.';
include("$relative_script_path/config/config.php");
mysql_connect(DBHOST,DBUSER,DBPASS)
or die("Connection unsuccessful");
if($_POST["submit"]) {
mysql_select_db(DBASE)
or die("Connection to database: unsuccessful");
$sql = "INSERT INTO news_table (id,title, author, date, shortstory,fullstory) VALUES ('',{$_POST[title]},{$_POST[author]},{$_POST[date]},{$_POST[shortstory]},{$_POST[fullstory]})";
$result = mysql_query($sql);
echo("Thank you. Your article was successfully added.");
?>
I've created the following table in a MySQL database.
CREATE TABLE news_table(
id int NOT NULL AUTO_INCREMENT,
title varchar(50),
author varchar(45),
date int,
shortstory text,
fullstory text,
PRIMARY KEY (id),
UNIQUE KEY (id)
);
When I try adding a record using my html form, no data is added to the database. I've checked that the submit button is named and that the form is using method="post".
NOTE: the code and html form are in the same file.
Can anyone help me ?
<?php
$relative_script_path = '.';
include("$relative_script_path/config/config.php");
mysql_connect(DBHOST,DBUSER,DBPASS)
or die("Connection unsuccessful");
if($_POST["submit"]) {
mysql_select_db(DBASE)
or die("Connection to database: unsuccessful");
$sql = "INSERT INTO news_table (id,title, author, date, shortstory,fullstory) VALUES ('',{$_POST[title]},{$_POST[author]},{$_POST[date]},{$_POST[shortstory]},{$_POST[fullstory]})";
$result = mysql_query($sql);
echo("Thank you. Your article was successfully added.");
?>
Code: Select all
<?php
$relative_script_path = '.';
include("$relative_script_path/config/config.php");
$connection = mysql_connect(DBHOST,DBUSER,DBPASS)
or die("Connection unsuccessful");
if($_POST["submit"]) {
mysql_select_db(DBASE)
or die("Connection to database: unsuccessful");
$sql = "INSERT INTO news_table (id,title, author, date, shortstory,fullstory) VALUES ('',{$_POST[title]},{$_POST[author]},{$_POST[date]},{$_POST[shortstory]},{$_POST[fullstory]})";
$result = mysql_query($sql,$connection);
echo("Thank you. Your article was successfully added.");
?>-Nay
Code: Select all
<?php
$relative_script_path = '.';
include("$relative_script_path/config/config.php");
$connection = mysql_connect(DBHOST,DBUSER,DBPASS)
or die("Connection unsuccessful");
if($_POST["submit"]) {
mysql_select_db(DBASE)
or die("Connection to database: unsuccessful");
$sql = "INSERT INTO news_table (id,title, author, date, shortstory,fullstory) VALUES ('',{$_POST[title]},{$_POST[author]},{$_POST[date]},{$_POST[shortstory]},{$_POST[fullstory]})";
$result = mysql_query($sql,$connection);
echo("Thank you. Your article was successfully added.");
?>Code: Select all
<?php
define("DBHOST","126.0.0.120:3306");
define("DBASE","article_db");
define("DBUSER","root");
define("DBPASS","");
?>Any idea why nothing is happening ?[/quote]
Code: Select all
<?php
$relative_script_path = '.';
include("$relative_script_path/config/config.php");
$connection = mysql_connect($DBHOST,$DBUSER)
or die("Connection unsuccessful");
if($_POST["submit"]) {
mysql_select_db($DBASE)
or die("Connection to database: unsuccessful");
$sql = "INSERT INTO news_table (id,title, author, date, shortstory,fullstory) VALUES ('',{$_POST[title]},{$_POST[author]},{$_POST[date]},{$_POST[shortstory]},{$_POST[fullstory]})";
$result = mysql_query($sql,$connection);
echo("Thank you. Your article was successfully added.");
?>-Nay
Here's the PHP code as well as the HTML form. I've made some changes, but it still doesn't add data to the database.
NOTE: When I select data from the database, everything works fine.
NOTE: When I select data from the database, everything works fine.
Code: Select all
<?php
$relative_script_path = '.';
include("$relative_script_path/config/config.php");
$connection = mysql_connect($DBHOST,$DBUSER)
or die("Connection unsuccessful: " . mysql_error());
if($_POST["submit"]) {
$title = $_POST[title];
$author = $_POST[author];
$date = $_POST[date];
$shortstory = $_POST[shortstory];
$fullstory = $_POST[fullstory];
mysql_select_db($DBASE)
or die("Connection to database unsuccessful: " . mysql_error());
$sql = "INSERT INTO news_table (id,title, author, date, shortstory,fullstory) VALUES ('',$title,$author,$date,$shortstory,$fullstory)";
$result = mysql_query($sql,$connection);
echo("Thank you. Your article was successfully added.<br />");
print(mysql_error());
mysql_close($connection);
} else {
?>Code: Select all
<form method="post" action="addnews.php" name="addnews">
<table width="90%" border="0" align="center" cellpadding="2" cellspacing="0">
<tr align="left" valign="top">
<td width="35%"> Title: </td>
<td width="65%"> <input type="text" name="title" />
</td>
</tr>
<tr align="left" valign="top">
<td> Author: </td>
<td> <input type="text" name="author" /> </td>
</tr>
<tr align="left" valign="top">
<td> Date: </td>
<td> <input type="text" name="date" /> </td>
</tr>
<tr align="left" valign="top">
<td>Short Story:</td>
<td><textarea name="shortstory" cols="50" rows="6"></textarea></td>
</tr>
<tr align="left" valign="top">
<td> Full Story: </td>
<td> <textarea name="fullstory" cols="50" rows="8"></textarea>
</td>
</tr>
<tr align="left" valign="top">
<td> </td>
<td> <input type="submit" name="submit" value="Add Article" />
</td>
</tr>
</table>
</form>Code: Select all
<?
}
?>Code: Select all
$title = $_POST[title];
$author = $_POST[author];
$date = $_POST[date];
$shortstory = $_POST[shortstory];
$fullstory = $_POST[fullstory];Code: Select all
$title = $_POST['title'];
$author = $_POST['author'];
$date = $_POST['date'];
$shortstory = $_POST['shortstory'];
$fullstory = $_POST['fullstory'];Last link from Mac's signature:
Why $foo[bar] is wrong
-Nay