Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Empty Form Field Validation?

  • This topic is empty.
Viewing 13 posts - 46 through 58 (of 58 total)
  • Author
    Posts
  • #160182
    MBM
    Participant

    Thank you for the code.

    I spent a week trying to get the other code to work so have been playing around with other scripts.

    The validation part of your code works but there are a couple of issues. I have id as a PK. How do I pass a non value, bypass (I don’t know the correct term) this field so the script doesn’t generate an error?

    You’re using the PDO method to connect to a DB. I would prefer to use my existing connection script as all the other pages in my website connect to it in that manner (and are not written in PDO) and I imagine if I switch over to the PDO script I will have to do a lot of debugging e.g. the I cannot login as usernames and passwords are no longer recognised, due to this statement in my authenticate script?

    $Link = mysql_connect($Host, $User, $Password);
    

    I wanted to get everything working before re-writing my code, which is one of the reasons I have been using older methods for validation.

    #160187
    __
    Participant

    The validation part of your code works but there are a couple of issues. I have id as a PK. How do I pass a non value, bypass (I don’t know the correct term) this field so the script doesn’t generate an error?

    Simply by omitting the column name from the query. If you notice, the query I wrote specifies each column name it uses: the id is not included, so it won’t be passed a value.

    This will only cause an error if the field has no default value. If your PK is an auto_increment field, then this won’t be a problem.

    I cannot login as usernames and passwords are no longer recognised, due to this statement in my authenticate script?

    $Link = mysql_connect($Host, $User, $Password);

    Connecting to PDO will not be affected, in any way, by any mysql_* functions you call. If you are unable to connect, then the problem lies elsewhere. Did you edit the database credentials for the get_PDO_connection function?

    • $DB_host: The name of your database host. Likely the same (though I cannot guarantee it) as $Host in your current script.
    • $DB_name: The name of the database to use. Likely the same as $DBname.
    • $DB_user: The database username. Likely the same as $User.
    • $DB_pass: The database password. Likely the same as $Pass.

    I wanted to get everything working before re-writing my code, which is one of the reasons I have been using older methods for validation.

    I understand. If you still wish to do so, that’s okay. I can only recommend that you read the manual entries very closely, since there are situations where choosing one function over the other —such as using mysql_escape_string instead of mysql_real_escape_string— will create serious problems. In cases like this, I cannot help you learn how to use them correctly, because there is no “correct way” to use them.

    As I said, use mysql_real_escape_string (not mysql_escape_string) and mysql_select_db + mysql_query (not mysql_db_query); otherwise, there’s nothing particularly wrong with your db code. Feel free to remove the PDO stuff and use your old code if you wish. It can go in the same spot.

    If you still get an error, please describe it.

    If you need a reference for the old mysql_* functions, use the PHP manual (and read all the warnings carefully). Except for using die to handle errors, this ext/mysql tutorial is fairly decent.

    #160191
    MBM
    Participant

    I can connect to the database the problem happens when trying to insert.

    This first error :

    Fatal error: Call to undefined function connect() on line 34 which is :

    $DB = connect();

    If I remove it I then get :

    Notice: Undefined variable: DB on line 38 Fatal error: Call to a member function prepare() on a non-object on line 38

    $stmt = $DB->prepare( $SQL );

    This is using your connectPDO.php file to connect to the database. I get the same errors using my connect script.

    I’ve spent more time getting the damn form to validate than all the other code combined. It should be simple to display errors for empty fields regardless of what code is used.

    #160199
    __
    Participant

    Fatal error: Call to undefined function connect() on line 34 which is $DB = connect();

    line 34 in my gist is:

    $DB = get_PDO_connection();
    

    Are you using this, or are you trying to replace the PDO code with your mysql_* code?

    It should be simple to display errors for empty fields regardless of what code is used.

    If you submit invalid values (empty values, or an invalid email address) with the form, the database-related code won’t run.

    edit: I’ve tested my code, it runs as expected.

    #160205
    MBM
    Participant

    I’ve copied you code again just in case. I get :

    Notice: Undefined variable: DBhost in connectPDO.php on line 19 Array

    Which is :

    $conn = new PDO( "mysql:host=$DBhost;dbname=$DB_name",$DB_user,$DB_pass );

    #160208
    __
    Participant

    That was a typo. (On that line, $DBhost should be $DB_host.) I’ve fixed the gist.

    #160210
    MBM
    Participant

    At last it’s working!

    Sorry to be a pain but I want to position the error messages in their respective fields in the form. I tried adding ?php echo $message;? but if I left forename blank I would see :

    Enter your forename
    Enter your surname
    Enter a username
    Enter a password
    Enter a valid email address
    

    And not just :

    Enter your forename.

    What’s the syntax for add a div class or paragraph class around the $message array within the php itself?

    OR should I store each error message in a variable and then display them in their respective fields? e.g.

    $err1["forename"] = "Enter your forename.";
    $err2["surname"] = "Enter your forename.";
    

    Then :

    <input type="text" forename="forename">
    <p>
    <?php echo $err2;?>
    </p>
    

    There wouldn’t be a need to implode using this method?

    #160211
    chrisburton
    Participant
    <?php echo "<p>".$variable."</p>"; ?>
    
    #160214
    __
    Participant

    if I left forename blank I would see : Enter your forename Enter your surname Enter a username Enter a password Enter a valid email address

    And not just : Enter your forename.

    When I leave the “forename” field blank (and fill in all the other fields correctly), I see only the “Enter your forename” message. Other combinations of missing/invalid fields also produce the expected messages. Are you using this exact code? Do all the field names in your html form match those in the code?

    I want to position the error messages in their respective fields in the form.

    So, you want the errors (if any) to be displayed next to the form input?

    Just by way of anticipation, would you also want correct inputs to be populated automatically (so the user doesn’t need to re-type everything)?

    We can modify the code to use two arrays: one for error messages, and one for valid values. When we get back to the html form, we check each array index and display the error, or valid value, as appropriate.

    updated gist (tested)

    #160231
    MBM
    Participant

    So, you want the errors (if any) to be displayed next to the form input?

    Yes I can style them myself now.

    Just by way of anticipation, would you also want correct inputs to be populated automatically (so the user doesn’t need to re-type everything)?

    No. This should be added as a tutorial to this website. There isn’t a great deal of PDO material out there. W3 schools are still using SQL. Maybe you could submit it to them as well?

    #160333
    __
    Participant

    This should be added as a tutorial to this website. There isn’t a great deal of PDO material out there. W3 schools are still using SQL. Maybe you could submit it to them as well?

    I certainly appreciate the sentiment. Two problems there:

    • W3Schools doesn’t accept user contributions or corrections.
    • W3Schools is a poor resource and I wouldn’t want to be associated with them. : )

    I’ve written tutorials at dynamicdrive.com. Perhaps I’ll write one on moving away from ext/mysql.

    Glad we got things working for you!

    #160364
    MBM
    Participant

    So am I! W3Schools is good for HTML and CSS. I agree it’s not so good for other stuff.

    Learning PHP Data Objects by Dennis Popel covers PDO but unless I’m mistaken he does nothing on forms!

    #160400
    __
    Participant

    W3Schools is good for HTML and CSS.

    A lot of its info is decent; problem is, there’s no easy way for beginners to figure out which info is good and which is bad. And even info that is not “wrong” is often oversimplified or misleading.

    If you’re careful, and take everything with a grain of salt, as it were, then it can be fine. The other big objection I have is that they sell “certifications” which are useless and for which they have no authority—they’re preying on beginners who think that paying a hundred bucks for an HTML certificate will get them a job, just because the site has “W3” in its title. It’s dishonest. But enough of my complaints! : )

    Learning PHP Data Objects by Dennis Popel covers PDO but unless I’m mistaken he does nothing on forms!

    Never heard of him, but I’ll look it up.

    Good luck!

Viewing 13 posts - 46 through 58 (of 58 total)
  • The forum ‘Back End’ is closed to new topics and replies.