MucaP MucaP - 1 month ago 7
HTML Question

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)

Answer

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