insert data from a php form to a database

0

I am making a form through which you insert some data and these must be inserted in the database, but when I give it to send I get it:

  

undefined index: id (for all id, description, ...)

What am I doing wrong?

data connection is where I have

<?php
$dsn="mysql:dbname=ejemplo9;host=localhost";
$usuario=...
$contrasena=...
?>

The php code is this:

<html>
<body>
    <?php

include("datosconexion.php");

     $id=$_POST['id'];
    $descripcion=$_POST['descripcion'];
    $pvp=$_POST['pvp'];
    $stock=$_POST['stock'];


    $consulta="INSERT INTO articulo
                (id,descripcion,pvp,stock) VALUES('$id','$descripcion,'$pvp','$stock')";

    if(mysqli_query($link,$consulta)){
echo " Datos insertados";
    }else{
        echo "Datos NO insertados";

    }
?>

      </body>
</html>

Form:

<html>
  <body>
    <form method="post" action="conexionagregar5.php">
      <p>Ingresa los datos</p>
      <p>Escribe el identificador del articulo</p>
      <input type="text" name "id_articulo">
      <p>Escribe la descripcion del articulo</p>
      <input type="text" name "descripcion">
      <p>Escribe el precio del articulo</p>
      <input type="text" name "pvp">
      <p>Escribe si hay stock del articulo</p>
      <input type="text" name "stock">
      <input type="submit" value="Guardar datos" name="Enviar">
    </form>
  </body>
</html>
    
asked by Maria 23.12.2018 в 17:00
source

1 answer

0

You are missing the = sign in the name attribute of the form.

<html>
  <body>
    <form method="post" action="conexionagregar5.php">
      <p>Ingresa los datos</p>
      <p>Escribe la descripcion del articulo</p>
      <input type="text" name="descripcion">
      <p>Escribe el precio del articulo</p>
      <input type="text" name="pvp">
      <p>Escribe si hay stock del articulo</p>
      <input type="text" name="stock">
      <input type="submit" value="Guardar datos" name="Enviar">
    </form>
  </body>
</html>

Also, as you have been told, if your ID is AutoIncremental, you should not assign it any value. Therefore, your query would be:

$consulta="INSERT INTO articulo (descripcion,pvp,stock) VALUES('$descripcion','$pvp','$stock')";

Apart from this, there are other things that you should look at. As for example, the PVP should be decimal/numeric and stock a boolean (as I see in the form in the stock field inform whether there is or not).

    
answered by 24.12.2018 в 13:34