#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6

    upload script ...mysql to mysqli??


    i was trying to make an upload script work , i was wondering if someone can help me turn the mysql code in the script to mysqli code ??

    i know from some discussions that this code is vulnerable ...

    but i was planning to use it for some learning purposes only ....

    config2.php

    PHP Code:
    <?php
    $con 
    mysqli_connect("localhost","root","root","dbtuts");

    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }
    ?>
    index.php

    PHP Code:
    <?php
    include_once 'config2.php';
    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>File Uploading With PHP and MySql</title>
    <link rel="stylesheet" href="style.css" type="text/css" />
    </head>
    <body>
    <div id="header">
    <label>File Uploading With PHP and MySql</label>
    </div>
    <div id="body">
        <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file" />
        <button type="submit" name="btn-upload">upload</button>
        </form>
        <br /><br />
        <?php
        
    if(isset($_GET['success']))
        {
            
    ?>
            <label>File Uploaded Successfully...  <a href="view.php">click here to view file.</a></label>
            <?php
        
    }
        else if(isset(
    $_GET['fail']))
        {
            
    ?>
            <label>Problem While File Uploading !</label>
            <?php
        
    }
        else
        {
            
    ?>
            <label>Try to upload any files(PDF, DOC, EXE, VIDEO, MP3, ZIP,etc...)</label>
            <?php
        
    }
        
    ?>
    </div>
    <div id="footer">
    <label>By <a href="http://cleartuts.blogspot.com">cleartuts.blogspot.com</a></label>
    </div>
    </body>
    </html>
    upload.php

    PHP Code:
    <?php
    include_once 'config2.php';
    if(isset(
    $_POST['btn-upload']))
    {    
         
        
    $file rand(1000,100000)."-".$_FILES['file']['name'];
        
    $file_loc $_FILES['file']['tmp_name'];
        
    $file_size $_FILES['file']['size'];
        
    $file_type $_FILES['file']['type'];
        
    $folder="uploads/";
        
        
    // new file size in KB
        
    $new_size $file_size/1024;  
        
    // new file size in KB
        
        // make file name in lower case
        
    $new_file_name strtolower($file);
        
    // make file name in lower case
        
        
    $final_file=str_replace(' ','-',$new_file_name);
        
        if(
    move_uploaded_file($file_loc,$folder.$final_file))
        {
            
    $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
            
    mysqli_query($sql);
            
    ?>
            <script>
            alert('successfully uploaded');
            window.location.href='index.php?success';
            </script>
            <?php
        
    }
        else
        {
            
    ?>
            <script>
            alert('error while uploading file');
            window.location.href='index.php?fail';
            </script>
            <?php
        
    }
    }
    ?>
    view.php

    PHP Code:
    <?php
    include_once 'config2.php';
    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>File Uploading With PHP and MySql</title>
    <link rel="stylesheet" href="style.css" type="text/css" />
    </head>
    <body>
    <div id="header">
    <label>File Uploading With PHP and MySql</label>
    </div>
    <div id="body">
        <table width="80%" border="1">
        <tr>
        <th colspan="4">your uploads...<label><a href="index.php">upload new files...</a></label></th>
        </tr>
        <tr>
        <td>File Name</td>
        <td>File Type</td>
        <td>File Size(KB)</td>
        <td>View</td>
        </tr>
        <?php
        $sql
    ="SELECT * FROM tbl_uploads";
        
    $result_set=mysqli_query($sql);
        while(
    $row=mysqli_fetch_array(($result_set))
        {
            
    ?>
            <tr>
            <td><?php echo $row['file'?></td>
            <td><?php echo $row['type'?></td>
            <td><?php echo $row['size'?></td>
            <td><a href="uploads/<?php echo $row['file'?>" target="_blank">view file</a></td>
            </tr>
            <?php
        
    }
        
    ?>
        </table>
        
    </div>
    </body>
    </html>
    Code:
    CREATE DATABASE `dbtuts` ;
    CREATE TABLE `dbtuts`.`tbl_uploads` (
    `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `file` VARCHAR( 100 ) NOT NULL ,
    `type` VARCHAR( 10 ) NOT NULL ,
    `size` INT NOT NULL
    ) ENGINE = MYISAM ;
    can anyone help me convert this mysql code to mysqli ??
    Last edited by digimon; August 27th, 2016 at 11:53 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    143
    Rep Power
    66
    You already have a thread on this forum converting a script from mysql to mysqli - file upload script error ,please help ?

    In that thread, all you were doing is repeatedly dumping all the code and the symptom or error you were getting on the forum, and expected someone to tell you exactly what to do at each step to make the code work. That method isn't going to teach you anything, since there is no 'you' involved in the process, just us, and I don't think the forum's members here are going to go around and around with you a second time telling you what each symptom or error means and provide the same information and links to the php documentation that has already been given.

    It doesn't matter what the script is actually using the database for, converting from one database api to another requires that you know what the existing api (mysql) statements are doing, then learn how to use the equivalent statements for a different api (PDO has been suggested as the best replacement to use), then actually make an attempt to convert each occurrence in the script, and learn from what works and what doesn't how to actually use the new api statements.

    So, "can anyone help me convert this mysql code to mysqli ??" Yes we can, if it's you making an attempt at doing the work yourself, and in the process learning how to do something new. If it's just you following along or copying some code that's given to you, then no we cannot help you.

    Comments on this post

    • digimon agrees
    • Will-O-The-Wisp agrees
    • s-p-n agrees : Tough love is often the most genuine help you can offer!
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    thanks for taking your time to reply ...

    sorry about the similar post ...

    i changed few lines of mysql code to mysqli , by reading a lot of stuff available on the internet ...

    and most of it only looks like this ...

    mysql_affected_rows -> mysqli_affected_rows($link)
    mysql_close -> mysqli_close($link)
    mysql_data_seek -> mysqli_data_seek( $result, $offset)
    mysql_errno -> mysqli_errno( $link)
    mysql_error -> mysqli_error( $link)
    mysql_fetch_array -> mysqli_fetch_array( $result, $type)
    mysql_fetch_assoc -> mysqli_fetch_assoc( $result)
    mysql_fetch_lengths -> mysqli_fetch_lengths( $result )
    mysql_fetch_object -> mysqli_fetch_object( $result, $class, $params)
    mysql_fetch_row -> mysqli_fetch_row( $result)
    mysql_field_seek -> mysqli_field_seek( $result, $number)
    mysql_free_result -> mysqli_free_result(result)
    mysql_get_client_info -> mysqli_get_client_info( $link)
    mysql_get_host_info -> mysqli_get_host_info( $link)
    mysql_get_proto_info -> mysqli_get_proto_info( $link)
    mysql_get_server_info -> mysqli_get_server_info( $link)
    mysql_info -> mysqli_info( $link)
    mysql_insert_id -> mysqli_insert_id( $link)
    mysql_num_rows -> mysqli_num_rows( $result)
    mysql_ping -> mysqli_ping( $link)
    mysql_query -> mysqli_query( $link, $query)
    mysql_real_escape_string -> mysqli_real_escape_string( $link)
    mysql_select_db - > mysqli_select_db( $link, $database)
    mysql_set_charset -> mysqli_set_charset( $link, $charset)
    mysql_stat -> mysqli_stat( $link)
    mysql_thread_id -> mysqli_thread_id( $link)
    i followed instructions like these and am still getting errors like this ...

    mysqli_query() expects at least 2 parameters ...

    i thought i would try the easy method first , and change the exisiting code to mysqli ...

    after this i might try the harder methods like trying to code an "upload.php" mysqli script from scratch ...

    i really need to start somewhere ...

    i thought changing a few lines of mysql code to mysqli would make it work ...

    let me try to modify this code once again as much as i possibly can with mysqli statements ...

    if i have errors from an attempt like that, can i post it in this thread ??
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    i just changed few lines of code from the original "upload.php" to this ...

    PHP Code:
    $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
            
    mysqli_query($conn $sql);
            
    ?> 
    and changed few lines of code from "view.php" to this ...

    PHP Code:
    $sql="SELECT * FROM tbl_uploads";
        
    $result_set=mysqli_query($conn $sql);
        while(
    $row=mysqli_fetch_array($result_set))
        { 

    the error messages has gone now ...





    but it is not showing any details ...
    Last edited by digimon; August 27th, 2016 at 04:12 PM.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    109
    Rep Power
    6
    PHP Code:
    <?php
    include_once 'config5.php';
    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>File Uploading With PHP and MySql</title>
    <link rel="stylesheet" href="style.css" type="text/css" />
    </head>
    <body>
    <div id="header">
    <label>File Uploading With PHP and MySql</label>
    </div>
    <div id="body">
        <table width="80%" border="1">
        <tr>
        <th colspan="4">your uploads...<label><a href="index.php">upload new files...</a></label></th>
        </tr>
        <tr>
        <td>File Name</td>
        <td>File Type</td>
        <td>File Size(KB)</td>
        <td>View</td>
        </tr>
        <?php
        $sql
    ="SELECT * FROM tbl_uploads";
        
    $result mysqli_query($conn$sql);
        while(
    $row mysqli_fetch_assoc($result))
        {
            
    ?>
            <tr>
            <td><?php echo $row['file'?></td>
            <td><?php echo $row['type'?></td>
            <td><?php echo $row['size'?></td>
            <td><a href="uploads/<?php echo $row['file'?>" target="_blank">view file</a></td>
            </tr>
            <?php
        
    }
        
    ?>
        </table>
        
    </div>
    </body>
    </html>
    this worked ...

    Comments on this post

    • s-p-n agrees : So happy with your personal progress, please keep learning and don't be afraid to try things!
  10. #6
  11. Not An Expert
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2015
    Posts
    404
    Rep Power
    5
    Glad to hear that your issues were resolved - well done and thank you for sharing your solution!

    I'll be closing this thread since everything seems resolved. I do agree with DSmabismad's remarks above, though, and I would keep them in mind for future thread posts.

IMN logo majestic logo threadwatch logo seochat tools logo