gpt4 book ai didi

php - 获取sql中自增列的最大值

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:21 25 4
gpt4 key购买 nike

所以我有这段代码可以上传图片并调整它的大小,然后同时保存

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




}






}

所以代码“有效”...我遇到了这个问题,我需要使用 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)“选择”结果事件id

3)“更新”行

还有:

您正在使用旧的、已弃用的 mysql API。请熟悉其中一个/两个新的 mysqli/PDO API。请考虑使用 prepared statements .它们效率更高……并且有助于降低 SQL 注入(inject)攻击的风险。

关于php - 获取sql中自增列的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14733232/

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