0

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)

6
  • well for one thing, you have the ordering backwards for email and phone columns in the query where you've switched them in the values. Commented Oct 16, 2016 at 14:39
  • Also, what is that lone </form> doing there? You closed the form before the submit button. Commented Oct 16, 2016 at 14:41
  • @RajdeepPaul No, it is after the submit button... Commented Oct 16, 2016 at 14:46
  • @Fred-ii- Oh, yeah, thank you. Still not working tho. Commented Oct 16, 2016 at 14:47
  • @MucaP I'm talking about this: ... <label for='first_name'>E-mail</label></div></form>, can you see this closing form tag right after the email input field? This is why your submit button is not working. Commented Oct 16, 2016 at 14:48

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

I prefer my code to you with this it's helpful and it is working to me.
No problem @MucaP
1

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

2 Comments

Can you explain a little more the third?
Also you need to add your database connection info with your code
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());
  }
}

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

2 Comments

can you insert full php script please
168 lines long, I prefer not to
0

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

1 Comment

Ahm, but I have an echo table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.