Error inserting data into Mysql table

Asked

Viewed 1,038 times

0

I’m trying to send data to the MySQL , of a restaurant reservation , but it only worked the first time and now when I try to send will no longer. What possible error in my script?

File Connection with the MySQL:

 <?php

    session_start();
        $servidor = "localhost";
        $usuario = "root";
        $senha = "";
        $dbname = "sistema";

        //Criar a conexao

            error_reporting(0);
        $link = new mysqli ("localhost", "root", "", "sistema");
         if($link->connect_errno){
             echo"Nossas falhas local experiência ..";
             exit();
         }  

?>

The Query and the Form:

 <?php

        if($_SERVER['REQUEST_METHOD'] == 'POST') {
        $mesa=$_POST['mesa'];
        $nome=$_POST['nome'];
        $telefone=$_POST['telefone'];
        $data=$_POST['data'];
        $hora=$_POST['hora'];
        $sql="INSERT INTO reservar(mesa,nome,telefone,data,hora) VALUES('$mesa','$nome','$telefone','$data','$hora')";
        $resultado_cadastro = mysqli_query($link,$sql);
    }


        ?>

    <form method="post" action="http://localhost/peixaria/inicio2.php?btn=entrega"> 
<div class="reservations">
  <h1>Reservar:</h1>

         <p>Mesa: </p>
      <input type="text" name="mesa" class="form" required>  


      <p>Nome: </p>
      <input type="text" name="nome" class="form" required> 
      <p>Telefone: </p>

    <input type="text" name="telefone" class="form" required> 
        <p>Data: </p>
    <input type="date" name="data" class="form" required placeholder="dd/mm/jjjj"> 
       <p>Hora: </p>
    <input type="time" name="hora" class="form" required placeholder="14:30"> 

  <button type="submit" >enviar</button>
</div>

<div class="thankyou">
  <i class="fa fa-check-square-o"></i>

</div>

<div id="dtBox"></div>
</form>

inserir a descrição da imagem aqui

  • @Edilson still doesn’t work .

  • The form and the PHP code that captures the data, are in the same file . php ?

  • yes is in the same file @Alissonacioli

  • Ah, I didn’t read the code right, the database was there, I’ll read it better, but you’re also misreading the values in the function mysql_query, you should put the words between double quotes or curly braces. From any form to your script seems a bit confusing, give me some time and I’ll see it better.

  • Can you post the table structure for better analysis? I suggest using the command desc nome_tabela; in phpmyadmin or other manager you use.

  • @Maurivan already posted

  • No error message is displayed?

  • @Jefersonleonardo displays nothing and does not send , alias only sent the first time .

  • 1

    Note that you are not passing value to the field id_reserva and the same is not auto_increment. Do you have any Rigger that does this job?

Show 4 more comments

1 answer

1


Some of the data are poorly formatted since it is received from the form, and others are by poor definition in table attributes reservar. Using this code here, you can solve the problem:

<?php
session_start();
$servidor = "localhost";
$usuario = "root";
$senha = "";
$dbname = "sistema";

$link = new mysqli ($servidor, $usuario, $senha, $dbname);
if($link->connect_errno){
 echo "Nossas falhas local experiência ..";
 exit();
}  


if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $mesa = $_POST['mesa'];
    $nome = $_POST['nome'];
    $telefone = $_POST['telefone'];
    $data = date('Y-m-d', strtotime($_POST['data']));
    $hora = date('H:m:s', strtotime($_POST['hora']));

    $sql = "INSERT INTO reservar (mesa,nome,telefone,data,hora) VALUES('{$mesa}','{$nome}','{$telefone}','{$data}','{$hora}')";
    if($resultado_cadastro = $link->query($sql)){
        print "dados cadastrados na tabela";
    } else {
        print "ocorreu um erro durante o cadastramento da informação";
    }
}    

?>

You also need to change the field type data for time and not timestamp, because the timestamp normally expect a much more complex format Y-m-d H:i:s which is a mixture of date and time with their seconds. Also make sure that the field id_reserva be the type auto_increment.

  • alter table`reserve` Modify `id_reserve` int(11) not null auto_increment;
  • alter table`reserve` Modify `date` date;

But after you changed the camp guy data for team, you should first readjust the form data, so it fits this pattern.

  • $data = date('Y-m-d', strtotime($_POST['date']));
  • $time = date('H:m:s', strtotime($_POST['time']));

The rest are errors, even structuring, because in your code are declared variables that are not even used - session, $server, etc - and instead of getting mixed up procedure mysqli with the object oriented mysqli, it’s easier if you pick one and just use it that way.

  • It seems to me that He is trying to insert some date in the field datach, for data has by default current_timestamp. His code is really messed up!

  • @Maurivan really.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.