Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

PHP Code not saving to MySQL Database

Hi all,

I am having a problem with some code. It's not saving to the database.

As far as values are concerned it's all posting and it actually says that it's save successfully but nothing is saving.

Can anyone spot any problems please?

Here is the code.
 
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$date = $_POST["date"];
$textarea = $_POST["textarea"];

$query = "INSERT INTO YOUR_TABLE (date,textarea) VALUES ('" . mysql_real_escape_string($date) . "','" . mysql_real_escape_string($textarea) . "')";

$result = mysql_query($query,$link);

echo "form saved successfully!";

?>

...and this is the table

CREATE TABLE IF NOT EXISTS `tbl_calendar` (
  `id` int(11) NOT NULL auto_increment,
  `date` text NOT NULL,
  `textarea` mediumtext NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Open in new window

0
error77
Asked:
error77
  • 2
1 Solution
 
Greg AlexanderLead DeveloperCommented:
This may be a typo, but shouldn't

$query = "INSERT INTO YOUR_TABLE (date,textarea) VALUES ('" . mysql_real_escape_string($date) . "','" . mysql_real_escape_string($textarea) . "')";

be

$query = "INSERT INTO tbl_calendar (date,textarea) VALUES ('" . mysql_real_escape_string($date) . "','" . mysql_real_escape_string($textarea) . "')";

Although that may have been an example for EE
0
 
APNFSSCCommented:
Hi

Replace YOUR_TABLE with the name of the table in your insert statement.

 
$query = "INSERT INTO tbl_calendar (date,textarea) VALUES ('" . mysql_real_escape_string($date) . "','" . mysql_real_escape_string($textarea) . "')";

Open in new window


Also you need to select the database after your connect statement.

replace the word databasename with the name of the mysql database that contains the tbl_calendar table.

mysql_select_db('databaasename',$link);
0
 
error77Author Commented:
Yes, it's an example to EE ... the fields are all corrent but not saving ... I don't understand why :o/
0
 
error77Author Commented:
Thanks ... DB was missing.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now