Jump to content

Script not inserting to DB


batstanggt

Recommended Posts

Hey guys Im currently using this script to upload pictures to a table in my database. However it doesnt seem to be working from the standpoint that after submitting the form nothing appears to be happening, and certianly no inserting is going on (when I check the table in phpMyadmin its empty). Anyways I was hoping someone here may be able to tell me what im doing wrong.

  </head>
<body>
  <h2>Please Choose a File and click Submit</h2>
  
  <form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
  
  <input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
  
  <div>Title: <input name="pic_name" type="text" /></div>
  
  <div>Description: <input name="pic_desc" type="text" /></div>
  
  <div>Picture to Upload<input name="picture" type="file" /></div>
  
  <div>yes or no question </div>
  
  <div>Yes: <input name="group" type="radio" value="y" /> No: 
  <input name="group" type="radio" value="n" /> </div>
  
  <div><input type="submit" value="Submit" /></div>
  </form>
  
<?php
/*** check if a file was submitted ***/
if(!isset($_FILES['picture']))
    {
    echo '<p>Please select a file</p>';
    }
else
    {
    try    {
        upload();
        /*** give praise and thanks to the php gods ***/
        echo '<p>Thank you for submitting</p>';
        }
    catch(Exception $e)
        {
        echo '<h4>'.$e->getMessage().'</h4>';
        }
    }
?>
<?php
/**
*
* the upload function
* 
* @access public
*
* @return void
*
*/
function upload(){
/*** check if a file was uploaded ***/
if(is_uploaded_file($_FILES['picture']['tmp_name']) && getimagesize($_FILES['picture']['tmp_name']) != false)
    {
    /***  get the image info. ***/
    $size = getimagesize($_FILES['picture']['tmp_name']);
    /*** assign our variables ***/
    $type = $size['mime'];
    $imgfp = fopen($_FILES['picture']['tmp_name'], 'rb');
    $size = $size[3];
    $pic_name = $_FILES['picture']['pic_name'];
$pic_desc = $_POST['pic_desc'];
$sixval = $_POST['group'];
    $maxsize = 99999999;


    /***  check the file is less than the maximum file size ***/
    if($_FILES['picture']['size'] < $maxsize )
        {
        /*** connect to db ***/
        $dbh = new PDO("mysql:host=localhost;dbname=dbname", 'mysqlusername', 'mysqlpassword');

                /*** set the error mode ***/
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            /*** our sql query ***/
        $stmt = $dbh->prepare("INSERT INTO mypictures (pic_type , picture, pic_size, pic_name, pic_desc, sixval) VALUES (? ,?, ?, ?, ?, ?)");

        /*** bind the params ***/
        $stmt->bindParam(1, $type);
        $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
        $stmt->bindParam(3, $size);
        $stmt->bindParam(4, $pic_name);
  $stmt->bindParam(5, $pic_desc);
  $stmt->bindParam(6, $sixval);

        /*** execute the query ***/
        $stmt->execute();
        }
    else
        {
        /*** throw an exception is image is not of type ***/
        throw new Exception("File Size Error");
        }
    }
else
    {
    // if the file is not less than the maximum allowed, print an error
    throw new Exception("Unsupported Image Format!");
    }
}
?>
</body></html>

Any suggestions?

-SB

Link to comment
Share on other sites

this:

$stmt = $dbh->prepare("INSERT INTO mypictures (pic_type , picture, pic_size, pic_name, pic_desc, sixval) VALUES (? ,?, ?, ?, ?, ?)");

 

is obviously incomplete. you need to put in your variables instead of the question marks.

 

He does, through the bindParam() function inside of the PDO class.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.