php – How to upgrade my MySQL code to MySQLi?

Question:

I have code that uses mysql and I need to upgrade to mysqli .

I need to make two changes, being them in the .php pages that insert the data in the database and data table and also in the pages that display the data.

How to proceed?

Here is the code for the page that inserts data:

<body>
<?php
if(isset($_POST['enviar'])){
    $nome = mysql_real_escape_string(trim($_POST['nome']));
    $descricao = mysql_real_escape_string(trim($_POST['descricao']));
    $detalhes = mysql_real_escape_string(trim($_POST['detalhes']));

    $img = $_FILES['foto'];
    $name = $img['name'];
    $tmp = $img['tmp_name'];
    $size = $img['size'];
    $ext = end((explode('.', $name)));
    $pasta = '../img/produtos';
    $maxSize = 1024 * 1024 * 2;
    $permite = array('jpg','jpeg','png');

    if(empty($nome) && empty($descricao) && empty($name)){
        echo '<script>alert("Por favor, preencha o formulário de cadastro de produto corretamente.");</script>';
    }else if(empty($nome)){
        echo '<script>alert("Por favor, preencha o campo Nome.");</script>';
    }else if(empty($descricao)){
        echo '<script>alert("Por favor, preencha o campo Descrição.");</script>';
    }else if(empty($detalhes)){
        echo '<script>alert("Por favor, preencha o campo Detalhes.");</script>';
    }else if(empty($name)){
        echo '<script>alert("Por favor, selecione uma imagem.");</script>';
    }else if(!in_array($ext, $permite)){
        echo '<script>alert("A extensão da imagem selecionada não é suportada.");</script>';
    }else if($maxSize < $size){
        echo '<script>alert("A imagem selecionada é grande demais.");</script>';
    }else{
        $name = uniqid().'.'.$ext;
        $sql = mysql_query("INSERT INTO produtos (nome, descricao, detalhes, imagem) VALUES ('$nome', '$descricao', '$detalhes', '$name')") or die(mysql_error());
        if($sql){
            $upload = move_uploaded_file($tmp, $pasta.'/'.$name);
            if($upload){
                echo '<script>alert("Postagem salva com sucesso!");</script>';
            }else{
                echo '<script>alert("A postagem não pôde ser salva corretamente.");</script>';
            }
        }else{
                echo '<script>alert("Desculpe, ocorreu um erro.");</script>';
            }
    }
}
?>
<div id="sendform">
<h2 class="textos" style="width:100%; text-align:center; font-weight:700">Inserir Produto</h2>
<form action="" method="post" enctype="multipart/form-data">
    <a class="textos">Nome do produto:</a><br>
    <input type="text" id="nome" name="nome" maxlength="80"><br>
    <br><a class="textos">Descrição do produto:</a><br>
    <textarea type="text" id="descricao" name="descricao"></textarea>
    <br><a class="textos">Detalhes do produto:</a><br>
    <textarea type="text" id="detalhes" name="detalhes"></textarea>
    <br><a class="textos">Selecione uma imagem para o produto.:</a><br><input type="file" id="foto" name="foto"><br>
    <input type="submit" id="enviar" name="enviar" value="Enviar">
</form>
</div>
<div id="deletar_prod">
<h2 class="textos" style="width:100%; text-align:center; margin-top:50px; font-weight:700">Deletar Produto</h2>
<?php
    if (isset($_POST['apagar']) && $_POST['apagar'] == 'excluir'){
        $deleta = mysql_query("DELETE FROM produtos WHERE id = '$_POST[id]'");
        if ($deleta == '1'){
        echo '<h2 class="textos" style="width:100%; text-align:center;">Produto deletado com sucesso!</h2>';
        }else{ '<h2 class="textos" style="width:100%; text-align:center;">Erro ao deletar o produto.</h2>';
    }
}
?>

<?php
    $sql = "SELECT id, nome FROM produtos ORDER BY data DESC, id DESC";
    $resultado = mysql_query($sql)
    or die (mysql_error());
    if (mysql_num_rows($resultado) == 0)
    echo '<h2 class="textos" style="width:100%; text-align:center; margin-top:0px;">Nenhum registro de produto foi encontrado.</h2>';
?>
<form id="form1" name="form1" method="post" action="" enctype="multipart/form-data">
    <select name="id" id="id">
        <option value="-1">Selecione o produto que deseja deletar</option>
<?php
    while($linha=mysql_fetch_array($resultado)){
    $id = $linha[0];
    $nome = $linha[1];
?>
        <option value="<?php echo $id; ?>"><?php echo $nome; ?></option>
<?php
}
?>
    </select>
    <input type="hidden" name="apagar" value="excluir"/>
    <input class="botao" type="submit" name="excluir" id="excluir" value="Excluir"/>
</form>
</div>
</body>

Answer:

In most cases it's just changing everything that starts with mysql to mysqli 🙂 Of course that's not all. There are some parameter differences and some different functions.

Have you seen the documentation ? I advise you to stick with the procedural syntax as you are used to it and the change would be simpler. The syntax working with objects does not bring any advantage other than code organization. Even so, whether it really is an advantage is questionable.

