?
Solved

Default value not added to DB

Posted on 2013-01-25
3
Medium Priority
?
156 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 2000 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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

771 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