Doubt - UPDATE MYSQL with PHP

Asked

Viewed 110 times

0

I need to update in mysql database but I can’t.

Basically I’m following the following steps:

  • taken bank variables that are similar to a given CPF and display 2 people
  • The user will click a button to select 1 person
  • As soon as the user clicks on this button I need to do 1 update in 1 table field.

Has as?

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>
<script src="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>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.js"></script>
<script src="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>
  • Post the code you’re using

  • Posted........

  • It is important to explain the exact problem in a more objective way. It would help a lot if you paste the last lines of the history (log file) of PHP errors to see what error actually occurs, and describe what loops and conditions the script is actually entering.

  • Bacco, no error... I have a lot of information that comes from the bank, and I give the option of the user to choose a... when the user clicks on the button I wanted to update the table. I was wondering if you had any way to do that...

1 answer

0

From what I understand it would be as follows:

You take the number and search, as 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 the way I see it is wrong, because you are generating 1 form for each record.

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

Example (not specific with 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>

Moving on, as long as your form is ok, pulling the records and your primary key, you will use this key to make your script that will filter and make the update.

In case I don’t really understand your doubt, comment.

  • Hello! ??

  • @Carolinaperretticabral I used examples, have to do according to their variables, etc. Edit the answer, and post the code after the change to see.

  • I switched to my variables, only he does not recognize the "While", only appears 1 student and does not appear in table...

Browser other questions tagged

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