Jump to content


Photo

Insertion into MySQL 'Text' Field


  • Please log in to reply
7 replies to this topic

#1 rish1103

rish1103
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 March 2006 - 05:09 PM

I'm trying to write a simple blog script. Basically a varchar(255) title and a text field for the blog entry and it works for short one line entries. everytime the entry is more than two lines, nothing is entered into database and i also donot receive any error messages or anything. Can anyone suggest anything.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 March 2006 - 05:18 PM

Please post your code.

#3 rish1103

rish1103
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 March 2006 - 05:23 PM

[!--quoteo(post=354570:date=Mar 13 2006, 12:18 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 13 2006, 12:18 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Please post your code.
[/quote]

That's the form I use for making the entry into the blog.

                    <form action="blogin.php" method="post">
                    <font face="Arial" size="2">
                    <input type="text" name="title" size = "60" value ="Title"><br>
                    <input type="text" name="date" value="YYYY-MM-DD"><br>
                    <textarea rows="20" name="entry" cols="60">Make Entry Here</textarea>
                    <br>
                    <input type="Submit"></font>
                    </form>

Thats the script that enters the form data into the database. Like i said simple form and simple action. I am unable to understand whats wrong.

<?
$username="######";
$password="######";
$database="######";

$title=$_POST['title'];
$date=$_POST['date'];
$entry=$_POST['entry'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO blog VALUES ('','$title','$date','$entry')";
mysql_query($query);

print "<font face=\"Arial\">Thankyou for submitting your information! <a href=\"../index.php\">Click here to return home</a></font>";

mysql_close();

?>


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 March 2006 - 05:41 PM

I would add some error checking and a minimal amount of data screening:
<?php
$title=mysql_real_escape_string($_POST['title']);
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if ($_POST['date'] != '')  {
   $tmp = strtotime($_POST['date']);
   if ($tmp == -1) date = '0000-00-00';
   else $date = date('Y-m-d',$tmp);
   }
else $date = '0000-00-00';
$entry=mysql_real_escape_string($_POST['entry']);
$query = "INSERT INTO blog VALUES ('','$title','$date','$entry')";
mysql_query($query) or die('Problem with insert query: ' . $query . '<br />' . mysql_error());
?>

The date checking code will allow your users to input any valid date. (not tested)

Ken


#5 rish1103

rish1103
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 March 2006 - 05:53 PM

[!--quoteo(post=354578:date=Mar 13 2006, 12:41 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 13 2006, 12:41 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I would add some error checking and a minimal amount of data screening:
<?php
$title=mysql_real_escape_string($_POST['title']);
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if ($_POST['date'] != '')  {
   $tmp = strtotime($_POST['date']);
   if ($tmp == -1) date = '0000-00-00';
   else $date = date('Y-m-d',$tmp);
   }
else $date = '0000-00-00';
$entry=mysql_real_escape_string($_POST['entry']);
$query = "INSERT INTO blog VALUES ('','$title','$date','$entry')";
mysql_query($query) or die('Problem with insert query: ' . $query . '<br />' . mysql_error());
?>

The date checking code will allow your users to input any valid date. (not tested)

Ken
[/quote]


cool I'll add what you mentioned and see what I end up with.

#6 rish1103

rish1103
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 March 2006 - 07:01 PM

Is it possible there could be something wrong with my MySQL isntallation? I've never had this problem before. I tried what was mentioned here and it didnt help. the I downloaded a blog script from hotscripts to try and its the same with that as well. I'm relly stuck.

#7 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 13 March 2006 - 07:37 PM

[!--quoteo(post=354605:date=Mar 13 2006, 07:01 PM:name=rish1103)--][div class=\'quotetop\']QUOTE(rish1103 @ Mar 13 2006, 07:01 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Is it possible there could be something wrong with my MySQL isntallation? I've never had this problem before. I tried what was mentioned here and it didnt help. the I downloaded a blog script from hotscripts to try and its the same with that as well. I'm relly stuck.
[/quote]

Probably not.. if you can connect to your database there's not a problem..

Please replace the following with your mysql_query and repost the results:

mysql_query($query) or die('Problem with insert query: ' . $query . '<br />' . mysql_error());

Hopefully this will give us a more accurate guess as to what the problem is! [=

GOOD LUCK!


Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#8 rish1103

rish1103
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 March 2006 - 08:57 PM

the error returned comes up where it encounters the first single quote or ' how do i cure that and find out which ohter characters can cause a query to fail?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users