将记录插入mysql echo表中?

时间:2016-10-16 14:36:04

标签: php html mysql sql

最近我为联系人启动了一个echo mysql表,现在我需要一个按钮来添加新的联系人。我尝试了很多东西,这是我的结果(请记住,所有内容都位于同一个文件中):

PHP:

//create record
if (isset($_POST['submitc'])) {
$empresa = $_POST['empresa'];
$contato = $_POST['contato'];
$telefone = $_POST['telefone'];
$email = $_POST['email'];
$sql = $conn->query("INSERT INTO Contacts (empresa, contato, email, phone) 
VALUES ('$_POST[empresa]', '$_POST[contato]', '$_POST[telefone]',      
'$_POST[email]')");

if(!$sql) {
echo ("Could not create" .mysqli_error());
  }
}

形式:

<form method=post>
  <div class='input-field'>
  <i class='material-icons prefix'>work</i>
      <input id='first_name' name='empresa' type='text' class='validate'>
      <label for='first_name'>Empresa</label>
    </div>

    <div class='input-field'>
    <i class='material-icons prefix'>account_circle</i>
      <input id='first_name' name='contato' type='text' class='validate'>
      <label for='first_name'>Contato</label>
    </div>

    <div class='input-field'>
    <i class='material-icons prefix'>phone</i>
      <input id='first_name' name='telefone' type='text' class='validate'>
      <label for='first_name'>Telefone</label>
    </div>

    <div class='input-field'>
    <i class='material-icons prefix'>email</i>
      <input id='first_name' name='email' type='text' class='validate'>
      <label for='first_name'>E-mail</label>
    </div>
  </form>

</div>
<div class='modal-footer'>
<button class='green darken-4 waves-effect waves-light btn' type='submit'       
name='submitc' value='Add'>Criar</button>
   </form>

正如你在PHP部分中看到的,我已经尝试过很多东西,比如使用vars,$ _POST但是当我点击提交按钮时,没有任何反应,甚至没有错误告诉我一些事情。请注意,我正在尝试为我拥有的所有4列添加值。这有什么不对?

(请忽略这些div,我正在使用Materialise,这只是CSS)

4 个答案:

答案 0 :(得分:1)

您的代码中几乎没有错误。

1)首先,您已在提交按钮之前关闭了表单。删除</form>。希望你能得到一些结果。

2)从此<form action="post">开始更新您的表单。

3)更新您的表单发布检查,如下所示

 if (!empty($_POST)) {
 echo "test<br/>";
 $empresa = $_POST['empresa'];
 $contato = $_POST['contato'];
 $telefone = $_POST['telefone'];
 $email = $_POST['email'];
 $sql = $conn->query("INSERT INTO Contacts (empresa, contato, email, phone) 
 VALUES ('$_POST[empresa]', '$_POST[contato]', '$_POST[telefone]',      
 '$_POST[email]')");

 if(!$sql) {
 echo ("Could not create" .mysqli_error());
 }
 }

答案 1 :(得分:1)

我认为这就是你要做的事。

if (isset($_POST['submitc'])) {
$empresa = $_POST['empresa'];
$contato = $_POST['contato'];
$telefone = $_POST['telefone'];
$email = $_POST['email'];
$sql = "INSERT INTO Contacts (empresa, contato, email, phone) VALUES('$empresa', '$contato', '$telefone', '$email')";
$insert = $conn->query($sql);
      if ( $insert) {
          header('Location: name.php');
      }else {
          echo "Error: " . $sql . "<br>" . mysqli_error($conn);
      }
}

FORM REPLACED THIS 
<form method="post" action="">
  <div class='input-field'>
  <i class='material-icons prefix'>work</i>
      <input id='first_name' name='empresa' type='text' class='validate'>
      <label for='first_name'>Empresa</label>
    </div>

    <div class='input-field'>
    <i class='material-icons prefix'>account_circle</i>
      <input id='first_name' name='contato' type='text' class='validate'>
      <label for='first_name'>Contato</label>
    </div>

    <div class='input-field'>
    <i class='material-icons prefix'>phone</i>
      <input id='first_name' name='telefone' type='text' class='validate'>
      <label for='first_name'>Telefone</label>
    </div>

    <div class='input-field'>
    <i class='material-icons prefix'>email</i>
      <input id='first_name' name='email' type='text' class='validate'>
      <label for='first_name'>E-mail</label>
    </div>

<div class='modal-footer'>
<button class='green darken-4 waves-effect waves-light btn' type='submit'       
name='submitc' value='Add'>Criar</button>
</div>
</form>

答案 2 :(得分:0)

//create record
if (isset($_POST['submitc'])) {
$empresa = $_POST['empresa'];
$contato = $_POST['contato'];
$telefone = $_POST['telefone'];
$email = $_POST['email'];

$sql = $conn->query("INSERT INTO `contacts`(`empresa`, `contato`, `email`, `phone`) 
VALUES('$empresa', '$contato', '$email', '$telefone');");

if(!$sql) {
echo ("Could not create" .mysqli_error());
  }
}

&#13;
&#13;
<form method="post">
  <div class='input-field'>
     <i class='material-icons prefix'>work</i>
     <input id='first_name' name='empresa' type='text' class='validate'>
     <label for='first_name'>Empresa</label>
  </div>

  <div class='input-field'>
      <i class='material-icons prefix'>account_circle</i>
      <input id='first_name' name='contato' type='text' class='validate'>
      <label for='first_name'>Contato</label>
  </div>

  <div class='input-field'>
      <i class='material-icons prefix'>phone</i>
      <input id='first_name' name='telefone' type='text' class='validate'>
      <label for='first_name'>Telefone</label>
   </div>

   <div class='input-field'>
      <i class='material-icons prefix'>email</i>
      <input id='first_name' name='email' type='text' class='validate'>
      <label for='first_name'>E-mail</label>
   </div>
  
   <div class='modal-footer'>
      <button class='green darken-4 waves-effect waves-light btn' type='submit'       
name='submitc' value='Add'>Criar</button>
   </div>
</form>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

更改为:

<form method="post" action="post.php">

其中post.php是您的POST操作文件,并在顶部

添加到此文件
if($_SERVER['REQUEST_METHOD'] == "POST") 
{
.... YOU CODE ...
}