SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    limiting text field length - doesnt work?

    Hi, i have a text field in one of my tables, i want it to be limited to only 3 characters. i set this using phymyadmin by changing the field, and changing "length/values" to 3. however when i insert data into it from a form on my site, if i put in more than 3 characters it still allows this to be entered in the table. it doesnt seem like its actually limited the field length. how do i do this?
    thanks

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    if it's a TEXT field, it will always hold 65K bytes

    perhaps you wanted a CHAR(3) field?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that has worked to a certain extent cos it only allows 3 characters in, but it doesnt say any errors occur so someone will just think everything they entered got recorded. how can i display an error message to say they cant have more than 3 characters? i already have messages for not filling in the form, which i used this for:

    if (!filled_in($_POST))
    {
    $_POST = $errorList[$count] = "Invalid entry: You did not fill in the form! Your results were not recorded. Close this window and hit submit quiz to do it again.";

    what kind of thing would i need for checking its no more than 3 characters?
    im extremely new to all this, sorry!

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    moving to php forum ...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    you need to check length of the entry

    check this function out strlen.
    -- Jelena --

  6. #6
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(strlen($watever) > 3) {
    echo 
    "Error: Max is 3 characters.";

    ...or grab only 3 first...

    PHP Code:
    if(strlen($watever) > 3) {
    $watever substr($watever,0,3);

    Shadowed Works | Shadowed Portal
    Experience: C/C++/C#, DirectX, OpenGL, HTML, PHP, PERL, VB6, ASM

  7. #7
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shadowed
    PHP Code:
    if(strlen($watever) > 3) {
    echo 
    "Error: Max is 3 characters.";

    ...or grab only 3 first...

    PHP Code:
    if(strlen($watever) > 3) {
    $watever substr($watever,0,3);

    okay thanks that works i incorporated it into my code. i have all errors put in an error list array and they are output at the end if there arent any. If there arent any errors the data is automatically inserted into the database if not the errors are shown. at the bottom of the page i have html to say thanks for submitting your results, however this obviously shows whether there were errors or not, is there a way i can only show this message if the results actually WERE put into the database? at the moment i resorted to putting "if there are no errors listed above your results have been recorded" which is obviouslty a bit sloppy.

    heres some of my code for that part

    // no errors? process
    if (sizeof($errorList) == 0)
    {

    // generate and execute query
    $query = "INSERT INTO test1 (total, correct, accuracy, name, form) VALUES ('".$_POST['total']."', '".$_POST['correct']."',

    '".$_POST['accuracy']."', '".$_POST['name']."', '".$_POST['form']."')";
    $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error() . "");

    }
    else
    {
    // errors found
    // print as list
    echo "<font size=-1>The following errors were encountered: <br>";
    echo "<ul>";
    for ($x=0; $x<sizeof($errorList); $x++)
    {
    echo "<li>$errorList[$x]";
    }
    echo "</ul></font>";
    }
    ?>

    <html>
    <head>
    <title>Submit Results</title>
    <meta http-equiv="refresh" content="7;URL=javascript:window.close();">
    </head>
    <body>
    Thank you for completing the test and submitting your results! As long as there
    are no errors listed, your results are recorded now!<br />
    This window will close automatically.
    </body>
    </html>

  8. #8
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know if im understanding your problem but it sounds like you can just limit the max length of the text field in the form.
    HTML Code:
    <input type="text" name="num" size="10" maxlength="3" value="" />


Bookmarks

Posting Permissions

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