php - Why is data I upload getting renamed, and corresponding data added to different rows? -


when insert data database, data submits successfully, have no idea how "success" message display informing me data has been sent successfully; have assume been submitted successfully.

then, when check in database, data there, not in way want. upload image, spongebob.png , image gets sent database 15-04-2015-1429064604.png, , image , data corresponding image inserted on separate rows data on row above image... no idea why.

i want image name same image uploaded, , on same row other data.

a screenshot of i'm getting:

http://screencast.com/t/qgas8dd3e

here's html:

<form action="insert_backend.php" method="post" enctype="multipart/form-data">  <!-- method can set post hiding values in url-->  <h2>form</h2>  <label for="uploadedimage">small image upload: </label>  <input type="file" name="uploadedimage" id="uploadedimage"/><br />  <label>date:</label>  <input class="input" name="date" type="text" value=""><br />  <label>retrace:</label>  <input class="input" name="retrace" type="text" value=""><br />  <label>start of swing trade:</label>  <input class="input" name="start_of_swing_trade" type="text" value=""><br />  <label>end of swing trade:</label>  <input class="input" name="end_of_swing_trade" type="text" value=""><br />  <label>bull flag:</label>  <input class="input" name="bull_flag" type="text" value=""><br />  <label>bear flag:</label>  <input class="input" name="bear_flag" type="text" value=""><br />  <label>ema crossover:</label>  <input class="input" name="ema_crossover" type="text" value=""><br />  <label>trading instrument:</label>  <input class="input" name="trading_instrument" type="text" value=""><br />  <input class="submit" name="submit" type="submit" value="insert">  </form>

here's php:

<?php  /**********mysql settings****************/  $host="";  $databasename="";  $user="";  $pass="";  /**********mysql settings****************/    error_reporting(e_all);  ini_set('display_errors', 1);    $conn=mysql_connect($host,$user,$pass);    if($conn)  {  $db_selected = mysql_select_db($databasename, $conn);  if (!$db_selected) {      die ('can\'t use foo : ' . mysql_error());  }  }  else  {      die('not connected : ' . mysql_error());  }  ?>  <?php      function getimageextension($imagetype)      {         if(empty($imagetype)) return false;         switch($imagetype)         {             case 'image/bmp': return '.bmp';             case 'image/gif': return '.gif';             case 'image/jpeg': return '.jpg';             case 'image/png': return '.png';             default: return false;         }         }    if (!empty($_files["uploadedimage"]["name"])) {    	$file_name=$_files["uploadedimage"]["name"];  	$temp_name=$_files["uploadedimage"]["tmp_name"];  	$imgtype=$_files["uploadedimage"]["type"];  	$ext= getimageextension($imgtype);  	$imagename=date("d-m-y")."-".time().$ext;  	$target_path = "images/".$imagename;  	    if(move_uploaded_file($temp_name, $target_path)) {            $query_upload="insert charts ( charts_url ) values ('".$target_path."')";  	mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());    	  }else{       echo("error while uploading image on server");  }     }  ?>  <?php  if(isset($_post['submit'])){ // fetching variables of form travels in url  $date = $_post['date'];  $retrace = $_post['retrace'];  $start_of_swing_trade = $_post['start_of_swing_trade'];  $end_of_swing_trade = $_post['end_of_swing_trade'];  $bull_flag = $_post['bull_flag'];  $bear_flag = $_post['bear_flag'];  $ema_crossover = $_post['ema_crossover'];  $trading_instrument = $_post['trading_instrument'];  if($date !=''||$trading_instrument !=''){  //insert query of sql  $query_upload = "insert charts (charts_date, charts_retrace, charts_start_of_swing_trade, charts_end_of_swing_trade, charts_bullflag, charts_bearflag, charts_ema_crossover, charts_trading_instrument) values ('$date', '$retrace', '$start_of_swing_trade', '$end_of_swing_trade', '$bull_flag', '$bear_flag', '$ema_crossover', '$trading_instrument')";  mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());    	  }else{       echo("data sent successfully!");  }   }  mysql_close($conn); // closing connection server  ?>

the new changes made above php code:

the image uploading code

if (!empty($_files["uploadedimage"]["name"])) {    	$file_name=$_files["uploadedimage"]["name"];  	$temp_name=$_files["uploadedimage"]["tmp_name"];  	$imgtype=$_files["uploadedimage"]["type"];  	$ext= getimageextension($imgtype);  	$imagename=$_files['uploadedimage']['name'];  	$target_path = "images/".$imagename;  	    if(move_uploaded_file($temp_name, $target_path)) {            $query_upload="insert charts ( charts_url ) values ('".$target_path."')";    	  }    $result =  mysqli_query($conn, $query_upload);  if ($result)  {   echo "success, image has been inserted successfully";  }  else  {   die("error: " . $sql . "<br>" . mysqli_error($conn);  }

the data inserting code below image upload button

if($date !=''||$trading_instrument !=''){  //insert query of sql  $query_upload = "insert charts (charts_date, charts_retrace, charts_start_of_swing_trade, charts_end_of_swing_trade, charts_bullflag, charts_bearflag, charts_ema_crossover, charts_trading_instrument) values ('$date', '$retrace', '$start_of_swing_trade', '$end_of_swing_trade', '$bull_flag', '$bear_flag', '$ema_crossover', '$trading_instrument')";  	  }  $result =  mysqli_query($conn, $query_upload);  if ($result)  {   echo "success, data has been inserted successfully";  }  else  {   die("error: " . $sql . "<br>" . mysqli_error($conn);  }

when insert data database, data submits have no idea how "success" message display informing me data has been sent successfully.

$result =  mysqli_query($connection, $query); if ($result) {  // use echo "success, data has been inserted or create success page , redirect page here } else {  // query fails } 


i can upload image spongebob.png , image gets sent database 15-04-2015-1429064604.png. want image name same image uploaded.

$_files['uploadedimage']['name'] give original image name. 

in code doing like,

$imagename=date("d-m-y")."-".time().$ext; // adding date, time , extension of image. $target_path = "images/".$imagename; 

so, like

$imagename= $_files['uploadedimage']['name']; // `$imagename` contain spongebob.png $target_path = "images/".$imagename; // images/spongebob.png 

important :

`mysql_*` deprecated. migrate `mysqli_*` or `pdo`. code vulnerable sql injection.  

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -