Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Aug 2017
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Undefined variable:

    After submitting the form, I am getting an undefined variable for each BindValue.
    Any help or guidance appreciated.

    PHP Code:
    if ($_SERVER['REQUEST_METHOD']=='POST'
        {  
        if(isset(
    $_POST['Submit']))
            {
            require_once 
    'host.php';
            
    $err = array();
                
    //performing all validations and raising corresponding errors
              
    if (empty($_POST['Name'])) $err[] = "Firstname is required";  
              if (empty(
    $_POST['County']))  $err[] = "County is required";
              if (empty(
    $_POST['Sector']))  $err[] = "Sector is required";
              if (empty(
    $_POST['CV']))  $err[] = "CV is required";
              if (!
    filter_var($_POST['Email'],FILTER_VALIDATE_EMAIL)) 
                {
                 
    $err[] = "Wrong email format";
                if (!
    $err
                    { 
                    
    $Name htmlspecialchars($_POST['Name']);
                    
    $County htmlspecialchars($_POST['County']);
                    
    $Degrees htmlspecialchars($_POST['Degrees']);
                    
    $Sector htmlspecialchars($_POST['Sector']);
                    
    $DrivingLicences htmlspecialchars($_POST['DrivingLicences']);
                    
    $Relocate htmlspecialchars($_POST['Relocate']);
                    
    $Visa htmlspecialchars($_POST['Visa']);
                    
    $CV htmlspecialchars($_POST['CV']);
                    
    $Email htmlspecialchars($_POST['Email']);
                    }
                }
            }
        
        try
            {
            
    $Con = new PDO('mysql:host=localhost;dbname=mydb'$User$Password);
            
    $Con->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
            
            
    $InsertIntoCandidatesQuery "insert into Candidates (Name, County, Sector, Degree, DrivingLicences, Relocate, Visa, CV, Email) values (:Name, :County, :Sector, :Degree, :DrivingLicences, :Relocate, :Visa, :CV, :Email)";
            
    $InsertIntoCandidatesPrepare $Con->prepare($InsertIntoCandidatesQuery);
            
    $InsertIntoCandidatesPrepare->BindValue(':Name'$Name);
            
    $InsertIntoCandidatesPrepare->BindValue(':County'$County);
            
    $InsertIntoCandidatesPrepare->BindValue(':Sector'$Sector);
            
    $InsertIntoCandidatesPrepare->BindValue(':Degree'$Degree);
            
    $InsertIntoCandidatesPrepare->BindValue(':DrivingLicences'$DrivingLicences);
            
    $InsertIntoCandidatesPrepare->BindValue(':Relocate'$Relocate);
            
    $InsertIntoCandidatesPrepare->BindValue(':Visa'$Visa);
            
    $InsertIntoCandidatesPrepare->BindValue(':CV'$CV);
            
    $InsertIntoCandidatesPrepare->BindValue(':Email'$Email);
            
    $InsertIntoCandidatesPrepare->execute(); 
    Last edited by richarddunnebsc; 09-12-2017 at 09:16 PM.

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,314
    Thanks
    31
    Thanked 989 Times in 986 Posts
    Can not run your code - the TRY section has no end bracket and you need CATCH
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    New Coder
    Join Date
    Aug 2017
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Undefined variable:

    This is the code I have at the moment, (new to pdo). I am now getting undefined variable for each BindValue.


    PHP Code:
    if ($_SERVER['REQUEST_METHOD']=='POST'
        {  
        if(isset(
    $_POST['Submit']))
            {
            require_once 
    'host.php';
            
    $err = array();
                
    //performing all validations and raising corresponding errors
              
    if (empty($_POST['Name'])) $err[] = "Firstname is required";  
              if (empty(
    $_POST['County']))  $err[] = "County is required";
              if (empty(
    $_POST['Sector']))  $err[] = "Sector is required";
              if (empty(
    $_POST['CV']))  $err[] = "CV is required";
              if (!
    filter_var($_POST['Email'],FILTER_VALIDATE_EMAIL)) 
                {
                 
    $err[] = "Wrong email format";
                if (!
    $err
                    { 
                    
    $Name htmlspecialchars($_POST['Name']);
                    
    $County htmlspecialchars($_POST['County']);
                    
    $Degrees htmlspecialchars($_POST['Degrees']);
                    
    $Sector htmlspecialchars($_POST['Sector']);
                    
    $DrivingLicences htmlspecialchars($_POST['DrivingLicences']);
                    
    $Relocate htmlspecialchars($_POST['Relocate']);
                    
    $Visa htmlspecialchars($_POST['Visa']);
                    
    $CV htmlspecialchars($_POST['CV']);
                    
    $Email htmlspecialchars($_POST['Email']);
                    }
                }
            }
        
        try
            {
            
    $Con = new PDO('mysql:host=localhost;dbname=mydb'$User$Password);
            
    $Con->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
            
            
    $InsertIntoCandidatesQuery "insert into candidates (Name, County, Sector, Degree, DrivingLicences, Relocate, Visa, CV, Email) values (:Name, :County, :Sector, :Degree, :DrivingLicences, :Relocate, :Visa, :CV, :Email)";
            
    $InsertIntoCandidatesPrepare $Con->prepare($InsertIntoCandidatesQuery);
            
    $InsertIntoCandidatesPrepare->BindValue(':Name'$Name);
            
    $InsertIntoCandidatesPrepare->BindValue(':County'$County);
            
    $InsertIntoCandidatesPrepare->BindValue(':Sector'$Sector);
            
    $InsertIntoCandidatesPrepare->BindValue(':Degree'$Degree);
            
    $InsertIntoCandidatesPrepare->BindValue(':DrivingLicences'$DrivingLicences);
            
    $InsertIntoCandidatesPrepare->BindValue(':Relocate'$Relocate);
            
    $InsertIntoCandidatesPrepare->BindValue(':Visa'$Visa);
            
    $InsertIntoCandidatesPrepare->BindValue(':CV'$CV);
            
    $InsertIntoCandidatesPrepare->BindValue(':Email'$Email);
            
    $InsertIntoCandidatesPrepare->execute();
            }
        catch (
    Exception $err)
            {
            echo 
    $err;
            
            } 

  4. #4
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,189
    Thanks
    2
    Thanked 124 Times in 120 Posts
    If you used a proper IDE you would see you still do not have your brackets right. if(isset($_POST['Submit'])) is completely unnecessary as well as all your variables for nothing. You are also using a way outdated array syntax.

    Check out this PDO tutorial https://phpdelusions.net/pdo
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  5. #5
    New Coder
    Join Date
    Aug 2017
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have changed the code as follows:

    PHP Code:
    require_once 'host.php';
    if (
    $_SERVER['REQUEST_METHOD']=='POST'
        {  
        
        
    $Name htmlspecialchars($_POST['Name']);
        
    $County htmlspecialchars($_POST['County']);
        
    $Degrees htmlspecialchars($_POST['Degrees']);
        
    $Sector htmlspecialchars($_POST['Sector']);
        
    $DrivingLicences htmlspecialchars($_POST['DrivingLicences']);
        
    $Relocate htmlspecialchars($_POST['Relocate']);
        
    $Passport htmlspecialchars($_POST['Passport']);
        
    $CV htmlspecialchars($_POST['CV']);
        
    $Email htmlspecialchars($_POST['Email']);
            
        try
            {
            
            
    $Con = new PDO('mysql:host=localhost;dbname=mydb'$User$Password);
            
    $Con->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
            
            
    $InsertIntoCandidates $Con->prepare("INSERT INTO table(Name,County,Sector,Degrees,DrivingLicences,Relocate,Visa,CV,Email) VALUES(:Name, :County, :Sector, :Degrees, :DrivingLicences, :Relocate, :Visa, :CV, :Email)");
            
    $InsertIntoCandidates->execute(array(':Name' => $Name':County' => $County':Sector' => $Sector':Degrees' => $Degrees':DrivingLicences' => $DrivingLicences':Relocate' => $Relocate':Visa' => $Passport':CV' => $CV':Email' => $Email));
            }
        catch (
    Exception $err)
            {
            echo 
    $err;
            
            }
        } 
    For some reason its giving me undefined variable for an input type checkbox, name="Passport" and a input type file, name="CV".
    An exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'on' for column 'Relocate', which I guessing relates to the Relocate column type Boolean (tinyint(1)).
    Is there any possibility POST is not picking up some values?

  6. #6
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,189
    Thanks
    2
    Thanked 124 Times in 120 Posts
    Why are you incorrectly using htmlspecialchars? You don't mess with input. You escape output based on the context it is being used.

    When you do not set a value for a checkbox it will POST to a value of "on" when it is checked which is why you have the DB error. With checkboxes, if it is not checked it will not post anything at all so you would need additional error checking if you are expecting it for your query.

    How about posting your form.
    Last edited by benanamen; 09-13-2017 at 02:10 AM.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  7. #7
    New Coder
    Join Date
    Aug 2017
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I don't see how a checkbox value creates a B error if the table field is Boolean. MySQL sets it to tinyint(1). These are the three form fields causing problems. Undefined Variable for Passport and CV.

    Code:
    <div class="relocate">
    	<input type="checkbox" name="Relocate">&nbsp;Willing to relocate?
    </div>
    				
    <div class="visa"
    	<input type="checkbox" name="Passport">&nbsp;Require Visa?
    </div>
    				
    <div class="cv">
    	<span style="font-size: 20px;">Upload CV </span> &nbsp; <input type="file" name="CV" style="font-size: 20px;"
    </div>

  8. #8
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,189
    Thanks
    2
    Thanked 124 Times in 120 Posts
    Apparently you just completely ignored what I just posted. I told you EXACTLY what the problem is.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  9. #9
    New Coder
    Join Date
    Aug 2017
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If you mean using htmlspeacialchars wrong by not validating text fields first, I see that now, unless you are referring to something else. I don't understand what is causing the Incorrect integer value error if the checkbox has an associated Boolean field. It shouldn't matter if the checkbox is checked or not.

  10. #10
    New Coder
    Join Date
    Aug 2017
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    When you do not set a value for a checkbox it will POST to a value of "on" when it is checked
    Isn't that what its supposed to do? Why would that cause a DB error?

  11. #11
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,189
    Thanks
    2
    Thanked 124 Times in 120 Posts
    Do a var dump on your check box and see what it tells you.

  12. #12
    New Coder
    Join Date
    Aug 2017
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It was my understanding that:
    If a checkbox was checked POST was 'On', 1 or true, if unchecked POST was 'Off', 0 or false.
    As I didn't check Passport, its not in the var dump (which makes absolutely no logical sense at all)
    I did select a Sector but its blank in the var dump, why is POST not seeing the selected sector?

    How do I insert 0 or false if a checkbox is not checked?
    Last edited by richarddunnebsc; 09-13-2017 at 04:33 PM.

  13. #13
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,189
    Thanks
    2
    Thanked 124 Times in 120 Posts
    I already told you how checkboxes work. Logical or not, if it is not checked it acts as though it does not exist. It does not post anything at all. Therefore you must check whether it is set or not. If you do not set a value for the check box it will post as "on" when checked. If you want another value you need to set it.

    Your "understanding" is wrong.

  14. #14
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,043
    Thanks
    3
    Thanked 494 Times in 482 Posts
    You need to validate all the submitted data before using it.

    The validation for check-boxes/radio-buttons needs to use isset(). The following will work for individual checkbox/radio-button fields -

    PHP Code:
    $field = isset($_POST['field']) ? 0
    (The reason for this behavior is because an un-checked checkbox/radiobutton is not a 'successful' form field and only successful form field values are included in the submitted data.)

    If your select/option data was an empty string, you either have a coding error in the html markup, you have two form fields with the same name (only the value from the last field with same names is submitted), or you have some php code that's setting the submitted value to an empty string (usually by using a single =, rather than two == in a comparison.)

    Lastly, type='file' form fields will submit the data in the php $_FILES array, not in $_POST.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  15. #15
    New Coder
    Join Date
    Aug 2017
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for that.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •