SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Why my $id var disapear???

    Hello sitepoint community!

    This is a script to manage upload of photos
    It's upload/remove/test if image already exits and display photo

    But my var $id seems disapear when I remove de photo

    to help you to understand, the name of this file is gere_foto1.php

    Anyone can help???

    Thanks in advance!


    PHP Code:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <script language="JavaScript">
    function deletecapa(id)
    {
        if (confirm("Really want FOTO 1?"))
        {
            window.location.href = 'gere_foto1.php?delcapa=' + id;
        }
    }
    </script>
    </head>

    <body topmargin="10">
    <?php
    include '../../library/config.php';
    include 
    '../../library/opendb.php';

    $id =  $_REQUEST['id'];

        
    // If photo submited, upload and save the url on database, at the final show the photo

        
    if(isset($_POST['img1']) && $_FILES['foto1']['size'] > 0) {

                     
    $uploaddir '/home/xpto/public_html/fotos/autom/';
            
    $uploadfile $uploaddir basename($_FILES['foto1']['name']);
            
    $imagem str_replace ("/home/xpto/public_html/fotos/autom/"""$uploadfile);
            
            
    $testa_img str_replace ("/home/xpto/public_html/""http://www.xpto.com/"$uploadfile);

            
            if (
    file_exists($uploadfile)) {
                   echo 
    "<p align=\"center\" class=\"normal\">File already exits on dbase!</p><p align=\"center\" class=\"normal\"><a href=\"$testa_img\" target=\"_blank\">VERIFY</a></p><p align=\"center\" class=\"normal\">Rename or choose other file!</p><p align=\"center\" class=\"normal\"><a href=\"gere_foto1.php?id=".$id."\";\">Back</a></p>";
                  exit;

            } else {


                echo 
    '<pre>';
                if (
    move_uploaded_file($_FILES['foto1']['tmp_name'], $uploadfile)) {
                    echo 
    "<p align=\"center\" class=\"normal\">Upload sucessfull!</p>\n";
                    
    $sql "UPDATE autom SET foto1 = '$uploadfile' WHERE id = '$id'"
                    
    mysql_query($sql) or die('Error : ' mysql_error());
                } else {
                    echo 
    "eRROR!<br>File not upload correctly!<br><a href=\"cms-admin.php\">Try again!</a>\n";
                }
                echo 
    "<p align=\"center\"><img src=\"../../library/thumb_p.php?".$imagem."\"></p>\n";
                echo 
    "<p align=\"center\" class=\"normal\"><a href=\"javascript:deletecapa('$id');\">delete photo</a></p>";
                echo 
    "</pre>";
                echo 
    "</body>";
                echo 
    "</html>";
                exit;
            }
        
        }

        
    // if asked to remove photo
        
        
    if(isset($_GET['delcapa'])) {

            
    $sql_fich "SELECT foto1 FROM autom WHERE id = '$delcapa'";
    #        echo $sql_fich;
            
    $result mysql_query($sql_fich) or die('Error : ' mysql_error());
            
    $row mysql_fetch_array($result); 
            
    $file $row['foto1'];
            
    $sql_delcapa "UPDATE autom SET foto1 = '' WHERE id = '$delcapa'";
            
    mysql_query($sql_delcapa) or die('Error : ' mysql_error());
            
    unlink($file);
            echo 
    "<p align=\"center\" class=\"normal\">Photo removed!<br><a href=\"gere_foto1.php?id=".$delcapa."\">continue</a></p>";
            exit;
        }



        if(isset(
    $_GET['id'])) {

            
    $sql_reg "SELECT foto1 FROM autom WHERE id = '$id'";
            
    $reg mysql_query($sql_reg) or die('Error : ' mysql_error());
            
    $row mysql_fetch_array($reg); 
            
    $capa_reg $row['foto1'];

        
            
    //se existe capa
            
    if (!empty($capa_reg)) {
            
                
    $imagem str_replace ("/home/xpto/public_html/fotos/autom/"""$capa_reg);
                
    $endereco str_replace ("/home/xpto/public_html/""http://www.xpto.com/"$capa_reg);
                echo 
    "<p align=\"center\" class=\"normal\"><img src=\"../../library/thumb_p.php?".$imagem."\" hspace=\"5\" vspace=\"6\"><br />\n";
                echo 
    "<a href=\"$endereco\" target=\"_blank\">real size</a>&nbsp;<a href=\"javascript:deletecapa('$id');\delete</p>\n";
                
            } else {
                echo 
    "<p align=\"left\">";
                echo 
    "<form enctype=\"multipart/form-data\" method=\"post\" action=\"$PHP_SELF\"?id=\".$id;\">";
                  echo 
    "<input type=\"hidden\" name=\"id\" value=\"$id\">";
                echo 
    "<input type=\"file\" name=\"foto1\" size=\"20\" id=\"foto1\" class=\"box\">";
                  echo 
    "<br><br><input type=\"submit\" name=\"img1\" value=\"put on-line\" id=\"img1\" class=\"box\">";
                echo 
    "</form>";
                echo 
    "</p>";
            }
                
        
        } else {
            
            echo 
    "Erro: Record or file not identified!<br><br><a href=\"javascript:history.go(-1)\";\">Back</a>";
            exit;

        }
    ?>
    Last edited by pgam; Jan 18, 2007 at 07:33.

  2. #2
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You never set $delcapa equal to $_GET['delcapa'] which is the ID.
    PHP | MySQL | (X)HTML | CSS

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I set it in javascript!

    window.location.href = 'gere_foto1.php?delcapa=' + id;
    .| PGAM |.

  4. #4
    SitePoint Enthusiast dyer85's Avatar
    Join Date
    Nov 2004
    Location
    L2 cache.
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your script seems to have several SQL injection holes. Aside from first verifying that the index in the $_REQUEST array exists, you need ensure that IDs are numeric (using the intval function, or the (int) cast). Strings should be validated by mysql_real_escape_string.
    "Structure padding is the use of extraneous materials to
    enhance the shape of a struct and make it more attractive
    to members of the opposite struct. (see also 'struct
    silicone.')" -- Eric Sosman

  5. #5
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pgam View Post
    I set it in javascript!

    window.location.href = 'gere_foto1.php?delcapa=' + id;
    You are not setting it in your PHP script.

    PHP Code:
    if(isset($_GET['delcapa'])) {

            
    $sql_fich "SELECT foto1 FROM autom WHERE id = '$delcapa'";
    #        echo $sql_fich;
            
    $result mysql_query($sql_fich) or die('Error : ' mysql_error());
            
    $row mysql_fetch_array($result);
            
    $file $row['foto1'];
            
    $sql_delcapa "UPDATE autom SET foto1 = '' WHERE id = '$delcapa'";
            
    mysql_query($sql_delcapa) or die('Error : ' mysql_error());
            
    unlink($file);
            echo 
    "<p align=\"center\" class=\"normal\">Photo removed!<br><a href=\"gere_foto1.php?id=".$delcapa."\">continue</a></p>";
            exit;
        } 
    Just need to add one line and all should work.

    PHP Code:
    if(isset($_GET['delcapa'])) {
            
    $delcapa $_GET['delcapa']; // This is the new line
            
    $sql_fich "SELECT foto1 FROM autom WHERE id = '$delcapa'";
    #        echo $sql_fich;
            
    $result mysql_query($sql_fich) or die('Error : ' mysql_error());
            
    $row mysql_fetch_array($result);
            
    $file $row['foto1'];
            
    $sql_delcapa "UPDATE autom SET foto1 = '' WHERE id = '$delcapa'";
            
    mysql_query($sql_delcapa) or die('Error : ' mysql_error());
            
    unlink($file);
            echo 
    "<p align=\"center\" class=\"normal\">Photo removed!<br><a href=\"gere_foto1.php?id=".$delcapa."\">continue</a></p>";
            exit;
        } 
    PHP | MySQL | (X)HTML | CSS

  6. #6
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ID's are always numeric and auto-increment
    .| PGAM |.

  7. #7
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot brent5392, you were right!
    Now it's seems to work!
    .| PGAM |.

  8. #8
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pgam View Post
    Thanks a lot brent5392, you were right!
    Now it's seems to work!
    Its always the simple things
    (yay 350th post!)
    PHP | MySQL | (X)HTML | CSS

  9. #9
    SitePoint Enthusiast dyer85's Avatar
    Join Date
    Nov 2004
    Location
    L2 cache.
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pgam View Post
    ID's are always numeric and auto-increment
    Yes, ideally, but since you're taking the ID from a source of user input, people will try and enter other things, which your code:
    PHP Code:
    $id =  $_REQUEST['id']; 
    does not validate. Although your database may be set up properly, this really has nothing to do with that. You might also want to consider limiting input to just $_POST.
    "Structure padding is the use of extraneous materials to
    enhance the shape of a struct and make it more attractive
    to members of the opposite struct. (see also 'struct
    silicone.')" -- Eric Sosman


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
  •