LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2008, 06:25 PM   #1
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

Rep: Reputation: 36
Default value in a php variable if a form-field is empty?


Hi guys, I was trying to figure out this problem for whole day without getting anywhere. I'm quite inexperienced in php but I'm doing my best.

I have this LAMP thingie running on my server, it's a simple php driven website where i can do queries against my mysql database and also add new stuff.

For that i designed a form. This is a recording-database, and one among the fields that I fill in is a "Date" field (not a date datatype in mysql, put simply a varchar to make it more simple). What I need to do in php is to make a conditional statement so that, if I leave this field untouched (empty) and hit 'Submit' with other fields filled in properly - the date form should automatically get the value 'unknown' and the query should be done successfully with this Date=unknown value. I tried to make it work something like this (i'm only including relevant fields in the code so you get a picture what I'm talking about):

PHP Code:
$date=$_REQUEST['date'];

if(!empty(
$date) {
$date "unknown"
}

$dbadd "INSERT INTO recordings values (NULL,'$date', '$artist','$venue','$city','$countrystate','$media','$storageid')"
So basically, what I was expecting this would do was, in case I didn't fill in the date - it would do the insert query to the database with all other stuff added (according to what I filled in) + date = unknown. What it does, however, is that it does a successful addition to my mysql database but date remains empty. How do I successfully declare this variable? First I thought it was something wrong with conditional-statement itself, so I altered it in debugging purposes, writing something like:
PHP Code:
if(!empty($date) {
echo 
"No date added";
exit;

This time the program would stop without adding anything to database and print that error-message. So, the program seems to be reacting to the conditional. So it's only when I try to set the value to the variable and actually make it go through mysql - that I get my problems.

I hope I made myself clear.

Thanks in advance!
 
Old 12-18-2008, 08:00 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I think you might simply have switched the meaning of "empty()" (is empty - nothing there) with "!empty()" (is NOT empty - you DO have a value in the field).

For example:
Code:
  $date = $_REQUEST['date'];

  if(empty($date) {
    // Unknown
   $sql = "INSERT INTO recordings values (NULL,NULL,
     '$artist','$venue','$city','$countrystate','$media','$storageid')";
  }
  else {
    $sql = "INSERT INTO recordings values (NULL,'$date',
      '$artist','$venue','$city','$countrystate','$media','$storageid')"; 
  }

Last edited by paulsm4; 12-18-2008 at 08:01 PM.
 
Old 12-19-2008, 06:21 AM   #3
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

Original Poster
Rep: Reputation: 36
Hi, and thanks for the reply.
I did, of course, use empty (and not !empty) in the actual code. I just made a typo in this thread
So that was not the problem. I discovered, however, this morning, what was causing the problem - the fact that the mysql-query part of the code was stated under an "else" conditional, which simply made the program stop at the evaluation point of this $date variable and not do anything else. I got confused cause I wrote a whole bunch of nested conditional statements with rules what stuff may (not) be filled in the form. Anyway, now I solved the problem but I thank you for trying helping me out. I should really stop writing Bash scripts and PhP code late at night (same goes to posting to forums ).

Cheers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php global variable appears in every html form with firefox 3.0.3 baikonur Programming 1 11-02-2008 05:16 AM
how to POST variable in PHP without using a form RMLinux Linux - Newbie 1 09-25-2008 01:53 AM
strange value of variable after a php form jeanpba Linux - Software 0 11-15-2005 02:38 AM
lsmod:used by field is empty? Sailaja Reddy Red Hat 0 08-24-2004 09:30 AM
Apache, php auto parameter variable empty lanzdale Programming 2 09-04-2003 05:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration