0

Okay so i have this form that is supposed to fill multiple tables but when i click on the send button, the page just refreshes, and when i check phpmyadmin the tables are empty.

i have a code that is supposed to tell me if the data got succesfully registered in the database, but i cant get it to work either, it doesn't show up at all, not even to tell me the registration was not succesful.

I've been struggling with this code for 3 days, im pretty new to coding but i tried every change i could an i still can't get it to work.

Here's the code:

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Registro empleado</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css">
</head>
<body>
<form>
    <h1>Ingresar Datos</h1>
    <h3>Cliente</h3>
    <input type="number" name="id_cliente" placeholder="id_local"><!--ingreso del id-->
        <input type="text" name="nombre" placeholder="nombre"><!--ingreso del nombre del cliente-->
         <input type="text" name="domicilio" placeholder="domicilio"><!--ingreso del domicilio-->
         <input type="text" name="telefono" placeholder="telefono"><!--ingreso del numero de telefono-->
         <input type="text" name="edad" placeholder="edad"><!--ingreso de la edad del cliente-->
    <h3>Ganancias</h3>
    <input type="number" name="id_ganancias" placeholder="id_ganancias"><!--ingreso del id-->
         <input type="number" name="diarios1" placeholder="diarios"><!--ingreso de ganancias diarias-->
         <input type="number" name="mensuales1" placeholder="mensuales"><!--ingreso de ganancias mensuales-->
         <input type="number" name="anuales1" placeholder="anuales"><!--ingreso de ganancias anuales-->
    <h3>Gastos</h3>
    <input type="number" name="id_gastos" placeholder="id_gastos"><!--ingreso del id-->
    <input type="number" name="diarios2" placeholder="diarios"><!--ingreso de gastos diarios-->
         <input type="number" name="mensuales2" placeholder="mensuales"><!--ingreso de gastos mensuales-->
         <input type="number" name="anuales2" placeholder="anuales"><!--ingreso de gastos anuales-->
        <input type="submit" name="enviar"><!--botton de enviar-->
    </form>
<form action="http://localhost/consulta_cliente.php"><!--link para phpadmin-->
        <input type="submit" value="consulta_cliente"><!--boton que te lleva phpadmin-->
        
        <form action="http://localhost/consulta_ganancias.php"><!--link para phpadmin-->
        <input type="submit" value="consulta_ganancias"><!--boton que te lleva phpadmin-->
        
        <form action="http://localhost/consulta_gastos.php"><!--link para phpadmin-->
        <input type="submit" value="consulta_gastos"><!--boton que te lleva phpadmin-->     
</form>
<?php 
        include("registrar_empleado.php");/*Llamado a el archivo php de registro*/
        ?>
</body>
</html>

PHP:

<?php 

//Llamada a código de conexión
include("conexion_empleado.php");

//validación de variables que recibirán cada campo de la tabla

 //cliente
if (isset($_POST['enviar'])) {
    if (strlen($_POST['id_cliente']) >=1 && strlen($_POST['nombre']) >= 1 && strlen($_POST['domicilio']) >= 1 && strlen($_POST['edad']) >= 1 && strlen($_POST['telefono']) >= 1) {
        $id_cliente = trim($_POST['id_cliente']);
        $nombre = trim($_POST['nombre']);
        $domicilio = trim($_POST['domicilio']);
        $telefono = trim($_POST['telefono']);
        $edad = trim($_POST['edad']);
        
//inserción de registros en la tabla datos
        $consulta1 = "INSERT INTO cliente(id_cliente, nombre, domicilio, telefono, edad) VALUES ('$id_cliente', '$nombre', '$domicilio', '$edad', '$telefono')";
        $resultado1 = mysqli_query($conecta,$consulta1);
         

       //ganancias
         if (strlen($_POST['id_ganancias']) >=1 && strlen($_POST['diarios1']) >= 1 && strlen($_POST['mensuales1']) >= 1 && strlen($_POST['anuales1']) >= 1) {
        $id_ganancias = trim($_POST['id_ganancias']);
        $diarios1 = trim($_POST['diarios1']);
        $mensuales1 = trim($_POST['mensuales1']);
        $anuales1 = trim($_POST['anuales1']);

         $consulta2 = "INSERT INTO ganancias(id_ganancias, diarios, mensuales, anuales) VALUES ('$id_ganancias', '$diarios1', '$mensuales1', '$anuales1')";
        $resultado2 = mysqli_query($conecta,$consulta2);
          
          //gastos
          if (strlen($_POST['id_gastos']) >=1 && strlen($_POST['diarios2']) >= 1 && strlen($_POST['mensuales2']) >= 1 && strlen($_POST['anuales2']) >= 1) {
        $id_gastos = trim($_POST['id_gastos']);
        $diarios2 = trim($_POST['diarios2']);
        $mensuales2 = trim($_POST['mensuales2']);
        $anuales2 = trim($_POST['anuales2']);

         $consulta3 = "INSERT INTO gastos(id_gastos, diarios, mensuales, anuales) VALUES ('$id_gastos', '$diarios2', '$mensuales2', '$anuales2')";
        $resultado3 = mysqli_query($conecta,$consulta3);
         
        if ($resultado1 && $resultado2 && $resultado3) {
            ?> 
            <h3 class="completo">Registro exitoso</h3>
           <?php
        } else {
            ?> 
            <h3 class="incompleto">Error de registro</h3>
           <?php
        }
        } else {
            ?> 
            <h3 class="incompleto">Por favor completa el registro</h3>
    <?php
            }
        } 
    }
}
?>

PHP code to connect to the database:

<?php

//código para asignar las variables para la conexión: localhost = nombre servido, usuario = root, nombre_BD = registro

$conecta = mysqli_connect("localhost","root","","locales"); 

?>

I have some code to show the last rows that were filled by the form, i can't test if it works yet since i can't get data to even get send to the database, anyways here it is just it case:

Query1:

<?php
include("conexion_cliente.php");//Llamado a el documento php
$consulta1_1 = "select * from pago";//se muestra la tabla pago
$resultado1_1 = mysqli_query($conecta,$consulta1_1);

$cuenta = mysqli_num_rows ($resultado);
$cuenta = $cuenta - 1; 
$resultado1 = mysqli_query($conecta, "SELECT * FROM cliente LIMIT $cuenta, 1"); //consulta de tabla empleando
//limit para obtener solo una fila

echo "<h1>ULTIMO CAMPO</h1>"; //titulo
echo "<pre>"; //atributo pre para poder realizar el salto de línea
while($row1 = mysqli_fetch_array($resultado1)) { //se obtienen los arrays de la consulta
    echo print_r($row1); //se imprimen los resultados de la consulta
    echo "\n"; //salto de linea
}
echo "</pre>";
?>

Query2:

<?php
include("conexion_cliente.php");//Llamado a el documento php
$consulta2_1 = "select * from pago";//se muestra la tabla pago
$resultado2_1 = mysqli_query($conecta,$consulta2_1);

$cuenta = mysqli_num_rows ($resultado);
$cuenta = $cuenta - 1; 
$resultado2 = mysqli_query($conecta, "SELECT * FROM ganancias LIMIT $cuenta, 1"); //consulta de tabla empleando
//limit para obtener solo una fila

echo "<h1>ULTIMO CAMPO</h1>"; //titulo
echo "<pre>"; //atributo pre para poder realizar el salto de línea
while($row2 = mysqli_fetch_array($resultado2)) { //se obtienen los arrays de la consulta
    echo print_r($row2); //se imprimen los resultados de la consulta
    echo "\n"; //salto de linea
}
echo "</pre>";
?>

Query3:

<?php
include("conexion_cliente.php");//Llamado a el documento php
$consulta3_1 = "select * from gastos";//se muestra la tabla gastos
$resultado3_1 = mysqli_query($conecta,$consulta3_1);

$cuenta = mysqli_num_rows ($resultado);
$cuenta = $cuenta - 1; 
$resultado3 = mysqli_query($conecta, "SELECT * FROM gastos LIMIT $cuenta, 1"); //consulta de tabla empleando
//limit para obtener solo una fila

echo "<h1>ULTIMO CAMPO</h1>"; //titulo
echo "<pre>"; //atributo pre para poder realizar el salto de línea
while($row3 = mysqli_fetch_array($resultado3)) { //se obtienen los arrays de la consulta
    echo print_r($row3); //se imprimen los resultados de la consulta
    echo "\n"; //salto de linea
}
echo "</pre>";
?>

Sorry it's written in spanish, im from mexico but i noticed that this "us" page was more active than the "es" one. Any help it's greatly appreciated and if you have any language related question i'll be glad to answer.

  • Troubleshoot by eliminating unknown variables. Is your receiving script getting the variables? use something like `die(print_r($_POST));` to verify. Are any of your queries working (i.e., selects) - then you know the connection code is irrelevant to the problem. You have a lot of information here that needs to be pared down, complicated by being in Spanish. Try to narrow down your question a bit, please. – Tim Morton Dec 11 '20 at 03:30
  • 1
    There are a few issues with the html code. The first form that contains all inputs does not have any action tag. Also the submit input in that form does not have value so probably you can not see that button.. The second form has nested form which is not allowed. And even if you submit that form which has a number of submit buttons and action tags, the inputs are all not part of that form, so none will be submitted. It will work if you create three different forms. – T.Shah Dec 11 '20 at 06:36
  • thank you for replying, i though it would make sense to add all the php code in one page since its supposed to run when i press the send button, which i did not think about adding a value to it before, i can see it normally in google, it just says "enviar". ill make the query too, i thought i could just check the access log but i guess it makes more sense to do it directly in the code. my deadline's close, i hope everything works out and again thank you so much for both of your replies!!. – Vargas Murillo Otmar Fidel Dec 11 '20 at 08:37
  • Side note: You should urgently learn to use parameters to get values into SQL, not string interpolation or concatenation. The latter two can cause funny errors and even make the program vulnerable to SQL injection attacks. Have a look [here](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – sticky bit Dec 11 '20 at 18:29

0 Answers0