<body>
<?php
if(isset($_POST['enviar'])){
    $nome = mysqli_real_escape_string($conexao, trim($_POST['nome']));
    $descricao = mysqli_real_escape_string($conexao, trim($_POST['descricao']));
    $detalhes = mysqli_real_escape_string($conexao, trim($_POST['detalhes']));
    
    $img = $_FILES['foto'];
    $name = $img['name'];
    $tmp = $img['tmp_name'];
    $size = $img['size'];
    $ext = end((explode('.', $name)));
    $pasta = '../img/produtos';
    $maxSize = 1024 * 1024 * 2;
    $permite = array('jpg','jpeg','png');
    
    if(empty($nome) && empty($descricao) && empty($name)){
        echo '<script>alert("Por favor, preencha o formulário de cadastro de produto corretamente.");</script>';
    }else if(empty($nome)){
        echo '<script>alert("Por favor, preencha o campo Nome.");</script>';
    }else if(empty($descricao)){
        echo '<script>alert("Por favor, preencha o campo Descrição.");</script>';
    }else if(empty($detalhes)){
        echo '<script>alert("Por favor, preencha o campo Detalhes.");</script>';
    }else if(empty($name)){
        echo '<script>alert("Por favor, selecione uma imagem.");</script>';
    }else if(!in_array($ext, $permite)){
        echo '<script>alert("A extensão da imagem selecionada não é suportada.");</script>';
    }else if($maxSize < $size){
        echo '<script>alert("A imagem selecionada é grande demais.");</script>';
    }else{
        $name = uniqid().'.'.$ext;
        $sql = mysqli_query($conexao, "INSERT INTO produtos (nome, descricao, detalhes, imagem) VALUES ('$nome', '$descricao', '$detalhes', '$name')") or die(mysqli_error($conexao));
        if($sql){
            $upload = move_uploaded_file($tmp, $pasta.'/'.$name);
            if($upload){
                echo '<script>alert("Postagem salva com sucesso!");</script>';
            }else{
                echo '<script>alert("A postagem não pôde ser salva corretamente.");</script>';
            }
        }else{
                echo '<script>alert("Desculpe, ocorreu um erro.");</script>';
            }
    }
}
?>
<div id="sendform">
<h2 class="textos" style="width:100%; text-align:center; font-weight:700">Inserir Produto</h2>
<form action="" method="post" enctype="multipart/form-data">
    <a class="textos">Nome do produto:</a><br>
    <input type="text" id="nome" name="nome" maxlength="80"><br>
    <br><a class="textos">Descrição do produto:</a><br>
    <textarea type="text" id="descricao" name="descricao"></textarea>
    <br><a class="textos">Detalhes do produto:</a><br>
    <textarea type="text" id="detalhes" name="detalhes"></textarea>
    <br><a class="textos">Selecione uma imagem para o produto.:</a><br><input type="file" id="foto" name="foto"><br>
    <input type="submit" id="enviar" name="enviar" value="Enviar">
</form>
</div>
<div id="deletar_prod">
<h2 class="textos" style="width:100%; text-align:center; margin-top:50px; font-weight:700">Deletar Produto</h2>
<?php
    if (isset($_POST['apagar']) && $_POST['apagar'] == 'excluir'){
        $deleta = mysqli_query($conexao, "DELETE FROM produtos WHERE id = '$_POST[id]'");
        if ($deleta == '1'){
        echo '<h2 class="textos" style="width:100%; text-align:center;">Produto deletado com sucesso!</h2>';
        }else{ '<h2 class="textos" style="width:100%; text-align:center;">Erro ao deletar o produto.</h2>';
    }
}
?>

<?php
    $sql = "SELECT id, nome FROM produtos ORDER BY data DESC, id DESC";
    $resultado = mysqli_query($conexao, $sql)
    or die (mysqli_error($conexao));
    if (mysqli_num_rows($resultado) == 0)
    echo '<h2 class="textos" style="width:100%; text-align:center; margin-top:0px;">Nenhum registro de produto foi encontrado.</h2>';
?>
<form id="form1" name="form1" method="post" action="" enctype="multipart/form-data">
    <select name="id" id="id">
        <option value="-1">Selecione o produto que deseja deletar</option>
<?php
    while($linha=mysqli_fetch_array($resultado)){
    $id = $linha[0];
    $nome = $linha[1];
?>
        <option value="<?php echo $id; ?>"><?php echo $nome; ?></option>
<?php
}
?>
    </select>
    <input type="hidden" name="apagar" value="excluir"/>
    <input class="botao" type="submit" name="excluir" id="excluir" value="Excluir"/>
</form>
</div>
</body>

I put it on GitHub for future reference .

Note that mysqli functions have a parameter for information on which connection is doing the work.

I haven't tested it but it shouldn't be a problem. Anyway, I advise you to check it out first, and especially study the subject before venturing into it. Then you can make more radical changes, perhaps taking advantage of new features, but it's best to start as simply as possible.

Scroll to Top