简体   繁体   English

PHP + MYSQL插入所有列null

[英]PHP + MYSQL inserting all columns null

I have a form in add.html and I'm trying to add a row in mysql table with the values of this form but the new row is added with all columns NULL except id (PK, AI). 我在add.html中有一个表单,我正在尝试在mysql表中添加具有该表单值的行,但是新行中添加了除id(PK,AI)以外的所有NULL列。 I thought that could be any problem with the $_POST variables ...then I checked with 我以为$ _POST变量可能有任何问题...然后我检查了

if(isset($_POST['nome']))
    $nome=mysqli_real_escape_string($db,$_POST['nome']); 
else 
    $nome="nomevazio";  

then, if the $_POST['nome'] is empty the $nome would be "nomevazio" in table but not. 然后,如果$ _POST ['nome']为空,则表中的$ nome将为“ nomevazio”,但不是。 It still insert a row with all columns null. 仍然插入所有列为空的行。

There's the form html code: 有形式的html代码:

          <form class="col s12" id="myForm" action="addparceiro.php" method="POST" >
            <div class="row">
              <div class="input-field col s6">
                <i class="mdi-action-account-circle prefix"></i>
                <input  id="name" type="text" class="validate" name="nome">
                <label for="name">Nome do Parceiro</label>
              </div>            
              <div class="input-field col s6">
                <i class="mdi-communication-email prefix"></i>
                <input id="email" type="email" class="validate" name="email">
                <label for="email">Email</label>
              </div>
            </div>
            <div class="row">
              <div class="input-field col s12">
                <i class="mdi-communication-location-on prefix"></i>
                <input id="address" type="text" class="validate" name="endereco">
                <label for="address">Endereço</label>
              </div>
            </div>
            <div class="row">
              <div class="input-field col s12">
                <i class="mdi-action-settings-phone prefix"></i>
                <input id="phone" type="number" class="validate" name="telefone">
                <label for="phone">Telefone</label>
              </div>
            </div>
            <div class="row">
              <div class="input-field col s12">
                <i class="mdi-action-restore prefix"></i>
                <input type="date" class="datepicker" name="data">
                <label for="date">Data de cadastro</label>
              </div>
            </div> 
            <div class="row">
              <div class="input-field col s12">
                <i class="mdi-action-lock prefix"></i>
                <input id="login" type="text" class="validate" name="login">
                <label for="login">Login</label>
              </div>
            </div>

            <div class="row">
              <div class="input-field col s12">
                <i class="mdi-communication-vpn-key prefix"></i>
                <input id="password" type="password" class="validate" name="senha">
                <label for="password">Senha</label>
              </div>
            </div>
            <div class="row">
              <div class="input-field col s12 center">
                <a onclick="Materialize.toast('CADASTRO REALIZADO COM SUCESSO', 4000)">
                  <button id="envia" class="btn waves-effect waves-light" type="submit" value="submit form" name="action">Enviar
                    <i class="mdi-content-send right"></i>
                  </button>
                </a>
              </div>
            </div>            
          </form>

and there's the addparceiro.php code 还有addparceiro.php代码

<?php
include("config.php");
session_start();
// if(isset($_SESSION['login_user'])
//  {
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from Form
if(isset($_POST['nome']))
    $nome=mysqli_real_escape_string($db,$_POST['nome']); 
else 
    $nome="nomevazio";  
$email=mysqli_real_escape_string($db,$_POST['email']); 
$endereco=mysqli_real_escape_string($db,$_POST['endereco']); 
$telefone=mysqli_real_escape_string($db,$_POST['telefone']); 
$data=mysqli_real_escape_string($db,$_POST['data']); 
$login=mysqli_real_escape_string($db,$_POST['login']); 
$senha=mysqli_real_escape_string($db,$_POST['senha']); 

$sql="INSERT INTO TB_ESTABELECIMENTOS (nome, endereco, email, telefone, data, login, senha) VALUES (nome='$nome', endereco='$endereco', email='$email', telefone='$telefone', data='$data', login='$login', senha='$senha')";
$result=mysqli_query($db,$sql);

if($result)
{

include("lista.html");
}
else 
{       
 echo
        '
            <script>
               alert(\'Não foi possível cadastrar este parceiro! Tente novamente mais tarde ou contate a equipe técnica!\');
            </script> 
        ';
    include("add.html");
}
}

?>

I think your INSERT syntax is a bit off. 我认为您的INSERT语法有些偏离。 Just tried this same syntax on my server and everything was inserted NULL. 刚在我的服务器上尝试了相同的语法,并且所有内容都插入了NULL。

Change to: 改成:

"INSERT INTO TB_ESTABELECIMENTOS 
(nome, endereco, email, telefone, data, login, senha) 
VALUES 
('$nome', '$endereco', '$email', '$telefone', '$data', '$login', '$senha');

Take a look at various INSERT syntax here: http://www.w3schools.com/sql/sql_insert.asp 在这里查看各种INSERT语法: http : //www.w3schools.com/sql/sql_insert.asp

Your Syntax is false: 您的语法为假:

$sql="INSERT INTO TB_ESTABELECIMENTOS (nome, endereco, email, telefone, data, login, senha) VALUES ('$nome', '$endereco', '$email', '$telefone','$data', '$login', '$senha')";

Also you should use prepared statements. 另外,您应该使用准备好的语句。 And you should check for Errors. 并且您应该检查错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM