Doubt - UPDATE MYSQL with PHP

0

I need to update to the mysql database but I can not.

I'm basically following these steps:

  • I get bank variables that are similar to a particular CPF and display 2 people
  • The user will click a button to select 1 person
  • Once the user clicks this button I need to make 1 update in 1 table field.

What is it like?

Code:

         

<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.js"></script><scriptsrc="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.js"></script><scriptsrc="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>

<link href="css/bootstrap.css" rel="stylesheet">

<title></title>
</head>

<body>

<?php



    $conexao = mysqli_connect("localhost","root","") 
    or die("Erro na conexão com banco de dados");      
    $select_db = mysqli_select_db($conexao,"mentoria"); 
    $campos = array(".", "-");      

    if(isset($_FILES['foto'])){ 
    $extensao = strtolower(substr($_FILES['foto']['name'], -4));
    $novo_nome = md5(time()). $extensao;
    $diretorio = "imagem/";
    move_uploaded_file($_FILES['foto']['tmp_name'], $diretorio.$novo_nome);
}

    $cpf = str_replace($campos,"",$_POST["inputCPF"]);

    $consulta_jovens = "SELECT jfoto,jnome,jfaixaetaria,jescolaridade,jhobby,jcomida,jmusica,jesporte,jtime,jcaracteristica,janimal,jlivro,jsonho, ((CASE WHEN jovem.jfaixaetaria = mentor.mfaixaetaria THEN 1 ELSE 0 END)+(CASE WHEN jovem.jescolaridade = mentor.mescolaridade THEN 1 ELSE 0 END)+(CASE WHEN jovem.jhobby = mentor.mhobby THEN 1 ELSE 0 END)+(CASE WHEN jovem.jcomida = mentor.mcomida THEN 1 ELSE 0 END)+(CASE WHEN jovem.jmusica = mentor.mcomida THEN 1 ELSE 0 END)+(CASE WHEN jovem.jesporte = mentor.mesporte THEN 1 ELSE 0 END)+(CASE WHEN jovem.jtime = mentor.mtime THEN 1 ELSE 0 END)+(CASE WHEN jovem.jcaracteristica = mentor.mcaracteristica THEN 1 ELSE 0 END)+(CASE WHEN jovem.janimal = mentor.manimal THEN 1 ELSE 0 END)+(CASE WHEN jovem.jlivro = mentor.mlivro THEN 1 ELSE 0 END)+(CASE WHEN jovem.jsonho = mentor.msonho THEN 1 ELSE 0 END)) AS qtdCaracteristicasIguais FROM jovem INNER JOIN mentor ON jovem.jcidade = mentor.mcidade AND jovem.jestado = mentor.mestado WHERE mentor.mcidade = jovem.jcidade AND mentor.mestado   = jovem.jestado AND mentor.CPF ='$cpf' and jselecionado IS null ORDER BY qtdCaracteristicasIguais DESC LIMIT 2";

    $string_sql = "SELECT * FROM mentor WHERE CPF='$cpf'";      

    $result= mysqli_query($conexao, $string_sql); 

    $resultadojovens = mysqli_query($conexao,$consulta_jovens);

    $dado = mysqli_fetch_array($result);    


    $nome = $dado['mnome']; 




    if(mysqli_affected_rows($conexao)){ 


        echo "<p><h2>Olá, $nome! <br><br> Escolha o seu mentorando abaixo:</h2></p>
        <br>
        <br>
        <p></p>";
        while ($aluno = mysqli_fetch_array($resultadojovens)){


        echo '<form id="formSelecionar" action="selecao.php" name="formSelecionar" method="post"><div  style="position: relative; float: left; margin: .5%; width: 25%; margin-left: 10%;"><div style="width:250px; height:250px;"><img style="width:70%" src=imagem/'.($aluno['jfoto']).'></div>'."<br />" .'<span class="glyphicon glyphicon-user" style="margin-right: 5px;"></span><b>Nome: </b>'.$aluno['jnome']."<br />".'<span class="glyphicon glyphicon-calendar" style="margin-right: 5px;"></span><b>Faixa Etária: </b> '.$aluno['jfaixaetaria']."<br />".'<span class="glyphicon glyphicon-education" style="margin-right: 5px;"></span><b>Escolaridade: </b> '.$aluno['jescolaridade']."<br />" .'<span class="glyphicon glyphicon-heart" style="margin-right: 5px;"></span><b>Hobby: </b>'.$aluno['jhobby']."<br />".'<span class="glyphicon glyphicon-cutlery" style="margin-right: 5px;"></span> <b>Comida: </b>'.$aluno['jcomida']."<br />".'<span class="glyphicon glyphicon-music" style="margin-right: 5px;"></span><b>Estilo Musical: </b>'.$aluno['jmusica']."<br />" .'<span class="glyphicon glyphicon-sunglasses" style="margin-right: 5px;"></span> <b>Esporte: </b>'.$aluno['jesporte']."<br />".'<span class="glyphicon glyphicon-asterisk" style="margin-right: 5px;"></span> <b>Time de Futebol:</b> '.$aluno['jtime']."<br />".'<span class="glyphicon glyphicon-search" style="margin-right: 5px;"></span><b>Caracteristica:</b> '.$aluno['jcaracteristica']."<br />" .'<span class="glyphicon glyphicon-knight" style="margin-right: 5px;"></span><b>Animal favorito: </b>'.$aluno['janimal']."<br />".'<span class="glyphicon glyphicon-book" style="margin-right: 5px;"></span><b>Livro: </b>'.$aluno['jlivro']."<br />".'<span class="glyphicon glyphicon-cloud" style="margin-right: 5px;"></span><b>Sonho: </b>'.$aluno['jsonho']."<br /> <br />".'<input type="hidden" name="id_atualizacao" value="Id da linha a ser alterada"><input type="submit"  value="Selecionar esse jovem" class="btn btn-primary" name="selecionar"/><br /> <br /><br /></div></form>' ; 
    }


    } else {
        echo mysqli_errno($conexao) . ": " . mysqli_error($conexao) . "\n";
    }


    mysqli_close($conexao); 
