Jump to content

Flaviaac

Members
  • Posts

    3
  • Joined

  • Last visited

Posts posted by Flaviaac

  1. Hi guys, 

    I have a odd problem here.

    I'm a beginner and I have no idea what it can be.. 😑

    ps. English is not my first language, so I hope you can understand... ☺️

     

    In dropdown field below, I put in value property the ID column, but when it registers in the database, it saves as string type and not as integer.

    <?php
    $sql = "SELECT id, sigla FROM local ORDER BY sigla ASC";
    $result = $conn->query($sql);
    ?>
    .
    .
    .
    <select name="local" class="form-control">
      <option selected>Selecione...</option>
      <?php
      while($row = $result->fetch_assoc()){                                  
        echo "<option value=".$row['id'].">".$row['sigla']."</option>";                                  
      }                              
      ?>                            
    </select>
    .
    .
    .

    I used a var_dump() on ID to know what would happend, and it shows as string type.

    <pre>
    <?php
    $sql = "SELECT id, sigla FROM local ORDER BY sigla ASC";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();  
    var_dump($row['id']);
    ?>
    </pre>

    Result:

      string(1) "5"

     

     

    This is the database:

    -- phpMyAdmin SQL Dump
    -- version 4.8.3
    -- https://www.phpmyadmin.net/
    
    
    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET AUTOCOMMIT = 0;
    START TRANSACTION;
    SET time_zone = "+00:00";
    
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8mb4 */;
    
    --
    -- Banco de dados: `arq`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Estrutura para tabela `local`
    --
    
    CREATE TABLE `local` (
      `id` int(11) NOT NULL,
      `SIGLA` mediumtext NOT NULL,
      `LOCAL` mediumtext NOT NULL,
      `TELEFONE` mediumtext NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    --
    -- Fazendo dump de dados para tabela `local`
    --
    
    INSERT INTO `local` (`id`, `SIGLA`, `LOCAL`, `TELEFONE`) VALUES
    (1, 'AAC', 'AAC', '2222-2222'),
    (2, 'DSV', 'DSV', '1111-1111'),
    (3, 'SCA', 'SCA', '3333-3333');
    
    --
    -- Índices de tabelas apagadas
    --
    
    --
    -- Índices de tabela `local`
    --
    ALTER TABLE `local`
      ADD PRIMARY KEY (`id`);
    
    --
    -- AUTO_INCREMENT de tabelas apagadas
    --
    
    --
    -- AUTO_INCREMENT de tabela `local`
    --
    ALTER TABLE `local`
      MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
    COMMIT;
    
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

    Thank you so much!!

     

     

     

  2. Hi guys,

    I'm beginning to learn PHP now, and i have too much doubts yet, so i would like some help. 😃

    I want to delete a registry from table when i press the button "Apagar" like show in the picture below:

    produtos.png.7defcef61a7206b0a43623710e40c655.png

     

    When I press the button, it shows a message saying it was successfully deleted, but in fact it was not deleted in the DB.

    The message: Notice: Undefined index: id in .../teste_tabela3/deletar_produto.php on line 11

     

    What did i do wrong in my code?

     

    code from "deletar_produto.php" file:

    <?php
        include_once 'cabeçalho.php';
        include_once 'conexao.php';          
            
        $id = $_POST['id']; 
        if(isset($_POST['apagar'])){ 
          $sql = "DELETE FROM produtos WHERE id='$id'";
          $result = mysqli_query($conn, $sql);
          if($conn->query($sql) === TRUE)  
            echo "<br/><br/><span>O registro foi deletado com sucesso...!!</span>"; 
        }else{
          echo "<p>Não foi possivel apagar o registro....!!</p>";
        }
        $conn->close();    
     ?> 

    And this is code of "listar_produtos.php" file:

    <?php
        include_once 'cabeçalho.php';
        include_once 'conexao.php';
    ?>
    
      <div class="d-flex mx-2 my-2">
        <div class="mr-auto p-2">                                            
          <h2 class="display-4 titulo">Lista de Produtos</h2>                    
        </div>                      
      </div> 
    
      <?php
        $sql = "SELECT id, produto, valor FROM produtos";
        $result = $conn->query($sql);                    
      ?>
    
      <!--------------------------------------------TABELA-------------------------------------------->
    
      <div class="table-responsive">
        <table class="table table-bordered table-hover table-striped">
          <thead>
            <tr>
              <th>ID</th>
              <th>Produto</th>
              <th>Valor</th>
              <th>Ações</th>                   
            </tr>
          </thead>                      
          <?php
          if ($result->num_rows > 0) {
            echo "<tbody>"; 
            while($row = $result->fetch_assoc()) {           
              echo "<tr>";  
                echo "<td>" .$row['id'] ."</td>";  
                echo "<td>" .$row['produto']. "</td>";  
                echo "<td>" .$row['valor']. "</td>"; 
                echo "<td>";                                       
                  echo "<form action='deletar_produto.php' method='post'>";
                    echo "<button type='submit' class='btn btn-outline-primary' name='editar'>Editar</button> &nbsp;";
                    echo "<button type='submit' class='btn btn-outline-primary' name='apagar'>Apagar</button>"; 
                  echo "</form>";                                                                                                                 
                echo "</td>";
              echo "</tr>";    
            }                      
          } else {
            echo "0 results";
          }
          ?>                             
          </tbody>
        </table> 
    
      <?php
      $conn->close();
      include_once 'rodape.php';
      ?>

     

    Thank you so much!!

     

     

×
×
  • Create New...