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

&lt;form method="post" action="addnews.php" name="addnews"&gt;

			          &lt;table width="90%" border="0" align="center" cellpadding="2" cellspacing="0"&gt;
                        &lt;tr align="left" valign="top"&gt; 
                          &lt;td width="35%"&gt; Title: &lt;/td&gt;
                          &lt;td width="65%"&gt; &lt;input type="text" name="title" /&gt; 
                          &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr align="left" valign="top"&gt; 
                          &lt;td&gt; Author: &lt;/td&gt;
                          &lt;td&gt; &lt;input type="text" name="author" /&gt; &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr align="left" valign="top"&gt; 
                          &lt;td&gt; Date: &lt;/td&gt;
                          &lt;td&gt; &lt;input type="text" name="date" /&gt; &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr align="left" valign="top"&gt;
                          &lt;td&gt;Short Story:&lt;/td&gt;
                          &lt;td&gt;&lt;textarea name="shortstory" cols="50" rows="6"&gt;&lt;/textarea&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr align="left" valign="top"&gt; 
                          &lt;td&gt; Full Story: &lt;/td&gt;
                          &lt;td&gt; &lt;textarea name="fullstory" cols="50" rows="8"&gt;&lt;/textarea&gt; 
                          &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr align="left" valign="top"&gt; 
                          &lt;td&gt; &lt;/td&gt;
                          &lt;td&gt; &lt;input type="submit" name="submit" value="Add Article" /&gt; 
                          &lt;/td&gt;
                        &lt;/tr&gt;
                      &lt;/table&gt;
			&lt;/form&gt;

Code: Select all

<?
			}                    
                    ?>

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 :D

-Nay