简体   繁体   中英

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. I tried a lot of things, and that was my result (keep in mind that everything is located at the same file):

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. Note that I'm trying to add value to all the 4 columns I have. What is wrong here?

(Please ignore these divs, i'm using Materialize, that's just CSS)

You have few error inside your code.

1) First of all, you have closed your form before submit button. Remove that </form> . Hope you will get some result.

2) Update your form starting like this <form action="post"> .

3) Update your form posting checking like this

 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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