简体   繁体   English

将记录插入mysql echo表?

[英]Insert record into mysql echo table?

Recently I started an echo mysql table for contacts, and now I need a button to add a new contact. 最近,我为联系人启动了echo mysql表,现在我需要一个按钮来添加新的联系人。 I tried a lot of things, and that was my result (keep in mind that everything is located at the same file): 我尝试了很多事情,这就是我的结果(请记住,所有内容都位于同一文件中):

PHP: 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: 形成:

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

As you can see in the PHP Part, I already tried a lot of things like using vars, $_POST but when I click the submit button, nothing happens, not even an error telling me something. 如您在PHP部分中所看到的,我已经尝试了很多类似使用vars,$ _ POST的方法,但是当我单击Submit按钮时,什么也没有发生,甚至没有错误告诉我一些事情。 Note that I'm trying to add value to all the 4 columns I have. 请注意,我正在尝试为所有4列增加价值。 What is wrong here? 怎么了

(Please ignore these divs, i'm using Materialize, that's just CSS) (请忽略这些div,我使用的是Materialize,那只是CSS)

You have few error inside your code. 您的代码中几乎没有错误。

1) First of all, you have closed your form before submit button. 1)首先,您先关闭表单,然后再提交按钮。 Remove that </form> . 删除</form> Hope you will get some result. 希望你能得到一些结果。

2) Update your form starting like this <form action="post"> . 2)像这样<form action="post">开始更新您的表单。

3) Update your form posting checking like this 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());
 }
 }

I think this is what you are trying to do . 我认为这就是您要尝试做的。

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>
//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());
  }
}

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

Change to: 改成:

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

where post.php is your POST action file and add to this file at the top 其中post.php是您的POST操作文件,并添加到该文件的顶部

if($_SERVER['REQUEST_METHOD'] == "POST") 
{
.... YOU CODE ...
}

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

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