runnerjp Posted November 10, 2009 Share Posted November 10, 2009 for some reason during my validation check i get Undefined variable although it was set within the script. Below is the script itself <?php ini_set("display_errors", "1"); error_reporting(E_ALL); $name = $_POST ['name']; $email=$_POST ['email']; $venue=$_POST ['venue']; $event=$_POST ['event']; $date=$_POST ['date']; if (!$_FILES['file']["name"]== '') { if ((($_FILES["file"]["type"] == "application/msword")) && ($_FILES["file"]["size"] < 500000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; //echo "Type: " . $_FILES["file"]["type"] . "<br />"; // echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; // echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("entrys/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "entrys/" . $_FILES["file"]["name"]); echo "Stored in: " . "entrys/" . $_FILES["file"]["type"]; } } } else { echo "Please upload word documents only!"; } } //print_r($_REQUEST['form']); ?> <div id="countrydivcontainer" style="border:1px solid gray; width:800px; margin-bottom: 1em; padding: 10px"> <? php if(isset($_POST['_submit_check'])) { foreach($_POST as $field => $value) { if (($field != 'submit') && ((!$value) || (trim($value) == ''))) { $err .= "$field cannot be empty. <br>"; $warnings[$field] ="required"; } } if (!$_POST["name"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["name"])) { $warnings["name"] = " <label for=\"uname\" class=\"error\"><em>*</em>Name can only contain letters</label>"; } if (!$_POST["venue"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["venue"])) { $warnings["venue"] = " <label for=\"uname\" class=\"error\"><em>*</em>please only user words</label>"; } if (!$_POST["event"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["event"])) { $warnings["event"] = " <label for=\"uname\" class=\"error\"><em>*</em>please only user words</label>"; } $regexp="/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i"; if (!$_POST["email"] || !preg_match($regexp, $_POST["email"])) { $warnings["email"] = " <label for=\"uname\" class=\"error\"><em>*</em>please enter your correct email address</label>"; $count = count($warnings); } if($count === 0) { if(array_key_exists('_submit_check', $_POST)) { $update = "UPDATE profile SET dob='$dob', club= '$club', first_name = '$first_name', gender = '$gender', last_name = '$last_name' WHERE ID='$id' "; $result = mysql_query($update); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $er = 'Invalid query: ' . mysql_error() . "\n"; $er .= 'Whole query: ' . $query; die($er); }} echo ' <p class="error">' . $message . '</p>' . "\n"; } if ($err){?> <div class="errors"> <p align="center"><em>Oops... the following errors were encountered:</em></p> <div align="center"><?php echo $err; ?> </div> <p align="center"> </p> <p align="center">Data has <strong>not</strong> been saved.</p> </div> <p> <?php } } ?> if ($err){?> -- this is the erro in question Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/ Share on other sites More sharing options...
Mchl Posted November 10, 2009 Share Posted November 10, 2009 $er is not $err (And you never define $err, and define $er only if MySQL error occurs) Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955110 Share on other sites More sharing options...
runnerjp Posted November 10, 2009 Author Share Posted November 10, 2009 i chnage dit to $er and still get the same result Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955133 Share on other sites More sharing options...
Mchl Posted November 10, 2009 Share Posted November 10, 2009 (And you ... define $er only if MySQL error occurs) If the query runs fine, it's undefined. Change if ($er){?> to if (isset($er)){?> Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955135 Share on other sites More sharing options...
iversonm Posted November 10, 2009 Share Posted November 10, 2009 I find myself saying this more and more now but put error_reporting(E_ALL) At the beginning of your page. Do any errors show up? Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955136 Share on other sites More sharing options...
runnerjp Posted November 10, 2009 Author Share Posted November 10, 2009 Ok that worked but now if i leave a field blank i get an error on with this <?php foreach($_POST as $field => $value) { if (($field != 'submit') && ((!$value) || (trim($value) == ''))) { $err .= "$field cannot be empty. <br>"; $warnings[$field] ="required"; ?> $err .= "$field cannot be empty. <br>"; -- Undefined variable: err @iversonm --- allready got that Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955147 Share on other sites More sharing options...
Mchl Posted November 10, 2009 Share Posted November 10, 2009 I think you want it to be $er as well. And perhaps you should just define it somewhere at the top of your script like $event=$_POST ['event']; $date=$_POST ['date']; $er = ''; //stores error messages Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955152 Share on other sites More sharing options...
runnerjp Posted November 10, 2009 Author Share Posted November 10, 2009 Ah yes that seemed to work if (!$_POST["name"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["name"])) { $er .= " <label for=\"uname\" class=\"error\"><em>*</em>Name can only contain letters</label>"; } if (!$_POST["venue"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["venue"])) { $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please only user words</label>"; } if (!$_POST["event"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["event"])) { $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please only user words</label>"; } $regexp="/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i"; if (!$_POST["email"] || !preg_match($regexp, $_POST["email"])) { $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please enter your correct email address</label>"; $count = count($er); } if($count === 0) with the above code it now displays Undefined variable: count would this not be the way to see if there are no errors Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955162 Share on other sites More sharing options...
Mchl Posted November 10, 2009 Share Posted November 10, 2009 This part seems just silly, but it would be helpful to see a few another lines. Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955165 Share on other sites More sharing options...
runnerjp Posted November 10, 2009 Author Share Posted November 10, 2009 Ok heres it all <?php ini_set("display_errors", "1"); error_reporting(E_ALL); $name = $_POST ['name']; $email=$_POST ['email']; $venue=$_POST ['venue']; $event=$_POST ['event']; $date=$_POST ['date']; $er = ''; if (!$_FILES['file']["name"]== '') { if ((($_FILES["file"]["type"] == "application/msword")) && ($_FILES["file"]["size"] < 500000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; //echo "Type: " . $_FILES["file"]["type"] . "<br />"; // echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; // echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("entrys/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "entrys/" . $_FILES["file"]["name"]); echo "Stored in: " . "entrys/" . $_FILES["file"]["type"]; } } } else { echo "Please upload word documents only!"; } } //print_r($_REQUEST['form']); ?> <div id="countrydivcontainer" style="border:1px solid gray; width:800px; margin-bottom: 1em; padding: 10px"> <? if(isset($_POST['_submit_check'])) { foreach($_POST as $field => $value) { if (($field != 'submit') && ((!$value) || (trim($value) == ''))) { $er .= "$field cannot be empty. <br>"; $warnings[$field] ="required"; } } if (!$_POST["name"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["name"])) { $er .= " <label for=\"uname\" class=\"error\"><em>*</em>Name can only contain letters</label>"; } if (!$_POST["venue"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["venue"])) { $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please only user words</label>"; } if (!$_POST["event"] || !preg_match("/^[a-zA-Z ]+$/", $_POST["event"])) { $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please only user words</label>"; } $regexp="/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i"; if (!$_POST["email"] || !preg_match($regexp, $_POST["email"])) { $er .= " <label for=\"uname\" class=\"error\"><em>*</em>please enter your correct email address</label>"; $count = count($er); } if($count === 0) { if(array_key_exists('_submit_check', $_POST)) { echo 'update'; //$update = "INSERT into results SET name='$name', venue= '$venue', date = '$date'"; //define the receiver of the email $to = '[email protected]'; //define the subject of the email $subject = 'New event added on kuhac'; //define the message to be sent. Each line should be separated with \n $message = "Hello Jarratt!\n\nA new event has been added. The event is ".$event; //define the headers we want passed. Note that they are separated with \r\n $headers = "From: [email protected]\r\nReply-To: [email protected]"; //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); $result = mysql_query($update); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $er = 'Invalid query: ' . mysql_error() . "\n"; $er .= 'Whole query: ' . $query; die($er); }} echo ' <p class="error">' . $message . '</p>' . "\n"; } if($er){?> <div class="errors"> <p align="center"><em>Oops... the following errors were encountered:</em></p> <div align="center"><?php echo $er; ?> </div> <p align="center"> </p> <p align="center">Data has <strong>not</strong> been saved.</p> </div> <p> <?php } } ?> The use of code is to assure correct data is issued into the db thats all...if u could suggest a better way of doing it im open Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955180 Share on other sites More sharing options...
mikesta707 Posted November 10, 2009 Share Posted November 10, 2009 I find myself saying this more and more now but put error_reporting(E_ALL) At the beginning of your page. Welcome to PHP freaks =) as for OP, i would suggest instead of checking the count of $er (which, since its a string, doesn't make any sense) check if its empty, IE if (empty($er)){ Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955211 Share on other sites More sharing options...
runnerjp Posted November 10, 2009 Author Share Posted November 10, 2009 ah yes funny enought i just tried if($er === '') ... thnaks guys! Link to comment https://forums.phpfreaks.com/topic/181027-solved-undefined-variable/#findComment-955223 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.