I am not understanding this error [closed]

-1

My PHP code:

<?php

include "conectar.php";


//comando para iserir dados direto do formulário para o banco de dados

$vnome=$_POST['nome'];
$vcpf=$_POST['cpf'];
$videntidade=$_POST["identidade"];
$vtelefone=$_POST["telefone"];
$vcelular=$_POST["celular"];
$vemail=$_POST["email"];
$vcep=$_POST["cep"];
$vendereco=$_POST["endereco"];
$vcomplemento=$_POST["complemento"];
$vbairro=$_POST["bairro"];
$vcidade=$_POST["cidade"];
$vuf=$_POST["uf"];
$vsexo=$_POST["sexo"];
$vidade=$_POST["idade"];
$vpeso=$_POST["peso"];

$sql="INSERT INTO solidario VALUES ('$vnome', '$vcpf', '$videntidade', '$vtelefone', '$vcelular', '$vemail', '$vcep', '$vendereco', '$vcomplemento', '$vbairro', '$vcidade', '$vuf', '$vsexo', '$vidade', '$vpeso')";

$dependente1=$_POST['dependente1'];
$cpfdependente1=$_POST['cpfdependente1'];
$enderecodependente1=$_POST['enderecodependente1'];
$ufdependente1=$_POST['ufdependente1'];
$sexodependnte1=$_POST['sexodependnte1'];
$idadedependente1=$_POST['idadedependente1'];
$pesodependente1=$_POST['pesodependente1'];

$sqla="INSERT INTO dependente1 VALUES ('$dependente1', '$cpfdependente1', '$ufdependente1', '$enderecodependente1', '$sexodependnte1', '$idadedependente1', '$pesodependente1')";


$res=mysqli_query($con,$sql,$sqla) or die(mysqli_error($con));
$num=mysqli_affected_rows($con);

if($num == 1){

And the error you're giving:

  

Warning: mysqli_query () expects parameter 3 to be integer, given in C: \ xampp \ htdocs \ solidario \ insert.php at line 37

    
asked by anonymous 21.10.2017 / 03:56

1 answer

4

Make sure you have a file by calling the database connection, so I created a .php file with the name conn.php and you should put the same name so that the code below works correctly, or if you already have a file doing this, change the name of the:

include("conn.php");

To

include("insiranomedoseuarquivo.php");

conn.php

<?php
$servername = "localhost"; /*nome do servidor, geralmente localhost funciona*/
$username = "root"; /* O nome do usuário do seu banco de dados */
$password = "senha "; /* A senha do usuário do banco de dados, se não tiver deixe => 
=> $password = "" */
$dbname = "exemplo"; /* O nome do banco a qual voce esta trabalhando */ 

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
?>

Add within your inputs the v in front of the names.

Example to input the field name <input name='vnome' and add this code below to insert the data, remembering that for UPDATE to work must have the id field in your table otherwise add the key field instead of id = '$id'

<?php
include("conn.php");

$vnome = $_POST['vnome']; 
$vcpf = $_POST['vcpf']; 
$videntidade = $_POST["videntidade"]; 
$vtelefone = $_POST["vtelefone"]; 
$vcelular = $_POST["vcelular"]; 
$vemail = $_POST["vemail"]; 
$vcep = $_POST["vcep"]; 
$vendereco = $_POST["vendereco"]; 
$vcomplemento = $_POST["vcomplemento"]; 
$vbairro = $_POST["vbairro"]; 
$vcidade=$_POST["vcidade"]; 
$vuf=$_POST["vuf"]; 
$vsexo=$_POST["vsexo"]; 
$vidade=$_POST["vidade"]; 
$vpeso=$_POST["vpeso"];

$sql = "SELECT * FROM solidario";
$resulta = $conn->query($sql);
$row = $resulta->fetch_assoc(); 

if ($resulta->num_rows > 0) {
    $result_solidario = "UPDATE solidario SET vnome = '$vnome', vcpf = '$vcpf', videntidade = '$videntidade', vtelefone = '$vtelefone', vcelular = '$vcelular, vemail = '$vemail', vcep = '$vcep', vendereco = '$vendereco', vcomplemento = '$vcomplemento', vbairro = '$vbairro', vcidade = '$vcidade', vuf = '$vuf', vsexo = '$vsexo', vidade = '$vidade', vpeso = '$vpeso' WHERE id = '$id' ";
} else {
    $result_solidario = "INSERT INTO solitario (vnome, vcpf, videntidade, vtelefone, vcelular, vemail, vcep, vendereco, vcomplemento, vbairro, vcidade, vuf, vsexo, vidade, vpeso) VALUES ('$vnome', '$vcpf', '$videntidade', '$vtelefone', '$vcelular', '$vemail', '$vcep', '$vendereco', '$vcomplemento', '$vbairro', '$vcidade', '$vuf', '$vsexo', '$vidade', '$vpeso')";
}

$resultado_produto = mysqli_query($conn, $result_solidario);
echo "$result_solidario <br>";

Do this to insert data into the solitario table and replicate to use for another table, and also repeat the INSERT command in this way as shown, you must enter where you will enter the fields coming from the form and then which variables will be responsible for entering data in these fields of the table you indicated.

And note that mysqli_query can only receive two parameters, and I indicated the variable used to create the connection to the database in the file conn.php and the variable responsible for receiving the insert command. p>

In this way the records will be inserted.

    
21.10.2017 / 04:12