Solved

Default value not added to DB

Posted on 2013-01-25
3
153 Views
Last Modified: 2013-01-25
Hi all,

I have a form input box with it's value set like this:

input type = "text" value="Journal Title (Optional)" onFocus="this.value=''" name = "Journal_Title"

Open in new window


If the user does not change the title (from Journal Optional) I do not want "journal optional" to be stored in the db as the name but want this field left as blank if it has been untouched.

Here is where I add it to the DB, could someone tell me how to make this change?

$title = mysql_real_escape_string($_POST['Journal_Title']);
	$content = mysql_real_escape_string($_POST['Journal_Content']);
	
	if ( strlen($title) > 1 && strlen($content) > 1 )
	{
		// Inserting data in the database
		if ( mysql_query("INSERT INTO journal_table (user_id, entry_title, entry_text) VALUES('$username','$title','$content')") )
		{
			$_SESSION[$outcome] = "Journal successfully added!";
			?>

Open in new window


Thanks,

D
0
Comment
Question by:TLN_CANADA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38819866
you have to test that case, then:
$title = mysql_real_escape_string($_POST['Journal_Title']);
if ($title == 'Journal Title (Optional)' ) { $title = ''; }

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38819886
Thanks, here is what I have on the page now:

	$username = Phpfox::getUserBy('full_name');
	$title = mysql_real_escape_string($_POST['Journal_Title']);
	if ($title == 'Journal Title (Optional)' ) { $title = ''; }
	$content = mysql_real_escape_string($_POST['Journal_Content']);
	
	if ( strlen($title) > 1 && strlen($content) > 1 )
	{
		// Inserting data in the database
		if ( mysql_query("INSERT INTO journal_table (user_id, entry_title, entry_text) VALUES('$username','$title','$content')") )
		{
			$_SESSION[$outcome] = "Journal successfully added!";
			?>
			<script type = "text/javascript">location.href = "http://www.clearthemirror.com/searchjournal.php";</script>
			<?php
		}
		else
		{
			$_SESSION[$outcome] = "Something unexpected went wrong! Please try again later!";
		}
	}
	else
	{
		$_SESSION[$outcome] = "The entry or the title was too short!";
	}

}
?>

Open in new window


I want to allow this field to be blank when I enter it into the database if the user has not put anything in there. At the moment it is asking for a value or else it won't enter the record.

Thank you,

D
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38820287
sure, that is because of this test:
if ( strlen($title) > 1

which will "fail" as 2 lines above, we set "$title" to '' ... and anyhow, you want to allow that field to be empty, so you should remove the test for length of $title ...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question