Update command does not update table

Asked

Viewed 354 times

1

I’m having trouble with my CRUD. My code searches in BD and returns me the data on the screen correctly, the problem that when I saved, it does not update the BD and also does not display any error message.

Follow the code of the page of displays the data:

<?php

                   ini_set( 'display_errors', true );
                    error_reporting( E_ALL );
                    $conexao = mysql_connect("localhost","root","usbw");
                    if (!$conexao) die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error()); 
                    mysql_select_db('sistemas para internet', $conexao);
                    $cod_time = $_GET["cod_time"];
                    $sql = "SELECT * FROM time where cod_time=$cod_time";
                    $q = mysql_query($sql);
                    while ($resultado = mysql_fetch_array($q))
    {
        $logo           = $resultado["logo"];
        $nome           = $resultado["nome"];
        $estadio            = $resultado["estadio"];
        $presidente             = $resultado["presidente"];
        $endereco           = $resultado["endereco"];
        $cod_cidade             = $resultado["cod_cidade"];
        $telefone           = $resultado["telefone"];
        $email          = $resultado["email"];
        $site           = $resultado["site"];
            }


?>

<!DOCTYPE html>
<html lang="pt">
<head>
    <meta charset="utf-8">
    <link   href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <script src="bootstrap/js/bootstrap.min.js"></script>
</head>

<body>
<div class="span10 offset1">
                        <div class="row">
                            <h3>Atualiza Cadastro do Time</h3>
                        </div>             
                        <form class="form-horizontal" action="recebeupdatetime.php" method="post" enctype="multipart/form-data">
            <input type="hidden" name="txtid" value="<?=$cod_time;?>">
                    <div class="controls">                   
                Logo:<input type="file" name="logo" id="logo" accept="image/*" required="1" value="<?=$logo;?>"><br>
                </div>
                 <div class="controls">
                Nome do Time:<input type="text" name="nome"  maxlength="40" size="30" required="1" pattern="[A-Z a-z]+$" placeholder="Nome do Time" value="<?=$nome;?>"><br>
                </div>
                 <div class="controls">
                Endere&ccedil;o:<input type="text" name="end" maxlength="40" size="30" required="1" placeholder="Endere&ccedil;o" value="<?=$endereco;?>"><br>
                 </div>
                 <div class="controls">
                Telefone:
                <input type="tel" required="required" maxlength="15" name="phone" pattern="\([0-9]{2}\) [0-9]{4,6}-[0-9]{3,4}$" required="1" value="<?=$telefone;?>" placeholder="Telefone"/><br>
                </div>
                 <div class="controls">
                Presidente do Time:<input type="text" name="pres"  maxlength="40" size="30" required="1" pattern="[A-Z a-z]+$" value="<?=$presidente;?>" placeholder="Presidente do Time"><br>
                </div>
                 <div class="controls">
                Site:<input type="text" name="site" maxlength="40" size="30" required="1" placeholder="Site" value="<?=$site;?>"><br>
                    </div>
                     <div class="controls">
                Email:<input type="email" required="required" class="input-text" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" value="<?=$email;?>" placeholder="E-mail" /><br>
                </div>
                 <div class="controls">
                Estadio:<input type="text" name="est"  maxlength="40" size="30" required="1" pattern="[A-Z a-z]+$" value="<?=$estadio;?>" placeholder="Nome do Estadio"><br>
                </div>
                 <div class="controls">
                Cidade:<SELECT NAME="cidade"  required="1">
                <option>Selecione...</option>
                <?php
                $conexao = mysql_connect("localhost","root","usbw");
                if (!$conexao) die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error()); 
                mysql_select_db('sistemas para internet', $conexao);
                $query1 = "SELECT * FROM cidade ORDER BY cod_cidade, nomecid ASC";
                $q1 = mysql_query($query1);
                while($dados = mysql_fetch_array($q1))
                {   
                ?>
                 <option value="<?=$dados['cod_cidade'] ?>">
                <?=$dados['nomecid'] ?>
                </option>
                <?php
                    }
                    ?>

                    </SELECT><br><br>
                     </div>
                    <div class="form-actions">                  
                <input type="submit" name="btnOK" class="btn btn-success" value="Atualizar" >
                <a  class="btn btn-danger" href="menutime.php">Voltar</a>
                </div>

                </form>
                </div>


</body>

</html>

Page where you send the data

$cod_time = $_GET["cod_time"];

