Jump to content

[SOLVED] Undefined variable:


runnerjp

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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 = 'jarratt@.com';
//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: kuhac@kuh.com\r\nReply-To: kuhac@kuh.com";
//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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.