Error inserting data into MySQL table

0

I'm trying to send data to MySQL from a restaurant reservation, but it only worked the first time and now when I try to send it will not go any further. What is the possible error in my script?

Connection File with 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();
         }  

?>

Query and 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>

    
asked by anonymous 27.03.2017 / 20:19

1 answer

1

Some of the data is poorly formatted as long as it is received from the form, and some are poorly defined in the attributes of the reservar table. 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 type of field data to time and not timestamp , because timestamp normally expects a much more complex Y-m-d H:i:s format which is a mixture of date and time with respective seconds. Also make sure that the id_reserva field is of type auto_increment .

  
  • alter table 'reserve' modify 'id_reserve' int (11) not null auto_increment;
  •   
  • alter table 'book' modify 'date' date;
  •   

But after you change the type of field data to time, you must first readjust the form data so that it fits this pattern.

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

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

    
27.03.2017 / 21:09