save records in phpmyadmin using form

0

I am trying to enter data in the phpmyadmin database from a php form, but I can not get it to register correctly. I can not figure out what the error is

The code I have is the following:

<?

include "includes/conexionBD.php";
if (!isset($accion)){
        echo"
        <html>
        <head><title>Guardar datos en la base</title></head>
        <body>
<h3>Guardar datos en la base</h3>
<form name="form1" method="post"

  <p>Nombre:<br>
    <input type="text" name="nombre">
  </p>
  <p>Apellido:<br>
    <input type="text" name="apellido1">
  </p>
  <p>Dirección:<br>
    <input type="text" name="Direccion">
  </p>
  <p>
    <input type="submit" name="Submit" value="Guardar Datos">
  </p>
</form>
</body>
</html>";
}elseif($accion=="guardar"){
  include"includes/conexionBD.php";
  $result=mysql_query("INSERT INTO personas  (nombre, apellido1,Direccion)
    VALUES ($nombre,$apellido1,$Direccion) ",$conexion);
  echo" <html>


?>
  

conexionBD.php

<?php
        define("SERVIDOR", "localhost");
        define("USUARIO", "root");
        define("CONTRASENA", "");
        define("BASEDATOS", "cochespersonas");

        $enlaceBD = mysqli_connect( SERVIDOR,USUARIO, CONTRASENA, BASEDATOS);

        $enlaceBD->set_charset("utf8");

        if( !$enlaceBD )
        {
            echo "La conexión no se ha podido establecer";
            exit();
        }
?>
    
asked by Juan 30.10.2018 в 16:54
source

1 answer

0

Hello for what I can notice is that you have a codebreak and everything scrambled.

What I did is basically what you want to do, but in a simpler and more understandable way.

I hope I can help you.

  

conexionBD.php

<?php
//parametros de la base de datos
$usuario='root';
$password='root';
$servidor='localhost';
$bd_name = 'nombre_bd';

// Realiza conexion
$conexion = mysqli_connect($servidor, $usuario, $password) or die('No se pudo conectar: ' . mysqli_error());
mysqli_select_db($conexion, $bd_name) or die('No se pudo seleccionar la base de datos'. mysqli_error());
?>
  

formulario.php

<html>
<head>
    <title>Guardar datos en la base</title>
</head>
    <body>
        <h3>Guardar datos en la base</h3>
        <form name="form1" method="post" action="registra.php">
          <p>Nombre:<br>
            <input type="text" name="nombre">
          </p>
          <p>Apellido:<br>
            <input type="text" name="apellido">
          </p>
          <p>Dirección:<br>
            <input type="text" name="direccion">
          </p>
          <p>
            <input type="submit" name="Submit" value="Guardar Datos">
          </p>
        </form>
    </body>
</html>
  

registra.php

<html>   
<head>   
<title>Guardar datos en la base</title>   
</head>   

<body>   
<?php   

// Recibimos por POST los datos del formulario   

$nombre = $_POST["nombre"];   
$apellido = $_POST["apellido"];   
$direccion = $_POST["direccion"];   

// Abrimos la conexion a la base de datos   
include ('includes/conexionBD.php'); 

$query = "INSERT INTO personas (nombre, apellido, direccion) VALUES ('$nombre','$apellido','$direccion')";    

if (mysqli_query($query)){
echo "<p>Registro agregado.</p>";
} else {
echo "<p>No se agregó.</p>";
} 

?>   
</body>   

</html>   
    
answered by 30.10.2018 в 18:13