Page 1 of 1
Insert not adding data to mysql
Posted: Mon Oct 06, 2003 4:17 am
by sergei
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.");
?>
Posted: Mon Oct 06, 2003 6:04 am
by 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.");
?>
You would need to set your connection as a variable ($connection) so when you have a query to execute, you can use the connection as mysql_query($sql, $connection);
-Nay
Posted: Mon Oct 06, 2003 6:59 am
by sergei
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.");
?>
I've made the changes suggested, but I'm still not getting any data added to the mysql database. Here is the config.php file that I include.
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]
Posted: Mon Oct 06, 2003 7:28 am
by Nay
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.");
?>
Sorry, my bad. The DBHOST should be as variables -> $DBHOST. And I also removed the DBPASS, since you're connecting as the root user.
-Nay
Posted: Mon Oct 06, 2003 8:05 am
by sergei
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.
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>
Posted: Mon Oct 06, 2003 9:11 am
by Nay
Code: Select all
$title = $_POST[title];
$author = $_POST[author];
$date = $_POST[date];
$shortstory = $_POST[shortstory];
$fullstory = $_POST[fullstory];
That is wrong. This is correct:
Code: Select all
$title = $_POST['title'];
$author = $_POST['author'];
$date = $_POST['date'];
$shortstory = $_POST['shortstory'];
$fullstory = $_POST['fullstory'];
Maybe that was the problem? Do you mean it doesn't insert at all or there are blank content in the columns?
Last link from Mac's signature:
Why $foo[bar] is wrong
-Nay
Posted: Mon Oct 06, 2003 9:33 am
by sergei
Thanks. I finally got everything sorted.
Posted: Mon Oct 06, 2003 9:40 am
by Nay
heh, no problem. I need to learn how to read more properly next time. I missed out three things, for three times. lol
-Nay