为什么我上传的数据被重命名,并且相应的数据被添加到不同的行?
Why is data I upload getting renamed, and corresponding data added to different rows?
当我向数据库中插入数据时,数据提交成功,但我不知道如何让"Success"消息显示,通知我数据已发送成功;我只需要假设它已成功提交。
然后,当我在数据库中检查它时,数据就在那里,但不是我想要的方式。假设我上传图像 spongebob.png
并且发送到数据库的图像是 15-04-2015-1429064604.png
,图像和与该图像对应的数据被插入到单独的行中,数据位于图像上方的行中...不知道为什么。
我希望图片名称与我上传的图片名称相同,并且与其他数据在同一行。
我得到的屏幕截图:
这是我的 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>
这是我的 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
?>
我对上面PHP代码所做的新改动:
图片上传代码
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);
}
图片上传按钮下方的数据插入代码
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);
}
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.
在您的代码中,您的做法是,
$imagename=date("d-m-Y")."-".time().$ext; // you are adding date, time and extension of the image.
$target_path = "images/".$imagename;
所以,喜欢
$imagename= $_FILES['uploadedimage']['name']; // `$imagename` will now contain spongebob.png
$target_path = "images/".$imagename; // images/spongebob.png
重要提示:
`mysql_*` is deprecated. Migrate it to `mysqli_*` or `PDO`. You code is vulnerable to SQL Injection.
当我向数据库中插入数据时,数据提交成功,但我不知道如何让"Success"消息显示,通知我数据已发送成功;我只需要假设它已成功提交。
然后,当我在数据库中检查它时,数据就在那里,但不是我想要的方式。假设我上传图像 spongebob.png
并且发送到数据库的图像是 15-04-2015-1429064604.png
,图像和与该图像对应的数据被插入到单独的行中,数据位于图像上方的行中...不知道为什么。
我希望图片名称与我上传的图片名称相同,并且与其他数据在同一行。
我得到的屏幕截图:
这是我的 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>
这是我的 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
?>
我对上面PHP代码所做的新改动:
图片上传代码
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);
}
图片上传按钮下方的数据插入代码
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);
}
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.
在您的代码中,您的做法是,
$imagename=date("d-m-Y")."-".time().$ext; // you are adding date, time and extension of the image.
$target_path = "images/".$imagename;
所以,喜欢
$imagename= $_FILES['uploadedimage']['name']; // `$imagename` will now contain spongebob.png
$target_path = "images/".$imagename; // images/spongebob.png
重要提示:
`mysql_*` is deprecated. Migrate it to `mysqli_*` or `PDO`. You code is vulnerable to SQL Injection.