简体   繁体   中英

get max value of an Auto Increment column in sql

so i have this code that uploads an image and resize it, and saves both

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




}






                                }

so the code "works"...i have this problem i need to save the images with the event_id . but i can't make $prefix memorize the max id from my database and i don't know why... hope you understand. I can't use mysql_insert_id(); because the id is generated after the aql submission, and i need the prefix before that... and MAX doesn't work... I'm using the event_id as primary key...

Cheers

Your syntax is correct: SELECT MAX(event_id) AS event_id FROM evenimente

The problem is that you want to know the event_id before it exists in the table.

SUGGESTION:

1) "insert" the new row

2) "select"the resulting event_id

3) "update" the row

ALSO:

You're using the old, deprecated mysql API. Please familiarize yourself with either/both of the new mysqli/PDO APIs. And please consider using prepared statements . They're more efficient ... and they help mitigate the risks of SQL injection attacks.

If you must do it that way, try

$sql="SELECT event_id AS event_id FROM evenimente ORDER BY event_id DESC LIMIT 1";

But note that this will only give you the event_id of the previous event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM