gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:00:23 24 4
gpt4 key购买 nike

嘿伙计们,我遇到了一些似乎相当容易的事情。让我告诉您我正在尝试做什么,然后我将粘贴我的注释代码。

我正在尝试:

  1. 连接到 MySQL
  2. 插入一个递增的值(id)
  3. 临时存储该值 [我使用了 mysql_insert_id()]
  4. 使用 class.upload.php 上传图片并调整其大小,并使用最后插入的 ID 为其命名
  5. 显示上传的文件,并允许用户将其裁剪成缩略图

这就是一切乱七八糟的地方。

  1. 存储缩略图并使用最后插入的 ID 为其命名

我认为实际发生的是:

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

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

  • include_once 方法插入行

  • 将流程分成 2 个页面,第一页插入一行的地方,然后使用隐藏字段发布它

  • 将 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>

拜托,拜托,如果您能以任何方式提供帮助,或者至少建议一个替代解决方案,我将不胜感激。

非常感谢您。

最佳答案

乍一看,我建议插入新行的代码行应该放在上传代码之前。即在以下之后:

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'];

关于php - 为什么 MySQL 插入 3 个值而不是 1 个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1611643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com