为什么MySQL插入3个值而不是1?

时间:2009-10-23 06:14:53

标签: php mysql

嘿伙计们,我很难用似乎相当容易。让我告诉你我正在尝试做什么,然后我将粘贴我的评论代码。

我想:

  1. 连接MySQL
  2. 插入递增值(id)
  3. 暂时存储该值[我使用mysql_insert_id()]
  4. 使用class.upload.php上传并调整图片大小,并使用上次插入的ID为其命名
  5. 显示上传的文件,并允许用户将其嵌入缩略图
  6. 这是所有地狱都崩溃的地方。

    1. 存储缩略图,并使用上次插入的ID
    2. 为其命名

      我认为实际发生的是:

      INSERT查询会针对每个步骤(上传,jcrop和缩略图显示)重复执行,因此它会插入3个值。所以会发生的事情是它[最初]上传的图像名称为3,但查找图像4以创建缩略图,然后显示错误的页面(应该显示缩略图)将另一行插入MySQL。完成所有这些后,下次我要上传图片时,它会给出初始上传ID为6。

      我一直在疯狂,试图使用不同的插入行的方法,但似乎文件名没有使用任何其他方法传递到每个步骤。我尝试过使用:

      • include_once方法插入行

      • 将流程分为2页, 第一页插入一行, 然后使用隐藏字段

      • 进行POST
      • 将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 support@michalkopanski.com</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>
      

      拜托,如果您能以任何方式提供帮助,或者至少提出替代解决方案,我将非常感谢。

      提前非常感谢你。

2 个答案:

答案 0 :(得分: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 support@michalkopanski.com</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'];

答案 1 :(得分:0)

从我收集的内容中,对于三个操作中的每个操作都会调用整个脚本,然后通过if语句(包括$ _POST变量)决定要做什么。这样,对于每个调用,您执行插入操作,这就是为什么有3个值而不是一个值。

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