Jump to content

Undefined variable error


Ander_3
 Share

Recommended Posts

I've just begun trying to learn php and MySQL and am having problems running some example code in a beginner book.I have tried everything I know to get the program working but keep getting an 'undefined variable: dateIn' error.The code is included from the beginning of program down to the error line. The line throwing the error is typed in red. Could someone please take a look and see if I have missed something?Thank you! <?php//connect to server or return an error$link=mysql_connect('localhost', 'mysqlapp', 'pw1')or die('Could not connect: '.mysql_error());//Select a database or return an errormysql_select_db('DVDRentals', $link);//Initialize an error-related variable$error='';//initialize variables with parameters retrieved from the posted formif(isset($_POST['command'])){ $command=$_POST['command'];}if(isset($_POST['transaction_id'])){ $transactionId=$_POST['transaction_id'];}if(isset($_POST['date_due'])){ $dateDue=$_POST['date_due'];}if(isset($_POST['order_id'])){ $orderId=$_POST['order_id'];}if(isset($_POST['dvd_id'])){ $dvdId=$_POST['dvd_id'];}if(isset($_POST['date_out'])){ $dateOut=$_POST['date_out'];}if(isset($_POST['date_in'])){ $dateIn=$_POST['date_in'];}//Process the save and savenew commandsif((strcmp('save', $command)==0)||(strcmp('savenew', $command)==0)){ //Check for missing parameters if($orderId==-1){ $error.='Please select an \'Order\'<br />'; } if($dvdId==-1){ $error.='Please select a \'DVD\'<br />'; } if(($dateOut==null)||(strlen($dateOut)==0)){ $error.='Please enter a \'Date Out\' Value<br />'; } if(($dateDue==null)||(strlen($dateDue)==0)){ $error.='Please enter a \'Date Due\' Value<br />'; } if(strlen($error)==0){ //Reformat dates so that they are compatible with the MySQL format if($dateOut!=null){ $dateOut=substr($dateOut, 6, 4).'-'.substr($dateOut, 0, 2).'-'.substr($dateOut, 3, 2); } if($dateDue!=null){ $dateDue=substr($dateDue, 6, 4).'-'.substr($dateDue, 0, 2).'-'.substr($dateDue, 3, 2); } if($dateIn!=null){ $dateIn=substr($dateIn, 6, 4).'-'.substr($dateIn, 0, 2).'-'.substr($dateIn, 3, 2); }

Link to comment
Share on other sites

Guest So Called

How about handling it like this?

if(isset($_POST['date_in'])){$dateIn=$_POST['date_in'];}else $dateIn=''; ...  if($dateIn==''){$dateIn=substr($dateIn, 6, 4).'-'.substr($dateIn, 0, 2).'-'.substr($dateIn, 3, 2);}

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...