Neden MySQL 1 yerine 3 değerlerini eklemek nedir?

2 Cevap php

Hey guys, seems oldukça kolay olması için ne ile sorun yaşıyorum. Benim yapmak ne çalışıyorum size söyleyeyim, ve sonra benim yorumladı kodu yapıştırın.

Ben çalışıyorum:

  1. MySQL bağlanmak
  2. Artan bir değer takın (id)
  3. Geçici [i kullanılan mysql_insert_id()] bu değeri saklamak
  4. Upload ve class.upload.php kullanarak görüntüyü yeniden boyutlveırmak ve son eklenen kimliği kullanarak o ad verin
  5. Yüklenen dosyayı göster ve kullanıcı bir küçük resmin içine Jcrop izin

This is where all hell breaks loose.

  1. Ayrıca son eklenen kimliği kullanarak o ad vererek üzerlerine Mağaza

What I think actually happens is:

INSERT sorgusu her adımda (upload, Jcrop ve küçük ekran) için tekrarlar ve bu nedenle 3. değerler ekler. Peki ne olur o 3 [başlangıçta] yüklenen görüntü adını verir, ancak, gelen (bana küçük göstermek gerekiyordu) hata gösterir MySQL içine başka bir satır ekler sonra sayfasını küçük resim oluşturmak için görüntü 4 için görünüyor. Olur Sonuçta, ben bir görüntü yüklemek isteyen bir dahaki sefer, 6 ilk yükleme kimliği verir.

Ben satırı ekleme farklı yöntemler kullanmaya çalışıyor, deli gidiyor, ancak dosya başka bir yöntemi kullanarak adımların her birine geçti almaz görünüyor. Ben kullanarak denedi:

  • satır eklemek için include_once yöntem

  • splitting the process into 2 pages, where t the firstpage inserts a row, then POSTs it using hidden field

  • moving the mysql insert into one of the other steps (doesnt pass the last id into any other step)


Here is my code:

<?php

   //connect to MySQL
   $dbname = "****";
   $username = "****";
   $password = "****";
   $hostname = "localhost"; 

  //connection to the database
  $dbhvele = 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, $dbhvele)
    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, ve create thumbnail
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], $targ_w,$targ_h,$_POST['w'],$_POST['h']);

// save the thumbnail ve 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, ve 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>

Eğer herhangi bir şekilde yardımcı olabilir, ya da en azından alternatif bir çözüm önermek Lütfen, lütfen, ben çok takdir ediyorum.

Şimdiden çok teşekkür ederim.

2 Cevap

İlk bakışta ben yeni bir satır eklemek için kod üst çizgiler sadece yükleme koduna önce gitmek gerektiğini öneririm. Aşağıdaki sonra, yani:

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();

Ardından hemen altında

if ($_POST['submit_coords'])
{

olmalıdır

$filename = $_POST['filename'];

Via script üç işlemlerinin her biri için bir bütün olarak adlandırılır toplamak ve ne karar ne ise $ _POST değişkeni içeren tablolar. Bu şekilde, her biri için 3 değerleri yerine biri var, o yüzden bir ekleme yapmak diyoruz.

Idam ediyor şube daha sonra aşağıdaki adımlar için geri sunucuya geçilecek tarayıcısı referans çeşit (büyük olasılıkla id) dönerseniz ilk içine MySQL INSERT sorgusu hareket etmelidir. Bu referansı her zaman doğrulamak için unutmayın.