php - Image file not inserting into database -


i'm trying image file send file have in host server called productpics/ , send file's name database of other data in form. i'm getting image file send productpics folder, prepared statement breaks , nothing else happens. nothing sent db. believe because of way i'm trying send image file db.

the line believe breaking is....

i'm getting error when submit form, regardless i'm not sure if trying send db correctly. doing proper way or based on have, how can structure this?

fatal error: function name must string in /home4/pfarley1/public_html/addproduct.php on line 110

//create     $filename = $_files['file']['name'];     //$filesize = $_files['file']['size'];     //$filetype = $_files['file']['type'];     $tmp_name = $_files['file']['tmp_name'];     $file_error = $_files['file']['error'];        if (isset($filename )) {         if (!empty($filename)) {              $destinationfolder = 'productpics/';              if (move_uploaded_file($tmp_name, $destinationfolder.$filename)) {                 echo 'uploaded!';             } else {                 echo 'there error!';             }          } else {             echo 'please choose file.';         }     }     if($validation->passed()) {         if(isset($_post['create'])){              $product_id = trim( $_post['product_id'] );             $product_name = trim( $_post['product_name'] );             $price = trim( $_post['price'] );             $saleprice = trim( $_post['saleprice'] );             $final_price = trim( $_post['final_price'] );             $shippingprice = trim( $_post['shippingprice'] );             $category = trim( $_post['category'] );             $item_details = trim( $_post['item_details'] );             $item_details2 = trim( $_post['item_details2'] );             $description = trim( $_post['description'] );             $viewproduct_type = trim( $_post['viewproduct_type'] );             $file = $_post ($filename['img']);             }else {             foreach($validation->errors() $error) {                 echo $error, '<br>';             }           //connection         $con = mysqli_connect("localhost","root","","bfb");         /* check connection */             if (mysqli_connect_errno()) {                 printf("connect failed: %s\n", mysqli_connect_error());                 exit();             }                /* create prepared statement */             if ($stmt = mysqli_prepare($con, "insert products (product_id, product_name, price, saleprice, final_price, shippingprice, category, item_details, item_details2, description, viewproduct_type, date_created, img) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), ?)")) {                   /* bind parameters markers */                 $stmt->bind_param('isiiiissssss', $product_id, $product_name, $price, $saleprice, $final_price, $shippingprice, $category, $item_details, $item_details2, $description, $viewproduct_type, $file);                  /* execute query */                 $stmt->execute();                 //if(!$stmt->execute()){trigger_error("there error....".$con->error, e_user_warning);}               /* close statement */             mysqli_stmt_close($stmt);                 echo "success!";             } else {                 echo "failed!";             }             } 

with of said, not getting echo else statement prepared statement's query. not getting 'success!' or 'failed!'. why aren't results of showing?

my form this(the img file part @ bottom of it...

<form action="" method="post" enctype="multipart/form-data">  <div class="field">     <label for="product_id">product id</label>     <input type="text" name="product_id" class="smallinputbar" required> </div> <div class="field">     <label for="product_name">product name</label>     <input type="text" class="inputbar" name="product_name" required> </div> <div class="field">     <label for="price">product price</label>     <input type="text" class="smallinputbar" name="price" required> </div> <div class="field">     <label for="saleprice">sale price</label>     <input type="text" class="smallinputbar" name="saleprice"> </div> <div class="field">     <label for="final_price">final price</label>     <input type="text" class="smallinputbar" name="final_price" required> </div> <div class="field">     <label for="shippingprice">shipping price</label>     <input type="text" class="smallinputbar" name="shippingprice" required> </div> <div class="field">     <label for="category">category</label>     <input type="text" class="inputbar" name="category" required> </div> <div class="field">     <label for="item_details">item details</label>     <input type="message" class="messageinput" name="item_details" required> </div> <div class="field">     <label for="item_details2">item details 2</label>     <input type="message" class="messageinput" name="item_details2"> </div> <div class="field">     <label for="description">description</label>     <input type="message" class="messageinput" name="description" required> </div> <div class="field">     <label for="viewproduct_type">view product type</label>     <select class="optionbar" name="viewproduct_type">         <option value="not selected">not selected</option>         <option value="a href='./viewproduct.php?view_product=$id">standard</option>         <option value="option">option</option>     </select> </div>  <span class="floatright">     <input type="file" name="file" class="inputbarfile">     <!--<input type="submit" name="create" id="signinbutton" value="upload">--> </span> <input type="hidden" name="token" value="<?php echo token::generate(); ?>">     <label for="button">     <input type="submit" id="button" name="create" value="create new product">     </label>   </form> 

update:

//create     $filename = $_files['file']['name'];     //$filesize = $_files['file']['size'];     //$filetype = $_files['file']['type'];     $tmp_name = $_files['file']['tmp_name'];     $file_error = $_files['file']['error'];        if (isset($filename )) {         if (!empty($filename)) {              $destinationfolder = 'productpics/';              if (move_uploaded_file($tmp_name, $destinationfolder.$filename)) {                 echo 'uploaded!';             } else {                 echo 'there error!';             }          } else {             echo 'please choose file.';         }     }      if($validation->passed()) {         if(isset($_post['create'])){              $product_id = trim( $_post['product_id'] );             $product_name = trim( $_post['product_name'] );             $price = trim( $_post['price'] );             $saleprice = trim( $_post['saleprice'] );             $final_price = trim( $_post['final_price'] );             $shippingprice = trim( $_post['shippingprice'] );             $category = trim( $_post['category'] );             $item_details = trim( $_post['item_details'] );             $item_details2 = trim( $_post['item_details2'] );             $description = trim( $_post['description'] );             $viewproduct_type = trim( $_post['viewproduct_type'] );             $file = $filename; 

full php code question.

//validation if(input::exists()) {     if(token::check(input::get('token'))) {          $validate = new validate();         $validation = $validate->check($_post, array(             'product_id' => array(                 'required' => true,                 'min' => 1,                 'max' => 50,                 'unique' => 'products'             ),             'product_name' => array (                 'required' => true,                 'min' => 2,                 'max' => 50             ),             'price' => array (                 'required' => true,                 'min' => 1,                 'max' => 50             ),             'saleprice' => array (                 'min' => 1,                 'max' => 50             ),             'final_price' => array (                 'required' => true,                 'min' => 1,                 'max' => 50             ),             'shippingprice' => array (                 'max' => 50             ),             'category' => array (                 'required' => true,                 'min' => 2,                 'max' => 50             ),             'item_details' => array (                 'required' => true,                 'min' => 2,                 'max' => 1550             ),             'item_details2' => array (                 'max' => 1550             ),             'description' => array (                 'required' => true,                 'min' => 2,                 'max' => 1550             )          )         ); //create          if($validation->passed()) {          $filename = $_files['file']['name'];         //$filesize = $_files['file']['size'];         //$filetype = $_files['file']['type'];         $tmp_name = $_files['file']['tmp_name'];         $file_error = $_files['file']['error'];            if (isset($filename )) {             if (!empty($filename)) {                  $destinationfolder = 'productpics/';                  if (move_uploaded_file($tmp_name, $destinationfolder.$filename)) {                     echo 'uploaded!';                 } else {                     echo 'there error!';                 }              } else {                 echo 'please choose file.';             }         }             if(isset($_post['create'])){                  $product_id = trim( $_post['product_id'] );                 $product_name = trim( $_post['product_name'] );                 $price = trim( $_post['price'] );                 $saleprice = trim( $_post['saleprice'] );                 $final_price = trim( $_post['final_price'] );                 $shippingprice = trim( $_post['shippingprice'] );                 $category = trim( $_post['category'] );                 $item_details = trim( $_post['item_details'] );                 $item_details2 = trim( $_post['item_details2'] );                 $description = trim( $_post['description'] );                 $viewproduct_type = trim( $_post['viewproduct_type'] );                 $file = $filename;                 $file = $_post['img'];              }else {                 foreach($validation->errors() $error) {                     echo $error, '<br>';                 }              //connection             $con = mysqli_connect("localhost","root","","bfb");             /* check connection */                 if (mysqli_connect_errno()) {                     printf("connect failed: %s\n", mysqli_connect_error());                     exit();                 }                    /* create prepared statement */                 if ($stmt = mysqli_prepare($con, "insert products (product_id, product_name, price, saleprice, final_price, shippingprice, category, item_details, item_details2, description, viewproduct_type, date_created, img) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), ?)")) {                       /* bind parameters markers */                     $stmt->bind_param('isiiiissssss', $product_id, $product_name, $price, $saleprice, $final_price, $shippingprice, $category, $item_details, $item_details2, $description, $viewproduct_type, $file);                      /* execute query */                     $stmt->execute();                     //if(!$stmt->execute()){trigger_error("there error....".$con->error, e_user_warning);}                   /* close statement */                 mysqli_stmt_close($stmt);                     echo "success!";                 } else {                     echo "failed!";                 }                 }             }         }      } 

commenting , forth getting long @ point , submitting following answer, since long comment.

although may not solution (or possibly could, or may lead one), nonetheless answer problem/error message posted.

the problem line $file = $_post ($filename['img']); , have no idea you're trying here. told me earlier in comments "the column have 'img' text".

  • post has nothing column.
  • post arrays use syntax $_post[] using square brackets, , not $_post() round brackets.

that why you're getting following error:

fatal error: function name must string in /home4/pfarley1/public_html/addproduct.php on line 110

i don't know if want insert uploaded file binary in column, or text-based pointer file, know exact intention it.

if want enter binary, need set associated column blob.

when using blob column type, data needs escaped, otherwise throw error.

you want check upload max size set/allowed in system files.

by default, php sets 2m. if file exceeds size, fail; increase , else relative such max timeout time.

you're not doing error checking in:

$file_error = $_files['file']['error']; 

it's stray/unused variable.

consult:

to check errors , use it.

as far i'm concerning, rid of $file = $_post ($filename['img']); , use $filename variable you're wanting enter in database, since going inside img column you've set in query.

  • check column length see if it's long enough accommodate entry.

"the column have 'img' text."

  • you may want change text varchar , set long enough length it. mysql may failing silently because of it.

another thing suggest do, place $filename = $_files['file']['name']; , other variables below that, inside conditional statement.


if want use similar syntax replace $file = $_post ($filename['img']);, add additional input , give img name attribute , do:

$file = $_post['img']; 

which valid.


another thing spotted in first piece of code, , if entire code, you're missing closing brace } if($validation->passed()) { conditional statement.

the final/last brace } associated block of code:

else {             foreach($validation->errors() $error) {                 echo $error, '<br>';             } 
  • therefore, need check bracing matches/pairs.

edit:

in line added in edit:

$file = $filename; $file = $_post['img']; 

you're overwriting first variable, , stated in comments not have form element name img.

but file not text. use $file = $_files['img']; or $file = $_files['file']; - @ point, i've no idea file input in form called.

  • if still gives trouble, use $file = $_files['file']['name'];

mysql , php 2 different animals , not know column used insertion.

you cannot rely on post array determine column inserted in. need specify in query.

make sure img column in fact exist, , use $_files array related variable value in values, being $file.

  • however, use $filename in values, instead of $file. or, whatever variable; confused @ point variable and/or input name you're using.

and may need add parameter in $validation = $validate->check($_post, array(... function/array.


add error reporting top of file(s) find errors.

<?php  error_reporting(e_all); ini_set('display_errors', 1);  // rest of code 

sidenote: error reporting should done in staging, , never production.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -