0

can't make mysqli_insert_id work.. The id is AUTO_INCREMENT but still doesn't work.

$query = "INSERT INTO categoriasdivisiones (titulo, orden, descripcion, id_familiadivisiones) VALUES ('$titulo','$orden', '$descripcion', '$id_familia');";

    mysqli_query($base, $query);

    $ultimo_id = mysqli_insert_id($base);

Here i call the database:

include('../../../php/config.php');

the route is ok.

I know the problem in the code is the insert_id function because i have an image where i use this path:

move_uploaded_file($foto['tmp_name'], '../../../imagenes/categoriadivisiones/catdivisiones_'.$ultimo_id.'.'.$extensionfoto.'');

And the image is being saved as = catdivisiones_0.png

Hope you can help me.

CREATE TABLE `categoriasdivisiones` (
  `id` int(11) NOT NULL,
  `titulo` text NOT NULL,
  `descripcion` text NOT NULL,
  `imagen` text NOT NULL,
  `orden` text NOT NULL,
  `id_familiadivisiones` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `categoriasdivisiones`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
  • 2
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 17 '17 at 15:25
  • The `mysqli_insert_id()` function returns the id (generated with `AUTO_INCREMENT`) used in the last query. Are you using auto increment? – Jay Blanchard Apr 17 '17 at 15:27
  • Yes i am, "The id is A_I but still doesn't work." – Julian Osole Apr 17 '17 at 15:32
  • I thought that was your format - you need to spell that out. – Jay Blanchard Apr 17 '17 at 15:36
  • @JulianOsole, the statement 'The id is A_I but still doesn't work' doesn't make any sense to me. Autoincrement fields are ONLY integers. Por favor, can you add your table creation script for categoriasdivisiones into the question. – Anne Gunn Apr 17 '17 at 16:05
  • What does `mysqli_error($base)` return after the insert? If there is some error preventing the insert from happening, no insert_id can be returned. – 11684 Apr 17 '17 at 18:52

2 Answers2

0

make sure $base variable has connection.after VALUES ('XX','XX','XX'XX').REMOVE ';'

The resultant query

$query = "INSERT INTO categoriasdivisiones (titulo, orden, descripcion, id_familiadivisiones) VALUES ('$titulo','$orden', '$descripcion', '$id_familia')";

mysqli_query($base, $query);

$ultimo_id = mysqli_insert_id($base);
Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31
MD.Mahedi hasan
  • 102
  • 1
  • 5
0

You can use the this at the Create id int auto_increment primary key or you can select the latest record by the id select id from your_table order by id DESC and store on a variable and sum +1 like this <?php $new_id = $id_from_database + 1 ?>

Hope i have helped