Thomas
Thomas

Reputation: 69

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

When I insert data into the database, the data submits successfully, but I have no idea how to get a "Success" message to display informing me that the data has been sent successfully; I just have to assume its been submitted successfully.

Then, when I check it in the database, the data is there, but not in the way I want. Say I upload the image, spongebob.png and the image that gets sent to the database is 15-04-2015-1429064604.png, and the image and data corresponding to that image are inserted on separate rows with the data on the row above the image... no idea why.

I want the image name to be the same as the image I uploaded, and to be on the same row as the other data.

A screenshot of what I'm getting:

http://screencast.com/t/QGAS8Dd3E

Here's my HTML:

<form action="insert_backend.php" method="POST" enctype="multipart/form-data">
<!-- Method can be set as POST for hiding values in URL-->
<h2>Form</h2>
<label for="uploadedimage">Small image to 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 my 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 into 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 the server");
} 

}
?>
<?php
if(isset($_POST['submit'])){ // Fetching variables of the form which 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 into 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 with Server
?>

The new changes I made to the 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 INTO 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 the image upload button

if($date !=''||$trading_instrument !=''){
//Insert Query of SQL
$query_upload = "INSERT into 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);
}

Upvotes: 1

Views: 111

Answers (1)

Yuva Raj
Yuva Raj

Reputation: 3881

When I Insert data into the database, the data submits successfully but I have no idea how to get a "Success" message to display informing me that the data has been sent successfully.

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


I can upload the image spongebob.png and the image that gets sent to the database is 15-04-2015-1429064604.png. I want the image name to be the same as the image I uploaded.

$_FILES['uploadedimage']['name'] will give you the original image name.

In your code you are doing like,

$imagename=date("d-m-Y")."-".time().$ext; // you are adding date, time and extension of the image.
$target_path = "images/".$imagename;

So, do like

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

IMPORTANT :

`mysql_*` is deprecated. Migrate it to `mysqli_*` or `PDO`. You code is vulnerable to SQL Injection. 

Upvotes: 2

Related Questions