Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    275
    Thanks
    39
    Thanked 0 Times in 0 Posts

    mysqli insert query not inserting

    Hi!

    I'm following an example I've seen on numerous website and have just tailored it for my needs.

    In the first file I have (see the code below). It works as intended.

    PHP Code:
    <html>
        <head>
            <title>Upload File To MySQL Database</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <style type="text/css">
                .myc {
                    font-size: 12px;
                    border: 1px solid #000000;
                }
            </style>
        </head>
       <body>
            <form action="upload.php" 
                  enctype="multipart/form-data" name="uploadform" method="post" >

    <?php

        $mysqli_db 
    = new mysqli("localhost""*****""**********""ftsc");
        
    $query "SELECT * FROM `ftsc`.`teams`";
        
    $query_result $mysqli_db->query($query);
        
        echo 
    "<select name='team_id'> ";

        while (
    $rows $query_result->fetch_assoc()) {
            echo 
    "<option value='" $rows['team_id'] . "'>" $rows['age_range'] . " " $rows['sex'] . " " $rows['team_name'] . "</option>";
        }

        
    $mysqli_db->close();

        echo  
    "</select>\n";
        echo 
    "<br/>\n";

    ?>              

                  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
                <tr> 
                   <td>
                    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
                    <input name="userfile" type="file" id="userfile"/>
                    <input name="upload" type="submit" class="myc" id="upload" value="Upload"/>
                   </td>
                </tr>
              </table>
            </form>
        </body>
    </html>
    Then the actual upload and insert code (see code below). I always get "Error preparing Statement" which is in the if statement of the connect for the insert query. I wish it where a bit more vocal on what the problem is. The good news is that the file gets uploaded to the directory I told it to go to. And before you ask, yes the table already exists in the database.

    This is my first time attempting doing this so I'm not sure what pitfalls to watch out for. I'm not 100% keen on the object oriented calls for mysqli, I'm far more comfortable using the procedural methods. Just not a lot of coding using object oriented style.

    PHP Code:
    <?php

    define
    ('UPLOAD_PATH'$_SERVER['DOCUMENT_ROOT'] . '/test_images/');
    define('DISPLAY_PATH''/test_images/');
    define('MAX_FILE_SIZE'2000000);
    $permitted = array('image/jpeg''image/pjpeg''image/png''image/gif');

    if (isset(
    $_POST['upload'])) {

        
    $team_id $_POST['team_id'];
        
    $fileName $_FILES['userfile']['name'];
        
    $tmpName $_FILES['userfile']['tmp_name'];
        
    $fileSize $_FILES['userfile']['size'];
        
    $fileType $_FILES['userfile']['type'];

        
    // get the file extension 
        
    $ext substr(strrchr($fileName"."), 1);
        
    // generate the random file name
        
    $randName md5(rand() * time());

        
    // image name with extension
        
    $myfile $randName '.' $ext;
        
    // save image path
        
    $path UPLOAD_PATH $myfile;

        if (
    in_array($fileType$permitted) && $fileSize && $fileSize <= MAX_FILE_SIZE) {
            
    //store image to the upload directory
            
    $result move_uploaded_file($tmpName$path);

            if (!
    $result) {
                echo 
    "Error uploading image file";
                exit;
            } else {
                
    $db = new mysqli("localhost""******""**********""ftsc");

                if (
    mysqli_connect_errno()) {
                    
    printf("Connect failed: %s<br/>"mysqli_connect_error());
                }

                
    $query =
                  
    "INSERT INTO photos(team_id, name, size, type, file_path) VALUES(?,?,?,?,?)";
                
    $conn $db->prepare($query);
                if (
    $conn == TRUE) {
                    echo 
    "i'm here\n";
                    
    $conn->bind_param("ssiss"$team_id$myfile$fileSize$fileType$path);
                    if (!
    $conn->execute()) {
                        echo 
    'error insert';
                    } else {
                        echo 
    'Success!<br/>';
                        echo 
    '<img src="' DISPLAY_PATH $myfile '"/>';
                    }
                } else {
                    die(
    "Error preparing Statement");
                }
            }
        } else {
            echo 
    'error upload file';
        }
    } else {
        echo 
    'error';
    }
    ?>
    Any tips/tricks are welcome and pointing out my idiotic mistakes are even more welcome!

    Thanks for the help in advance!

    - Dave
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    you can use the ->error property to find out why the prepare failed. add echo $db->error; right before your die("Error preparing Statement"); statement.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    MrBiggZ (04-22-2014)

  • #3
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    275
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    you can use the ->error property to find out why the prepare failed. add echo $db->error; right before your die("Error preparing Statement"); statement.
    OMG I was missing a column in the table! =\ I have no clue on how that happened!

    BTW would you happen to have any good sources on learning the object method? I'm sure if I have something to reference it'll make more sense to me. There's been a number of times the php doc has had much to be desired.

    Thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •