Solved

Default value not added to DB

Posted on 2013-01-25
3
146 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
  • 2
3 Comments
 
LVL 142

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 142

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now