I can’t change records from a database table

Asked

Viewed 808 times

2

The thing is, I need to make sure that my site can change login data, registered in the database. I did the codes, but when I click to change, nothing happens... The files I’m using:

alteraadm.php

$conexao = @mysql_connect("localhost","root",""); 

if (!$conexao)

die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error());

$banco = mysql_select_db("vidal",$conexao); 

if (!$banco)
die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysql_error());

$sql = mysql_query("SELECT * FROM loginadm");

$i = 1;

$loop = 3;

while ($list = mysql_fetch_array($sql)) {

    if ($i < $loop){

            echo
            '<tr align="justify" valign="middle" bgcolor="#FFFFFF"> 
            <td><a href="" >Usuario:'.$list['usuarioadm'].'</a><br></td>

            <form action="editar.php"><input type="submit" value="alterar" name="alterar"></form>  
            </tr>
        <tr>
            ';
    $i=0;
    }
    $i++;
}
?>  

edit.php

<form method="post" action="update.php" enctype="multipart/form-data">
Codigo:<input type="text" name="codigologin" size="30"><br>
Usuario:<input type="text" name="usuarioadm" size="30"><br>
Senha:<input type="text" name="senhaadm" size="30"><br><br><br>
 <input type="submit" name="concluir" value="concluir" class="botao2">
 </form>

update php.

$conexao = @mysql_connect("localhost","root",""); 

if (!$conexao)
die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error());

$banco = mysql_select_db("vidal",$conexao); 

if (!$banco)
die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysql_error());

$codigologin= $_POST ["codigologin"];

$usuarioadm= $_POST["usuarioadm"];

$senhaadm= $_POST["senhaadm"];

$query = "UPDATE INTO `loginadm` ( `codigologin` , `usuarioadm` , `senhaadm`) VALUES ('$codigologin', '$usuarioadm', '$senhaadm')";

mysql_query($query,$conexao);

echo "Alteracao Realizada com sucesso!!!";

?>

If anyone helps me find the mistake, I’d appreciate it.

1 answer

3

First, don’t use the mysql_* functions, they’ve already been removed from php7, arrobas are more disturbing than they help they hide the errors, an error message is better than 'nothing happens''.

The problem is the syntax of your update

$query = "UPDATE INTO `loginadm` 

If it is an update it should be like the code below, never forget that most of the updates have a WHERE to specify which records should be changed, otherwise all rows in that table will have the same values (in some cases this may be purposeful), backup, restores or rollbacks will be required.

The correct syntax is:

UPDATE tabela SET 
   campo1 = valor1,
   campo2 = valor2,
   campo3 = valor3
WHERE id = valor_id

How to identify errors in the query:

To be able to identify syntax and other errors in database operations, use the function that returns the error, in this case it is the mysql_error() in carrying out the consultation carried out by mysql_query().

mysql_query($query,$conexao) or die(mysql_error());

Remember to remove the arrobas and handle errors instead of hidden.

Recommended reading:

Why should we not use mysql type functions_*?

Why do they say using @arroba to suppress errors is a bad practice?

  • I got it!! Thank you very much.

  • @Luanwesley was just the update syntax?

Browser other questions tagged

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