$logo = $_POST["logo"]; $name = $_POST["name"]; $estadio = $_POST["estadio"]; $presidente = $_POST["presidente"]; $endereco = $_POST["address"]; $cod_city = $_POST["cod_city"]; $phone = $_POST["phone"]; $email = $_POST["email"]; $site = $_POST["website"];

ini_set( 'display_errors', true ); error_reporting( E_ALL );

$connection = mysql_connect("localhost","root","usbw"); if (!$connected) die ("Connection error with localhost, the following error occurred -> ".mysql_error()); mysql_select_db('internet systems', $connection);

$res= mysql_query ("update time set logo='$logo',name='$name',stadium='$stadium',president='$president',address='$address',cod_city='$cod_town',phone='$phone',email='$email',site='$site' Where cod_time=$cod_time");

if($res)ģ // echo "Data updated successfully." ; }These{
echo "Failed to update data."; } ?>

2 answers

0

In the mysql_query you are using variables inside the text string, you have to send them as such:

mysql_query ("update time set logo='".$logo."',nome='".$nome."',estadio='".$estadio."',presidente='".$presidente."',endereco='".$endereco."',cod_cidade='".$cod_cidade."',telefone='".$telefone."',email='".$email."',site='".$site."' where cod_time=".$cod_time");


<?php

               ini_set( 'display_errors', true );
                error_reporting( E_ALL );
                $conexao = mysql_connect("localhost","root","usbw");
                if (!$conexao) die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error()); 
                mysql_select_db('Time de Futebol', $conexao);
                $cod_time = $_GET["cod_time"];
                $sql = "SELECT * FROM time where cod_time=$cod_time";
                $q = mysql_query($sql);
                while ($resultado = mysql_fetch_array($q))
{
    $logo           = $resultado["logo"];
    $nome           = $resultado["nome"];
    $estadio            = $resultado["estadio"];
    $presidente             = $resultado["presidente"];
    $endereco           = $resultado["endereco"];
    $cod_cidade             = $resultado["cod_cidade"];
    $telefone           = $resultado["telefone"];
    $email          = $resultado["email"];
    $site           = $resultado["site"];
        }

    mysql_query ("update time set logo='".$logo."',nome='".$nome."',estadio='".$estadio."',presidente='".$presidente."',endereco='".$endereco."',cod_cidade='".$cod_cidade."',telefone='".$telefone."',email='".$email."',site='".$site."' where cod_time=".$cod_time");

?>
  • Have extra quotes at the end of the query.

  • @Pedro Luzio I, unfortunately at the moment I have no way to test because I am at work, I will only be able to test the night. Thanks

  • And it’s got nothing to do with it, how it’s supposed to work.

  • @Jorgeb. Thank you I will pay more attention

  • @Malfus is not at the end of your query is in this answer.

  • It has nothing to do with why? I didn’t understand

  • @Pedroluzio as the Malfus has works. Only they are doing an update with the same data that already has in the table. So do not notice anything.

Show 2 more comments

0

<?php //PHP para ALTERAR OS DADOS NA TABELA

ini_set( 'display_errors', true );
error_reporting( E_ALL );

$conexao = mysqli_connect("localhost","root","usbw");
if (!$conexao) die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error()); 
mysql_select_db('Time de Futebol', $conexao);

$cod_time = $_GET["cod_time"];

$logo           = $_POST["logo"];
$nome           = $_POST["nome"];
$estadio        = $_POST["estadio"];
$presidente     = $_POST["presidente"];
$endereco       = $_POST["endereco"];
$cod_cidade     = $_POST["cod_cidade"];
$telefone       = $_POST["telefone"];
$email          = $_POST["email"];
$site           = $_POST["site"];


mysql_query ("update time set logo='$logo',nome='$nome',estadio='$estadio',presidente='$presidente',endereco='$endereco',cod_cidade='$cod_cidade',telefone='$telefone',email='$email',site='$site' where cod_time=$cod_time");
?>
  • 1

    Thank you, as soon as I get home I’ll test you and I’ll get back to you.

  • Oops, it didn’t work. I must be doing something wrong. I put your code above my html and msm so nothing changed, then created a page only with php and msm so nothing happened

  • Notice: Undefined index: cod_time in, this appears in all fields

  • cod_time must have an index (index) in the mysql database

  • If you want to chat me or something I’ll help you. Otherwise it gets complicated without talking

  • I can join the chat now

  • You gotta open one I can’t get on my phone

  • I took Ashkela msm room from before you had created for me, as I am young I do not know yet create

  • I edited my question by putting the full code but it’s hard to format

Show 4 more comments

Browser other questions tagged

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