-1

EDITED

I wrote an if in the php where if $consulta fetches a row then it throws an echo, but if it does not load a row with the insert

the problem is the if condition dont do nothing, every time I do the query insert the row, even if there is already an equal row. whats wrong?

function vincular(){
  $.ajax({
          url:   'api/vinculados.php',
          type:  'POST',
          data: {
            juridica: $("#sel_jur").val(),
            fisica: $("#mod_id_perfil").val(),
            usuario:  $("#mod_usuario_perfil").val()
          },
          success:  function (data) {
            if(data.status == "ok") {
              console.log(data);
            }
          }
  });
}
<?php

include 'conexion.php';



$consulta=mysqli_query($conexion, "SELECT * from perfiles_vinculados WHERE perfil_juridica = '$_POST[juridica]'");
if (mysqli_num_rows($consulta) > 0)
{
echo "ya existe";
} else {
  mysqli_query($conexion, "INSERT INTO perfiles_vinculados (perfil_juridica, perfil_fisica, usuario)
   VALUES ('$_POST[juridica]', '$_POST[fisica]', '$_POST[usuario]')");
}

 ?>
shios
  • 67
  • 8
  • 3
    Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – aynber Sep 20 '17 at 14:00
  • 1
    You are wide open for SQL injection. Since you're using mysqli (partially), take advantage of [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [bind_param](http://php.net/manual/en/mysqli-stmt.bind-param.php). **This will take care of any pesky quoting issues that may occur.** – aynber Sep 20 '17 at 14:01
  • you are using mysql and mysqli at the same time. that could be an issue. Use only mysqli – Jurick Pastechi Genaro Sep 20 '17 at 14:02
  • BTW there is no _PHP query_ – B001ᛦ Sep 20 '17 at 14:07
  • i edited the question, now the problem is always isert the row, as if the if statement did not exist – shios Sep 20 '17 at 14:09
  • You are still mixing APIs. You can NOT use mysql_* functions and mysqli_* functions together. Stop using the mysql_* functions all together as they've been deprecated. – aynber Sep 20 '17 at 14:15
  • Please tell me this isn't in production. – Rotimi Sep 20 '17 at 14:18
  • aynber, i change to mysqli, but still dosnt work – shios Sep 20 '17 at 14:22
  • no you didnt, change `mysql_num_rows` to `mysqli_num_rows` too – kscherrer Sep 20 '17 at 14:24
  • also, arrayvariables like $_POST['...'] will not be replaced inside Strings even with double quotes. concatenate the string instead: `WHERE perfil_juridica = '".$_POST[juridica]."'");` – kscherrer Sep 20 '17 at 14:26
  • all of you have reason jajajaja thanks, sorry for the problems, im beginner – shios Sep 20 '17 at 14:27
  • [the problem was that i was mixing mysql with mysqli](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) the comments of the question helped me a lot – shios Sep 20 '17 at 14:29
  • Please note on [mysqli_num_rows](http://php.net/manual/en/mysqli-result.num-rows.php): `The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. For unbuffered result sets, mysqli_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved.` – aynber Sep 20 '17 at 14:29

1 Answers1

0

Fix your php code to this

$juridica = $_POST['juridica'];

$consulta=mysqli_query($conexion, "SELECT * from perfiles_vinculados 
WHERE perfil_juridica = '$juridica'");

if (mysqli_num_rows($consulta) > 0)
{
echo "ya existe";
} else {
$fisica = $_POST['fisica'];
$usuario = $_POST['usuario'];
  mysqli_query($conexion, "INSERT INTO perfiles_vinculados 
(perfil_juridica, perfil_fisica, usuario)
   VALUES ('$juridica', '$fisica', '$usuario')");
}

Remember that $_POST variables required a string to be in the brackets, which requires ''.

Let me know if this helps you!

Edit: Oh also you're vulnerable to SQL injection. Be careful

Matthew Bergwall
  • 340
  • 1
  • 12