[英]Php Mysql - Get value auto increment before and insert it to another column
[英]get max value of an Auto Increment column in sql
所以我有这段代码可以上传图像并调整其大小,并保存两者
if(isset($_POST['submit']))
{
$sql="SELECT MAX(event_id) AS event_id FROM evenimente";
//$result=mysql_query($sql);
$prefix=mysql_query($sql);
$file=$_FILES['image']['tmp_name'];
$file2=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
//
$sizes = array();
$sizes['100'] = 100;
list(,,$type) = getimagesize($_FILES['image']['tmp_name']);
$type = image_type_to_extension($type);
move_uploaded_file($_FILES['image']['tmp_name'], 'images/'.$prefix.$type);
$t = 'imagecreatefrom'.$type;
$t = str_replace('.','',$t);
$img = $t('images/'.$prefix.$type);
foreach($sizes as $k=>$v)
{
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = $v;
$new_height = floor( $height * ( $v / $width ) );
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagealphablending( $tmp_img, false );
imagesavealpha( $tmp_img, true );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$c = 'image'.$type;
$c = str_replace('.','',$c);
$c( $tmp_img, 'images/th/'.$prefix.$type );
}
$location="images/" . $prefix.$type;
$sql="INSERT INTO evenimente (event_data, event_datasf, event_titlu, event_detalii, event_poza) VALUES ('". $_POST['data'] ."', '". $_POST['datasf'] ."', '". $_POST['titlu'] ."', '". $_POST['detalii'] ."', '$location') ";
mysql_query($sql);
//$prefix =mysql_insert_id();
include 'include.html';
echo 'OK!!!<br /> Redirecting';
echo "<meta http-equiv='refresh' content='1;adauga_eveniment.php'>";
}
}
所以代码“工作” ...我有这个问题,我需要使用the event_id
保存图像。 但是我不能让$prefix
记住我数据库中的最大ID,我也不知道为什么...希望您理解。 我不能使用mysql_insert_id();
因为ID是在aql提交之后生成的,所以我需要在此之前添加前缀...而MAX
无法正常工作...我使用event_id
作为主键...
干杯
您的语法是正确的: SELECT MAX(event_id) AS event_id FROM evenimente
问题是您想知道event_id 在表中存在之前 。
建议:
1)“插入”新行
2)“选择”结果event_id
3)“更新”行
也:
您正在使用旧的,不推荐使用的mysql API。 请熟悉新的mysqli / PDO API中的一个或两个。 并且请考虑使用准备好的语句 。 它们更加高效...并且有助于减轻SQL注入攻击的风险。
如果您必须这样做,请尝试
$sql="SELECT event_id AS event_id FROM evenimente ORDER BY event_id DESC LIMIT 1";
但是请注意,这只会为您提供上一个事件的event_id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.