2009-10-23 58 views
1

嘿家伙,我有什么似乎是相当容易的麻烦。让我告诉你我想要做什么,然后我会粘贴我的评论代码。为什么MySQL插入3个值而不是1?

我想:

  1. 连接到MySQL
  2. 插入一个递增值(ID)
  3. 临时存储值我用mysql_insert_id()]
  4. 上传和使用调整图像大小class.upload.php并使用最后插入的名称给它起一个名字
  5. 显示上传的文件,并允许用户将其缩小到缩略图

这是所有地狱崩溃的地方。

  • Store中的缩略图也利用最后插入的ID
  • 我认为实际情况给它的名字是:

    插入查询重复为每一步(上传,jcrop和缩略图显示),因此它插入3个值。那么会发生什么呢是它给出了[最初]上传的图像名称3,但是查找图像4以创建缩略图,然后显示错误的页面(应该显示缩略图)将另一行插入到MySQL中。发生所有这一切后,下一次我想上传一个图像,它给出了最初的上传id为6.

    我一直在疯狂,试图使用插入行的不同方法,但它似乎文件名doesn使用任何其他方法都不会传递给每个步骤。我已经尝试使用:

    • include_once方法来插入的行

    • 分裂过程分成2页, 其中t的第一页插入一行, 然后投递它使用隐藏字段

    • 移动mysql插入到其中一个 其他步骤(不通过最后 id到任何其他步骤)

    这里是我的代码:

    <?php 
    
        //connect to MySQL 
        $dbname = "****"; 
        $username = "****"; 
        $password = "****"; 
        $hostname = "localhost"; 
    
        //connection to the database 
        $dbhandle = mysql_connect($hostname, $username, $password) 
        or die("<br/><h1>Unable to connect to MySQL, please contact support at [email protected]</h1>"); 
    
        //select a database to work with 
        $selected = mysql_select_db($dbname, $dbhandle) 
        or die("Could not select database."); 
    
        //insert an auto_incrementing value into the 'pictures' table 
        if (!$result = mysql_query("INSERT INTO `pictures` VALUES ('')")) 
        echo 'mysql error: '.mysql_error(); 
    
        $filename = mysql_insert_id(); 
    
    ?> 
    <?php 
    
    //submit coordinates of jcrop preview 
    if ($_POST['submit_coords']) 
    { 
    
    $targ_w = 180; // desired width of thumbnail 
    $targ_h = 60; // desired height of thumbnail 
    $jpeg_quality = 90; // desired jpeg quality 
    $src = 'f/'.$filename.'.jpg'; // get path to the resized image 
    
    // Fetch the uploaded jpeg file 
    $img_r = imageCreateFromJpeg($src); 
    
    //Use these proportions 
    $dst_r = ImageCreateTrueColor($targ_w, $targ_h); 
    
    // Get coordinates from jcop, and create thumbnail 
    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], $targ_w,$targ_h,$_POST['w'],$_POST['h']); 
    
    // save the thumbnail and echo the result 
    $output_file = 't/'.$filename.'.jpg'; 
    imagejpeg($dst_r, $output_file, $jpeg_quality); 
    $results = <<<EOT 
    <img src="t/$filename.jpg"/> 
    EOT; 
    echo $results; 
    die(); 
    } 
    
    // upload & resize mig image 
    if ($_POST['upload_image'] && $_FILES['image_upload']) 
    { 
    require_once '../lib/jcrop/class.upload.php'; 
    
    $ih = new Upload($_FILES['image_upload']); 
    if ($ih->uploaded) { 
        //save full size 
        $ih->file_new_name_body = $filename; 
        $ih->file_new_name_ext = 'jpg'; 
        $ih->image_resize = true; 
        $ih->image_x = 860; 
        $ih->image_ratio_y = true; 
        $ih->process('f/'); 
        if ($ih->processed) { 
    
        $x = $ih->image_dst_x; 
        $y = $ih->image_dst_y; 
        echo 'image resized'; 
    
        $ih->clean(); 
        } else { 
        echo 'error : ' . $ih->error; 
        } 
    
    } 
    
    //if succesful show the thumbnail preview 
    $output = <<<EOT 
    <img src="f/$filename.jpg" id="jcrop" /> 
    <br /> 
    <div style="overflow: hidden; width: 180px; height: 60px;"> 
    <img src="f/$filename.jpg" id="preview" /> 
    </div> 
    EOT; 
    
    // Show uploaded image, and allow jcrop 
    $head = <<<EOT 
    <link rel="stylesheet" href="../lib/jcrop/jquery.Jcrop.css" type="text/css" /> 
    <script type="text/javascript" src="../lib/jcrop/jquery.min.js"></script> 
    <script type="text/javascript" src="../lib/jcrop/jquery.Jcrop.min.js"></script> 
    <script> 
    $(function(){ 
    function showPreview(coords) 
    { 
        var rx = 180/coords.w; 
        var ry = 60/coords.h; 
    
        $('#preview').css({ 
        width: Math.round(rx * $x) + 'px', 
        height: Math.round(ry * $y) + 'px', 
        marginLeft: '-' + Math.round(rx * coords.x) + 'px', 
        marginTop: '-' + Math.round(ry * coords.y) + 'px' 
        }); 
    }; 
    
    function insertCoords(c) 
    { 
        $('#x').val(c.x); 
        $('#y').val(c.y); 
        $('#x2').val(c.x2); 
        $('#y2').val(c.y2); 
        $('#w').val(c.w); 
        $('#h').val(c.h); 
    }; 
    
    
    $('#jcrop').Jcrop({ 
        onChange: showPreview, 
        onSelect: insertCoords, 
        aspectRatio: 3 
    }); 
    }); 
    </script> 
    EOT; 
    } 
    ?> 
    
    <html> 
    <head> 
    <?php if ($head) echo $head; ?> 
    </head> 
    <body> 
    <?php if (!$head) : ?> 
    <form action="" method="POST" enctype="multipart/form-data"> 
    <input type="file" name="image_upload" /> 
    <input type="submit" name="upload_image" value="click me" /> 
    </form> 
    <?php else : ?> 
    <form action="" method="POST"> 
    <input type="hidden" name="x" id="x" /> 
    <input type="hidden" name="y" id="y" /> 
    <input type="hidden" name="x2" id="x2" /> 
    <input type="hidden" name="y2" id="y2" /> 
    <input type="hidden" name="w" id="w" /> 
    <input type="hidden" name="h" id="h" /> 
    <input type="hidden" name="filename" id="filename" value="<?php echo $filename ?>" /> 
    <input type="submit" name="submit_coords" value="gogo" /> 
    </form> 
    <?php endif; ?> 
    <?php if ($output) echo $output; ?> 
    </body> 
    </html> 
    

    请,请,如果你能帮助以任何方式,或者至少提出一个替代的解决方案,我将不胜感激。

    非常感谢你提前。

    回答

    4

    乍一看,我会建议插入新行的代码的顶部行应该在上传代码之前进行。即在以下几点:

    if ($_POST['upload_image'] && $_FILES['image_upload']) 
    { 
    require_once '../lib/jcrop/class.upload.php'; 
        //connect to MySQL 
        $dbname = "****"; 
        $username = "****"; 
        $password = "****"; 
        $hostname = "localhost"; 
    
        //connection to the database 
        $dbhandle = mysql_connect($hostname, $username, $password) 
        or die("<br/><h1>Unable to connect to MySQL, please contact support at [email protected]</h1>"); 
    
        //select a database to work with 
        $selected = mysql_select_db($dbname, $dbhandle) 
        or die("Could not select database."); 
    
        //insert an auto_incrementing value into the 'pictures' table 
        if (!$result = mysql_query("INSERT INTO `pictures` VALUES ('')")) 
        echo 'mysql error: '.mysql_error(); 
    
        $filename = mysql_insert_id(); 
    

    然后就下

    if ($_POST['submit_coords']) 
    { 
    

    应该

    $filename = $_POST['filename']; 
    
    +0

    它工作!哦,单词无法表达刚刚从我肩上掉下来的重量。这个事情的最后期限是在几个小时之内! 如果您希望看到它的工作方式演示,请告诉我,我很乐意向您介绍。另外如果你想要一个文件的zip文件,我也会发送。这实际上很酷,很原始! 再一次,非常感谢,你是一种生活品味! – 2009-10-23 06:37:46

    0

    从我所收集你的脚本被称为整体为三个运营和您的通过包含$ _POST变量的if语句决定要做什么。这样,对于每次调用,您都会执行插入操作,这就是为什么有3个值而不是1个值的原因。

    您应该将您的MySQL INSERT查询移动到第一个if分支中,然后返回给浏览器的某种引用(最可能是id),然后将其传递回服务器以执行以下步骤。不要忘记每次验证此参考。

    相关问题