![](/img/trans.png)
[英]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.