Ander_3 Posted July 9, 2012 Share Posted July 9, 2012 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 More sharing options...
justsomeguy Posted July 9, 2012 Share Posted July 9, 2012 If it says that $dateIn is undefined, then it's undefined. It would be undefined if the item in $_POST that you're checking for when you set the variable doesn't exist. Link to comment Share on other sites More sharing options...
Guest So Called Posted July 9, 2012 Share Posted July 9, 2012 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);} 1 Link to comment Share on other sites More sharing options...
Ander_3 Posted July 9, 2012 Author Share Posted July 9, 2012 Thank you! Replacing that code took away the undefined error! Link to comment Share on other sites More sharing options...
Guest So Called Posted July 9, 2012 Share Posted July 9, 2012 YWVM. I'm sure there are many other ways to solve the problem. Link to comment Share on other sites More sharing options...
rink.attendant.6 Posted July 9, 2012 Share Posted July 9, 2012 Same concept with shorter code: $dateIn = (isset($_POST['date_in'])) ? $_POST['date_in'] : ''; Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now