php - 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


        $sql="SELECT MAX(event_id) AS event_id FROM evenimente";


        $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')  ";
    //$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 withthe 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 usemysql_insert_id(); because the id is generated after the aql submission, and i need the prefix before that... andMAX doesn't work... I'm using theevent_id as primary key...





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.


1) "insert" the new row

2) "select"the resulting event_id

3) "update" the row


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.

People are also looking for solutions to the problem: php - SQL SUM values from column and dividide by number of rows


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.