为什么我上传的数据被重命名,并将相应的数据添加到不同的行? [英] Why is data I upload getting renamed, and corresponding data added to different rows?
问题描述
当我向数据库中插入数据时,数据提交成功,但我不知道如何获得成功消息来通知我数据已成功发送;我只需假定它已成功提交。
然后,当我在数据库中检查它时,数据就在那里,但没有按照我想要的方式。假设我上传了图片 spongebob.png
,并且发送到数据库的图片是 15-04-2015-1429064604.png
,并且与该图像相对应的图像和数据被插入到图像上方行上的数据的单独行上...不知道为什么。
我希望图片名称与我上传的图片相同,并与其他数据位于同一行。
我得到的截图:
这是我的HTML:
< form action =insert_backend.phpmethod =POSTenctype =multipart / form-data>< ! - 可将方法设置为POST,以便在URL中隐藏值 - >< h2>表格< / h2>< label for =uploadedimage>小图片上传:< / label>< input < / label>< input class =inputname =datetype =filename =uploadedimageid =uploadedimage/>< br /> textvalue =>< br />< label> Retrace:< / label>< input cl < label>>< input> name =" retrace" type = < label>< / label>< input class =inputname =end_of_swing_tradetype = textvalue =>< br />< label> Bull flag:< / label>< input class =inputname =bull_flagtype =textvalue => ;< br>< label> Bear flag:< / label>< input class =inputname =bear_flagtype =textvalue =>< br /><< br ;标签> EMA Crossover:< / label>< input class =inputname =ema_crossovertype =textvalue =>< br>< label> Trading Instrument:< / label>< input class =inputname =trading_instrumenttype =textvalue =>< br />< input class =submitname =submittype =submit value =Insert>< / form>
这是我的PHP:
<?php / ********** MYSQL设置**************** / $ host = ; $ databasename =; $ user =; $ pass =; / ********** MYSQL设置**************** / 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('不能使用foo:'。 mysql_error());}} else {die('Not connected:'。mysql_error());}?><?php function GetImageExtension($ imagetype){if(empty($ imagetype))return false;开关($ imagetype){case'image / bmp':return'.bmp'; case'image / gif':return'.gif'; 'image / jpeg':返回'.jpg'; case'image / png':return'.png';默认值:返回false; }} if(!empty($ _ FILES [uploadedimage] [name])){$ file_name = $ _ FILES [uploadedimage] [name]; $ temp_name = $ _ FILES [ uploadedimage] [ tmp_name的值]; $ imgtype = $ _ FILES [ uploadedimage] [ 型]; $ ext = GetImageExtension($ imgtype); $ imagename =日期( d-M-Y) - 时间()$分机;。 $ 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)或死(错误在$ query_upload == ---->.mysql_error()); }}?><?phpif(isset($ _ POST ['submit'])){//获取在URL中传输的表单的变量$日期= $ _POST [ '日期']; $回扫= $ _POST [ '回扫']; $ 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 '];!如果($日期=''|| $ trading_instrument! = ''){// SQL $ query_upload的插入查询=INSERT成图表(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( '$日期', '$回扫',$ start_of_swing_trade ','$ end_of_swing_trade','$ bull_flag','$ bear_flag','$ ema_crossover','$ trading_instrument'); mysql_query($ query_upload)或die(e $ query_upload == ---->>.mysql_error()); } else {echo(Data sent successfully!);}} mysql_close($ conn); //关闭与服务器的连接>>
$ b 我对上面的PHP代码做了一些新的修改:
图片上传代码
图片上传按钮
if($ date!=''|| $ trading_instrument != ''){// SQL $ query_upload的插入查询=INSERT INTO图表(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( '$日期', '$回扫',$ ($ conn,$ query_upload); if($ result){$ start_of_swing_trade','$ end_of_swing_trade','$ bull_flag','$ bear_flag','$ ema_crossover','$ trading_instrument');} $ result = mysqli_query回声成功,数据已成功插入;} else {die(Error:。 $ sql。 <峰; br> 中。 mysqli_error($ conn);}
将数据插入数据库时,数据成功提交
,但我不知道如何获取成功消息以显示通知
me数据已成功发送。
$ result = mysqli_query($ connection,$ query);
if($ result)
{
//使用echo成功,数据已成功插入或创建成功页面并从此处重定向到另一页
}
else
{
//查询失败
}
< b
$ b
我可以上传图像spongebob.png和发送到
的图像,数据库是15-04 -2015-1429064604.png。我希望图片名称为
,与我上传的图片相同。
<$ p $
$> $ _ FILES ['uploadedimage'] ['name']会为您提供原始图片名称。
在你的代码中,你喜欢,
$ imagename = date(dmY)。 - 。time()。$ ext; //添加日期,时间和图像的扩展名
$ target_path =images /\".$ imagename;
因此,像
$ imagename = $ _FILES ['uploadedimage'] ['name']; //`$ imagename`现在将包含spongebo b.png
$ target_path =images /\".name imagename; // images / spongebob.png
重要提示:
`mysql_ *`已弃用。迁移到`mysqli_ *`或`PDO`。您的代码易受SQL注入攻击。
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 is15-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:
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); }
解决方案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.
这篇关于为什么我上传的数据被重命名,并将相应的数据添加到不同的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!