简体   繁体   中英

My PHP code isn´t inserting the values into the Database

I am trying to make a code that will capture the data inside some form and insert it into my database in Wamp server, but for some reason it isn´t working even though I am sure I did it with a lot of care.

Here is the relevant code:

HTML

  <form id="frmLabs" method="post">
    <div class="divFormlabs">
      <label for="txtCapacidad">Capacidad</label>
      <input type="text" name="txtCapacidad" id="txtCapacidad">
      <label for ="txtCantEquipos">Carrera</label>

      <select type="text" name="txtCarrera" id="txtCarrera">
          <option value="Desarrollo">Desarrollo</option>
          <option value="Diseño">Diseño</option>
          <option value="Telematica">Telemática</option>
          <option value="T.I">T.I</option>
    </select>

    </div>

      <div class="divFormlabs">
      <label for="txtNumLab">Número de laboratorio</label>
      <input type="text" name="txtNumLab" id="txtNumLab">
      <label for="txtUbicacion">Ubicación</label>
      <input type="text" name="txtUbicacion" id="txtUbicacion">
    </div>
  </form>

jQuery

$("#btnAceptar").click(function() {
  var idlab = $('#txtNumLab').val(),
  capacidad = $('#txtCapacidad').val(),
  carrera = $('txtCarrera').val(),
  ubicacion = $('txtUbicacion').val();


var request = $.ajax({
 url: "includes/functionsLabs.php",
 type: "post",
 data: {

    'call': 'addLab',
    'pIdLab':idlab,
    'pCapacidad':capacidad,
    'pCarrera':carrera,
    'pUbicacion':ubicacion},

     dataType: 'json',

     success: function(response){
        alert('exito');
    }
  });
});

PHP

function addLab(){
if(isset($_POST['pIdLab']) && 
    isset($_POST['pCapacidad']) &&
    isset($_POST['pCarrera']) &&
    isset ($_POST['pUbicacion'])){

    $idlab = $_POST['pIdLab'];
    $capacidad = $_POST['pCapacidad'];
    $carrera = $_POST['pCarrera'];
    $ubicacion = $_POST['pUbicacion'];

    $query = "INSERT INTO labs VALUES" . "('$idlab', '$capacidad','$carrera',
 '$ubicacion')";

    $result = do_query($query);


}

}


 if($_SERVER['REQUEST_METHOD']=="POST") {
$function = $_POST['call'];
if(function_exists($function)) {        
    call_user_func($function);
} else {
    echo 'Function doesnt exists!';
}
   }



?>

The order of the rows in my database in the table Labs is exactly the one in the code:

IdLab Capacidad Carrera Ubicacion

Also please forgive me for putting the variables in another languaje but it cannot be helped >.< Any kind of assistance would be greatly appreciated.

Best Wishes and thanks in advance.

EDIT:

This is the code for btnAceptar.

<input class="formatButton verInfo2"  type="button" value="Aceptar"   id="btnAceptar"onclick="window.location='labs.html';" />

You are not assigning your variables correctly:

var idlab = $('#txtNumLab').val(),
  capacidad = $('#txtCapacidad').val(),
  carrera = $('txtCarrera').val(),
  ubicacion = $('txtUbicacion').val();

Note that the last two are missing the # in the selector, so your if condition in your php file will never be met.

You can replace the way you assign your data with:

data: $('#frmLabs').serialize(),

Edit: Note that because of the sql injection problem you have, your query could fail if one of the fields contains for example a ' character.

first:

$("#btnAceptar").click(function(e) {
e.preventDefault();
idlab = $('#txtNumLab').val(),
capacidad = $('#txtCapacidad').val(),
carrera = $('#txtCarrera').val(),
ubicacion = $('#txtUbicacion').val();


var request = $.ajax({
url: "includes/functionsLabs.php",
type: "post",
data: {
'call': 'addLab',
'pIdLab':idlab,
'pCapacidad':capacidad,
'pCarrera':carrera,
'pUbicacion':ubicacion},

 dataType: 'json',

 success: function(response){
    alert('exito');
   }
  });
});

Then in includes/functionsLabs.php:

print_r($_POST);

or:

echo $query;

And quit onclick="window.location='labs.html';" of you btnAceptar:

Recommended use firebug complement for test

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