I have this code, how to update in sql?

Asked

Viewed 60 times

-2

have tried


$sql = " UPDATE biblioteca SET nome_usuario WHERE nome_usuario IN ( SELECT nome_usuario FROM ( SELECT  nome_usuario, COUNT(id_usuario) FROM orders WHERE nome_usuario )"



$sql = "UPDATE usuario(nome_usuario) VALUES ('$name') WHERE nome_usuario=".$_REQUEST["nome_usuario"];




$sql = "UPDATE usuario(nome_usuario) VALUES ('$name') WHERE nome_usuario= '$name'";



<?php
    switch($_REQUEST["acao"]){
        case "editar":

            $sql = " UPDATE biblioteca SET nome_usuario WHERE nome_usuario IN ( SELECT nome_usuario FROM ( SELECT  nome_usuario, COUNT(id_usuario) FROM orders WHERE nome_usuario )"

             // SELECT * FROM biblioteca 
             // WHERE id_biblioteca=".$_REQUEST["id_biblioteca"];

            # $sql = "UPDATE usuario(nome_usuario) VALUES ('$name') WHERE nome_usuario= '$name'";
            # $sql = "UPDATE usuario(nome_usuario) VALUES ('$name') WHERE nome_usuario=".$_REQUEST["nome_usuario"];
            $res = $conn->query($sql);
            if($res==true){
                print "<br><div class='alert alert-success'>Usuário atualizado com sucesso!</div>";
            }else{
                print "<div class='alert alert-danger'>Não é possível atualizar o usuário.</div>";
            }
        break;
    }
?>


  • 2

    SET nome_usuario WHERE and where is the value for the "username"? should be something like SET nome_usuario='alguma coisa' WHERE. I don’t think you understand how the update, see documentation here: https://dev.mysql.com/doc/refman/8.0/en/update.html

  • With all due respect. I recommend you take a basic sql course, there are many free ones on the internet that are good. Has a softblue, very good (moderators do not know if I can post, if it is forbidden edit please)

1 answer

0


From what I understand you want to update a user’s name, if that’s what I believe the code below can solve:

<?php

$id = $_POST['id'];
$name = $_POST['name'];


$sql = "UPDATE users
SET name = '{$name}'
WHERE id = {$id}";

$res = $conn->query($sql);

Now if you want to update the user who is registered in the library table, you have to make a relationship between the library table and the users table.

If you want to register a user in the table library it would be ideal to have a column called user_id (foreign key) that refers to the id of the table of users.

  • Now if you want to update the user who is registered in the library table, you have to make a relationship between the library table and the users table. How do I do?

  • Search for "creating a foreign key in Mysql", you will find several tutorials that will help you, it’s quite simple, on Youtube itself you find courses on how to do this.

Browser other questions tagged

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