Saturday, 15 May 2010

php - Having error in saving image link to the database using pdo -



php - Having error in saving image link to the database using pdo -

this question has reply here:

how can upload files asynchronously? 22 answers

i want create image uploader scheme , send image upload folder , save link database.

my problem got errors in images.php can help me please.

html:

<form method="post" enctype="multipart/form-data"> <img id="picture" data-src="#" /> <br /> <input type='file' name="image" id="imginp" accept="image/*" /><br /> <input type="submit" name="submit" id="submit" value="submit" /> </form>

script:

<script type="text/javascript"> $(document).ready(function() { $('#submit').click(function (e) { e.preventdefault(); var info = {}; data.image = $('#imginp').val(); $.ajax({ type: "post", url: "images.php", data: data, cache: false, success: function (response) { } }); homecoming false; }); }); </script>

images.php

<?php $host = "localhost"; $user = "root"; $pass = ""; $db = "test"; $dbc = new pdo("mysql:host=" . $host . ";dbname=" . $db, $user, $pass); $dbc->setattribute(pdo::attr_errmode, pdo::errmode_exception); $image = addslashes(file_get_contents(@$_files['image']['tmp_name'])); $image_name = addslashes(@$_files['image']['name']); $image_size = getimagesize(@$_files['image']['tmp_name']); move_uploaded_file(@$_files["image"]["tmp_name"], "upload/" . @$_files["image"]["name"]); $location = "upload/" . @$_files["image"]["name"]; $q = "insert students( image ) values( :image)"; $query = $dbc->prepare($q); $query->bindparam(':image', $location); $results = $query->execute(); ?>

script image upload:

<script type="text/javascript"> $(document).ready(function() { var currentsrc = $('#picture').attr('src'); if(currentsrc==null || currentsrc==""){ $('#picture').attr('src','http://i38.photobucket.com/albums/e149/eloginko/profile_male_large_zpseedb2954.jpg'); $("#picture").on('click', function() { $("#imginp").trigger('click')} )} function readurl(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { $('#picture').attr('src', e.target.result); } reader.readasdataurl(input.files[0]); } } $("#imginp").change(function(){ readurl(this); }); }); </script>

the simplest thing rid of error messages place conditional checks if $_files has in it. past that, unclear on root cause of $files beingness empty. in experience ajax file uploading php receiver on other side doesn’t work consistently @ best. anyway, here version of code conditional in place:

if (!empty($_files)) { $image = addslashes(file_get_contents(@$_files['image']['tmp_name'])); $image_name = addslashes(@$_files['image']['name']); $image_size = getimagesize(@$_files['image']['tmp_name']); move_uploaded_file(@$_files["image"]["tmp_name"], "upload/" . @$_files["image"]["name"]); $location = "upload/" . @$_files["image"]["name"]; $q = "insert students( image ) values( :image)"; $query = $dbc->prepare($q); $query->bindparam(':image', $location); $results = $query->execute(); }

php html ajax file-upload pdo

No comments:

Post a Comment