How to make records in the database with PHP?

1

This is the code I have made

       <?php
      $enlace = mysqli_connect("localhost", "root", "", "loteriav2");
if (!$enlace) {
    echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
    echo "errno de depuración: " . mysqli_connect_errno() . PHP_EOL;
    echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
  $Nombre_loteria = $_POST['nombre'];
  mysqli_query($enlace, "INSERT INTO loterias ('Nombre_loteria') VALUES ($Nombre_loteria)");
  mysqli_query($enlace, "INSERT INTO datos_2dig ('N_loteria') VALUES ($Nombre_loteria)");
  mysqli_query($enlace, "INSERT INTO datos_2dig ('ultimo_ingreso_2dig') VALUES (0)");
  for ($i=1; $i <= 100; $i++) {
    $sql = "INSERT INTO datos_2dig ('dig". $i ."') VALUES (0)";
    mysqli_query($enlace, $sql);
  }
  mysqli_query($enlace, "INSERT INTO datos_3dig ('N_loteria') VALUES ($Nombre_loteria)");
  mysqli_query($enlace, "INSERT INTO datos_3dig ('ultimo_ingreso_2dig') VALUES (0)");
  for ($i=1; $i <= 1000; $i++) {
    $sql = "INSERT INTO datos_2dig ('dig". $i ."') VALUES (0)";
    mysqli_query($enlace, $sql);
  }
  header("Location: index.php");
?>

The page simply does not do anything, it does not register anything in the database. It's as if it did not exist, it even throws me an error message or something similar. I know it may seem very silly but I have very little practice, I would like to know what I am wrong about.      In advance, thank you very much, who can give me a hand with my problem. for all who want here is the full code on GitHub link

    
asked by Gianfranco G. Manganiello T. 28.08.2016 в 01:28
source

1 answer

1

Testing the code of your github repository, the error is what @AlvaroMontoro says the quotes, now your querys are like this:

  $Nombre_loteria = $_POST['nombre'];
  mysqli_query($enlace, "INSERT INTO loterias ('Nombre_loteria') VALUES ($Nombre_loteria)");
  mysqli_query($enlace, "INSERT INTO datos_2dig ('N_loteria') VALUES ($Nombre_loteria)");
  mysqli_query($enlace, "INSERT INTO datos_2dig ('ultimo_ingreso_2dig') VALUES (0)");

They should go like this:

  $Nombre_loteria = $_POST['nombre'];
  mysqli_query($enlace, "INSERT INTO loterias (Nombre_loteria) VALUES ('$Nombre_loteria')") or die("q1".mysqli_error($enlace));
  mysqli_query($enlace, "INSERT INTO datos_2dig (N_loteria) VALUES ('$Nombre_loteria')") or die("q2".mysqli_error($enlace));
  mysqli_query($enlace, "INSERT INTO datos_2dig (ultimo_ingreso_2dig) VALUES ('0')") or die("q3".mysqli_error($enlace));
  for ($i=1; $i <= 100; $i++) {
    $sql = "INSERT INTO datos_2dig (dig". $i .") VALUES (0)";
    mysqli_query($enlace, $sql);
  }
  mysqli_query($enlace, "INSERT INTO datos_3dig (N_loteria) VALUES ('$Nombre_loteria')") or die("q4".mysqli_error($enlace));
  mysqli_query($enlace, "INSERT INTO datos_3dig (ultimo_ingreso_2dig) VALUES (0)") or die("q5".mysqli_error($enlace));
  for ($i=1; $i <= 1000; $i++) {
    $sql = "INSERT INTO datos_2dig (dig". $i .") VALUES (0)";
    mysqli_query($enlace, $sql) or die("q6".mysqli_error($enlace));
  }

Although it will still fail because in your database the column ultimo_ingreso_2dig is declared as not null, so the insert of the second query will fail, to correct that you can insert that data in the second query without doing so separated in the third query

mysqli_query($enlace, "INSERT INTO loterias (Nombre_loteria) VALUES ('$Nombre_loteria')") or die("q1".mysqli_error($enlace));
mysqli_query($enlace, "INSERT INTO datos_2dig (N_loteria, ultimo_ingreso_2dig) VALUES ('$Nombre_loteria',0)") or die("q2".mysqli_error($enlace));

in the second query you enter the two columns in a single call.

    
answered by 28.08.2016 / 05:01
source