SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    In Cyber Space!
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with this script ...

    Hi I've got the following script working. It's not the neatest or even the slimmest version. I was curious if you guys would offer some help and better ways of doing this. I'm sure the code could be cleaned up and slimmed down. I'm also having a problem that say information is already in the DB and you just want to update some quick text. In order to do that, you have to re-enter all of the data otherwise it's over written. How would I go about getting around that?

    PHP Code:
    <form action="<?=$_SERVER['code/PHP_SELF']?>" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="204800">
    <table width="100%" border="0" cellspacing="0" cellpadding="1">
      <tr>
        <td colspan="4"><div align="center"><font size="5" face="Verdana, Arial, Helvetica, sans-serif">Police Department<br>
          Missing Person Administration</font></div></td>
      </tr>
      <tr>
        <td height="25" colspan="4"></td>
      </tr>  
      <tr>
        <td width="72"><span id=app_small_text>ID # :</span></td>
        <td width="213"><select name="id">
          <option value="0" selected>----------------------</option>
          <option value="1">ID Number 1</option>
          <option value="2">ID Number 2</option>
          <option value="3">ID Number 3</option>
          <option value="4">ID Number 4</option>
          <option value="5">ID Number 5</option>
          <option value="6">ID Number 6</option>
        </select></td>
        <td width="72"><span id=app_small_text>Height :</span></td>
        <td width="213"><input name="height" type="text" id="height"></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>Case # :</span></td>
        <td width="213"><input name="case_number" type="text" id="case_number"></td>
        <td width="72"><span id=app_small_text>Weight :</span></td>
        <td width="213"><input name="weight" type="text" id="weight"></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>Firstname :</span></td>
        <td width="213"><input name="first_name" type="text" id="first_name"></td>
        <td width="72"><span id=app_small_text>Age :</span></td>
        <td width="213"><input name="age" type="text" id="age" maxlength="2"></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>Lastname :</span></td>
        <td width="213"><input name="last_name" type="text" id="last_name"></td>
        <td width="72"><span id=app_small_text>Sex :</span></td>
        <td width="213"><input name="sex" type="text" id="sex"></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>Alias :</span></td>
        <td width="213"><input name="nickname" type="text" id="nickname"></td>
        <td width="72"><span id=app_small_text>Eye Color :</span></td>
        <td width="213"><input name="eye" type="text" id="eye"></td>
      </tr>
      <tr>
        <td width="2"><span id=app_small_text>Address :</span></td>
        <td width="213"><input name="address" type="text" id="address"></td>
        <td width="72"><span id=app_small_text>Hair Color :</span></td>
        <td width="213"><input name="hair" type="text" id="hair"></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>City :</span></td>
        <td width="213"><input name="city" type="text" id="city"></td>
        <td width="72"><span id=app_small_text>Complexion :</span></td>
        <td width="213"><input name="complexion" type="text" id="complexion"></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>State :</span></td>
        <td width="213"><input name="state" type="text" id="state"></td>
        <td width="72"><span id=app_small_text>Race :</span></td>
        <td width="213"><input type="text" name="textfield"></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>Zip :</span></td>
        <td width="213"><input name="zip" type="text" id="zip" maxlength="10"></td>
        <td width="72"><span id=app_small_text>Birthdate<br>
          (MM/DD/YYYY) :</span></td>
        <td width="213"><input name="birthdate" type="text" id="birthdate"></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>Heading :</span></td>
        <td colspan="3"><textarea name="heading1" cols="45" rows="5" wrap="VIRTUAL" id="heading1"></textarea></td>
      </tr>
      <tr>
        <td width="72"><span id=app_small_text>Information :</span></td>
        <td colspan="3"><textarea name="information" cols="45" rows="5" wrap="VIRTUAL" id="information"></textarea></td>
      </tr>  
      <tr>
        <td width="72">&nbsp;</td>
        <td width="213">&nbsp;</td>
        <td width="72">&nbsp;</td>
        <td width="213">&nbsp;</td>
      </tr>
      <tr>
        <td width="72">Small picture :</td>
        <td width="213"><input name="uploadedfile" type="file"></td>
        <td width="72">Large picture :</td>
        <td width="213"><input name="uploadedfile1" type="file"></td>
      </tr>
        <tr>
        <td width="72">&nbsp;</td>
        <td width="213">&nbsp;</td>
        <td width="72">&nbsp;</td>
        <td width="213">&nbsp;</td>
      </tr>  
      <tr>
        <td width="72">&nbsp;</td>
        <td width="213"><div align="center"><input type="reset" name="Reset" value="RESET FORM"></div></td>
        <td width="72">&nbsp;</td>
        <td width="213"><div align="center"><input type="submit" name="submitinfo" value="SUBMIT"></div></td>
      </tr>
    </table>
    </form>

    <?PHP

    # ----------------------------------------------------------------------------------
    # database connection 
    # ----------------------------------------------------------------------------------

    $db 'database';
    $dbuser 'username';
    $dbpass 'password';
    $dbhost 'localhost';

    mysql_connect($dbhost,$dbuser,$dbpass) or die("Sorry, I could not connect to the database server!");
    mysql_select_db("$db") or die("Sorry, I could not connect to the database!");


        if (isset(
    $_POST['submitinfo'])) {
        
    $id $_POST['id'];
        
    $case =  $_POST['case_number'];
        
    $first_name $_POST['first_name'];
        
    $last_name $_POST['last_name'];
        
    $alias =  $_POST['nickname'];
        
    $address =  $_POST['address'];
        
    $city =  $_POST['city'];
        
    $state =  $_POST['state'];
        
    $zip =  $_POST['zip'];
        
    $height =  $_POST['height'];
        
    $weight =  $_POST['weight'];
        
    $age =  $_POST['age'];
        
    $sex =  $_POST['sex'];
        
    $eyes =  $_POST['eye'];
        
    $hair =  $_POST['hair'];
        
    $complexion =  $_POST['complexion'];
        
    $race =  $_POST['race'];
        
    $birthdate =  $_POST['birthdate'];
        
    $heading1 =  $_POST['heading1'];
        
    $information =  $_POST['information'];

          
    $sql "UPDATE missing_person SET
                  date=CURDATE(),
                  case_number='
    $case',
                  first_name='
    $first_name',
                  last_name='
    $last_name',
                  nickname='
    $nickname',
                  address='
    $address',
                  city='
    $city',
                  state='
    $state',
                  zip='
    $zip',
                  height='
    $height',
                  weight='
    $weight',
                  age='
    $age',
                  sex='
    $sex',
                  eyes='
    $eyes',
                  hair='
    $hair',
                  complexion='
    $complexion',
                  race='
    $race',
                  birthdate='
    $birthdate',
                  heading1='
    $heading1',
                  information='
    $information'
                  WHERE ID='
    $id'";
          if (@
    mysql_query($sql)) {
            echo(
    '<p>Your information has been added.</p>'); 
          } else {
            echo(
    '<p>Error adding submitted information: ' .
                 
    mysql_error() . '</p>');
          }
        }
        
    # ----------------------------------------------------------------------------------
    # upload and copy small picture
    # ----------------------------------------------------------------------------------

    if ($uploadedfile_type == "image/pjpeg" 
        
    or $uploadedfile_type == "image/gif" ) { 
        
    // handle the file. 
    } else { 

        die(
    "<p> please submit a JPEG or GIF image file.</p>" ); 

        } 
         
    // Pick a file extension 

    if ( "image/pjpeg" == $uploadedfile_type 
        
    $extension ".jpg"
        else 
    $extension ".gif"


    // the complete path / filename 
    $filename "/public_html/images/missing/" "0" $id "-small" $extension

    // Copy the file (if it is deemed safe) 
    if (is_uploaded_file($uploadedfile) &&
        
    move_uploaded_file($uploadedfile$filename)) { 
        echo(
    "<P>File stored successfully as $filename.</p>" ); 
        } else { 
        echo(
    "<p>Could not save file as $filename!</p>" ); 
        }
        
    # ----------------------------------------------------------------------------------
    # upload and copy large picture
    # ----------------------------------------------------------------------------------

    if ($uploadedfile_type == "image/pjpeg" 
        
    or $uploadedfile_type == "image/gif" ) { 
        
    // handle the file. 
    } else { 

        die(
    "<p> please submit a JPEG or GIF image file.</p>" ); 

        } 
         
    // Pick a file extension 

    if ( "image/pjpeg" == $uploadedfile_type 
        
    $extension ".jpg"
        else 
    $extension ".gif"


    // the complete path / filename 
    $filename "/public_html/images/missing/" "0" $id "-large" $extension

    // Copy the file (if it is deemed safe) 
    if (is_uploaded_file($uploadedfile1) &&
        
    move_uploaded_file($uploadedfile1$filename)) { 
        echo(
    "<P>File stored successfully as $filename.</p>" ); 
        } else { 
        echo(
    "<p>Could not save file as $filename!</p>" ); 
        }    
        
    ?>
    Any suggestions and help would be great!

    Thanks

  2. #2
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's not a whole lot you can do with the HTML part, unless you are willing to write a script to analyze the database and build forms out of it... or, if you are going to keep the same forms every time, you could rename each field "field[]". Then, when your script is passed the $_POST values, you can just loop through each and build up your SQL query...

    As for the PHP, however, I would condense your extension checks :
    PHP Code:

    $filename 
    "/public_html/images/missing/" "0" $id "-small";
    switch (
    $uploadedfile_type) {
       case 
    "image/pjpeg":
          
    $filename .= ".jpg"
           
    break;

       case 
    "image/gif":
           
    $filename .= ".gif"
           break;

       default: 
          die(
    "<p> please submit a JPEG or GIF image file.</p>" ); 
          break;
    }

    //... 
    Do the same for both scripts, and you can shorten / improve your code.
    My name is Steve, and I'm a super-villian.

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Define your table cell widths on the first row only.
    Build your option box with a for-loop.
    Use an array and foreach loops to build your variables from the $_POST[] array, AND build your names for your inputs in your forms.
    In CSS an ID attribute is intended for only ONE instance of the ID on a page. CSS compliant browsers will use the first instance of an ID and ignore the rest that follow. You may want to change your ID to a class.

    Those are just a few off the top of my head.
    John


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
  •