Solved

Default value not added to DB

Posted on 2013-01-25
3
148 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 dynamically set the form action using jQuery.

920 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

15 Experts available now in Live!

Get 1:1 Help Now