?>
    </body>
    </html>
    
asked by anonymous 26.01.2018 / 16:32

1 answer

0

What I understood would be as follows:

You get the CPF and search, as you passed:

$string_sql = "SELECT * FROM mentor WHERE CPF='$cpf'";  

Then you generate your form here:

while ($aluno = mysqli_fetch_array($resultadojovens)){


        echo '<form id="formSelecionar" action="selecao.php" name="formSelecionar" method="post"><div  style="position: relative; float: left; margin: .5%; width: 25%; margin-left: 10%;"><div style="width:250px; height:250px;"><img style="width:70%" src=imagem/'.($aluno['jfoto']).'></div>'."<br />" .'<span class="glyphicon glyphicon-user" style="margin-right: 5px;"></span><b>Nome: </b>'.$aluno['jnome']."<br />".'<span class="glyphicon glyphicon-calendar" style="margin-right: 5px;"></span><b>Faixa Etária: </b> '.$aluno['jfaixaetaria']."<br />".'<span class="glyphicon glyphicon-education" style="margin-right: 5px;"></span><b>Escolaridade: </b> '.$aluno['jescolaridade']."<br />" .'<span class="glyphicon glyphicon-heart" style="margin-right: 5px;"></span><b>Hobby: </b>'.$aluno['jhobby']."<br />".'<span class="glyphicon glyphicon-cutlery" style="margin-right: 5px;"></span> <b>Comida: </b>'.$aluno['jcomida']."<br />".'<span class="glyphicon glyphicon-music" style="margin-right: 5px;"></span><b>Estilo Musical: </b>'.$aluno['jmusica']."<br />" .'<span class="glyphicon glyphicon-sunglasses" style="margin-right: 5px;"></span> <b>Esporte: </b>'.$aluno['jesporte']."<br />".'<span class="glyphicon glyphicon-asterisk" style="margin-right: 5px;"></span> <b>Time de Futebol:</b> '.$aluno['jtime']."<br />".'<span class="glyphicon glyphicon-search" style="margin-right: 5px;"></span><b>Caracteristica:</b> '.$aluno['jcaracteristica']."<br />" .'<span class="glyphicon glyphicon-knight" style="margin-right: 5px;"></span><b>Animal favorito: </b>'.$aluno['janimal']."<br />".'<span class="glyphicon glyphicon-book" style="margin-right: 5px;"></span><b>Livro: </b>'.$aluno['jlivro']."<br />".'<span class="glyphicon glyphicon-cloud" style="margin-right: 5px;"></span><b>Sonho: </b>'.$aluno['jsonho']."<br /> <br />".'<input type="hidden" name="id_atualizacao" value="Id da linha a ser alterada"><input type="submit"  value="Selecionar esse jovem" class="btn btn-primary" name="selecionar"/><br /> <br /><br /></div></form>' ; 
    }

But in my view it is wrong, because you are generating 1 form for each record.

The correct would be you open the form, and make the while only of the records, not 1 form for each. But if so, ok, WHEN: when generating the link in the registry, you pull the chave primária of the 2 records, because you need to know which record the user selected correct!?

Example (not specific to your code):

?>
<form>
  <tr>
    <th>Nome</th>
    <th>Ação</th> 
  </tr>
<?
while ($aluno = mysqli_fetch_array($resultadojovens)){
?>
  <tr>
    <td><?$aluno['nome']?></td>
    <td><a href="script.php?id="<?$aluno['id']?>>Selecionar</a></td> 
  </tr>
<?
}
?>
</form>
                                    
28.01.2018 / 02:06