PHP + MYSQL将所有列插入null

时间:2015-04-24 16:13:42

标签: php html mysql forms post

我在add.html中有一个表单,我正在尝试使用此表单的值在mysql表中添加一行,但是除了id(PK,AI)之外,新行添加了所有列NULL。我认为这可能是$ _POST变量的任何问题......然后我用

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

然后,如果$ _POST ['nome']为空,$ nome将在表中为“nomevazio”但不是。它仍然插入一行,所有列都为null。

有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>

并且有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");
}
}

?>

2 个答案:

答案 0 :(得分:2)

我认为你的INSERT语法有点过时了。刚在我的服务器上尝试了相同的语法,所有内容都插入了NULL。

更改为:

$arrName[0]->getDescription();

在这里看看各种INSERT语法: http://www.w3schools.com/sql/sql_insert.asp

答案 1 :(得分:1)

您的语法为false:

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

此外,您应该使用预备语句。你应该检查错误。