Insert data by PHP

Asked

Viewed 54 times

0

After submitting the form I do the following:

 $n_processo = $_POST['n_processo'];
 $nome = $_POST['nome'];
  $dia = $_POST['dia'];
  $mes=$_POST['mes'];
  $ano = $_POST['ano'];
  $data = $dia."-".$mes."-".$ano;
    $cc = $_POST['cc'];
    $curso =$_POST['cursos'];
  $ciclo1 = $_POST['ciclo'];
  $ciclo2 = $_POST['ciclo2'];
  $ciclo = $ciclo1."-".$ciclo2;` 


    $sqlcurso = mysql_query("SELECT n_curso FROM cursos WHERE designacao LIKE '".$curso."'");

    $row = mysql_fetch_array($sqlcurso);    


    $sql = "INSERT INTO alunos(n_processo,nome,data_nasc,cc,n_curso,ciclo_formacao)
          VALUES ('".$n_processo."','".$nome."','".$data."','".$cc."','".$row['n_curso']."','".$ciclo."')";

    if(!mysql_query($sql))
    {
        echo"1";
    }
    else
    {
        $sql2 = mysql_query("SELECT n_processo FROM alunos WHERE n_processo = '".$n_processo."'");
      $sql22 = mysql_num_rows($sql2);

      if($sql22 > 0)
      {
        echo "2";
      }
      else
      {
        echo "3";
      }
  }

I was confused because supposedly the exclamation mark(!) means different as in the query above indicates when the query did not work, gave a message, below did the query to see if there is already any record with that primary key, if it exists from message 2 otherwise it gives message 3 which means it was entered successfully, but it is only inserted when it returns message 2 which means it already exists? I’ll be messing things up?

  • You noticed there’s a ` out there right? Its code is not safe, it is using an obsolete function and, moreover, it is fully open to SQL Injection attacks. And yes, your doubt has become very confusing. Try to edit and separate the questions by parts, one by one or simply tell us the problem / what you want to do.

  • Hello, echo 1 will be when it is wrong. echo 2 will appear when it is not wrong. echo 3 will never appear because it will always exist. That was his point?

  • Clayderson, I wanted to understand why message 2 should appear instead of message 3, but as Tiago said, 3 will always exist. Why? In the query if there is no data will be = 0 then message 3 should not appear and not message 2 is not?

  • 1

    Hello, @programmer2016 will always exist because an Insert is made before.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.