Error inserting data with php and mysql

1

I have the following problem when trying to enter data to my bd these are the codes:

<html lang="en">
<head>
<meta charset="UTF-8">
<title> Creacion de Usuario Operador </title>
<link href="estilos.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="group">
<form action="crear_operador.php" method="POST">
<h2><em>Registro de Usuario Operador</em></h2>  

  <label for="rut">RUT: </label>
  <input type="text" name="rut" class="form-input" required/>

  <label for="clave">Clave: </label>
  <input type="password" name="clave" class="form-input" required/>

  <label for="institucion_cod_institucion">Cod Institucion:</label>
  <input type="text" name="institucion_cod_institucion" class="form-     input" required/>

 <label for="nombre_operador">Nombre: </label>
 <input type="text" name="nombre_operador" class="form-input" required/>

  <label for="ape_mat_operador">Apellido Materno: </label>
  <input type="text" name="ape_mat_operador" class="form-input" required/>

  <label for="ape_pat_operador">Apellido Paterno: </label>
  <input type="text" name="ape_pat_operador" class="form-input" required/>

  <center> <input class="form-btn" name="submit" type="submit" value="Crear Usuario Operador" /></center>

  <li><a href="indexadmin.html">Inicio</a></li>
  </p>
  </form>

 </div>

</body>
</html>

and the php code would be:

    <?php
    mysqli_connect('localhost','root','') or die ("error al conectar" .                 mysql_error());
    mysqli_select_db('proyecto') or die ("error seleccionar db" .     mysqli_error());

     $rut = $_POST['rut'];
     $clave = $_POST['clave'];
     $institucion_cod_institucion = $_POST['institucion_cod_institucion'];
     echo $institucion_cod_institucion;
     $nombre_operador = $_POST['nombre_operador'];
     $ape_mat_operador = $_POST['ape_mat_operador'];
     $ape_pat_operador = $_POST['ape_pat_operador'];


     $insertar = "insert into operador values('$rut', '$clave', $institucion_cod_institucion','$nombre_operador','$ape_mat_operador',  '$ape_pat_operador')";

    $resultado =msql_query($con, $insertar)
    or die("error al insertar datos");
    echo "datos ingresados";
?>

Thanks to your help I could verify the errors and solve them so I attach the correct code in case someone else has the same problem so I can solve it, many thanks to all those who helped me solve my problem     

$con = mysqli_connect($host,$user,$pw, $db) or die("no se pudo conectar");

$rut = $_POST['rut'];
$clave = $_POST['clave'];
$institucion_cod_institucion = $_POST['institucion_cod_institucion'];
$nombre_operador = $_POST['nombre_operador'];
$ape_mat_operador = $_POST['ape_mat_operador'];
$ape_pat_operador = $_POST['ape_pat_operador'];

$insertar = "insert into operador (rut, clave, institucion_cod_institucion, nombre_operador, ape_mat_operador, ape_pat_operador) values
 ('$rut', '$clave', '$institucion_cod_institucion','$nombre_operador','$ape_mat_operador','$ape_pat_operador')";

$resultado =mysqli_query($con, $insertar) or die("error al insertar datos");
echo "datos ingresados";
?>
    
asked by Tondrax 20.07.2017 в 11:11
source

4 answers

3
<?php 
$resultado = msql_query($con, $insertar);

That line does not look right, I think it should be mysql_query , not msql_query.

The code snippet also does not see where you initialize $con (where you make the connection to mysql).

    
answered by 20.07.2017 в 11:18
2

The insert query is not correct, assuming that you connect well with the database (check the connection) and that this may be the error. A quote is missing even if you do not include the fields.

$insertar = "insert into operador (campo1, campo2, campo3, campo4, campo5, campo6) values ('".$rut."', '".$clave."', '".$institucion_cod_institucion."','".$nombre_operador."','".$ape_mat_operador."',  '".$ape_pat_operador."') ";
    
answered by 20.07.2017 в 11:19
2

Make sure that the connection string is working correctly and correct the errors that you have indicated. I understand that mysql is already obsolete, I recommend that you work with mysqli.

    
answered by 20.07.2017 в 19:13
0

Initially check if the name of the fields in your operator table are the same as those in the INSERT. For me the insertion query is correct adding the quote that is missing before $ institution_cod_institucion, and if the rut field is the primary key, but if you have a field auto_increment as primary key it should be like this:

$insertar = "insert into operador (rut, clave, institucion_cod_institucion, nombre_operador, ape_mat_operador, ape_pat_operador) values
	 ('$rut', '$clave', '$institucion_cod_institucion','$nombre_operador','$ape_mat_operador','$ape_pat_operador')";
    
answered by 20.07.2017 в 18:57