searls03 Posted August 9, 2011 Share Posted August 9, 2011 I have this code: <?php if($_POST['submit']){ // Where the file is going to be placed $target_path = "images/".$_SESSION['username']."/"; foreach ($_FILES["uploadedfile"]["name"] as $key => $value) { $uploadfile = $target_path . basename($_FILES[uploadedfile][name][$key]); //echo $uploadfile; if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$key], $uploadfile)) { echo $value . ' uploaded<br>'; } } } ?> and I want to insert: $sql = mysql_query("UPDATE pics SET link='$target_path' WHERE id='$userid1'")or die(mysql_error()); so that when it uploads the file, it will also put info, such as a link to the file in the dbase. how can I make $target_path = "images/".$_SESSION['username']."/ (the name of the file)"; I am stumped as to where to put the code or how to do this. please help. Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/ Share on other sites More sharing options...
phpSensei Posted August 9, 2011 Share Posted August 9, 2011 Hi Sean, If you want to insert use INSERT not UPDATE. I also don't understand the problem here, you already seem to have figured it out as you explained your situation. Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254704 Share on other sites More sharing options...
searls03 Posted August 9, 2011 Author Share Posted August 9, 2011 I want to insert that piece of code into the first piece. I do not know where to put it or how to make it so that it is the file name on the end of $target_path. I need to know where to place the second code inside of the first. haha Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254708 Share on other sites More sharing options...
phpSensei Posted August 9, 2011 Share Posted August 9, 2011 Hi Searls, That piece of code will go inside the Foreach loop, inbetween this line if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$key], $uploadfile)) { /// insert piece of code echo $value . ' uploaded<br>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254710 Share on other sites More sharing options...
searls03 Posted August 9, 2011 Author Share Posted August 9, 2011 And what do I usr for the file name piece? Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254711 Share on other sites More sharing options...
phpSensei Posted August 9, 2011 Share Posted August 9, 2011 Hi Searls, The file name would be in full path $uploadfile = $target_path . basename($_FILES[uploadedfile][name][$key]); Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254712 Share on other sites More sharing options...
searls03 Posted August 9, 2011 Author Share Posted August 9, 2011 So how exactly is the SQL statement written then???? Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254714 Share on other sites More sharing options...
phpSensei Posted August 9, 2011 Share Posted August 9, 2011 Do some research man, Its not that hard. Google "Mysql INSERT Tutorial" Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254715 Share on other sites More sharing options...
voip03 Posted August 9, 2011 Share Posted August 9, 2011 $sql = "INSERT INTO tableName (imageName, userid) VALUES('" .$uploadfile ."','" .$_SESSION['username']."')"; http://www.w3schools.com/PHP/php_mysql_insert.asp Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254742 Share on other sites More sharing options...
phpSensei Posted August 9, 2011 Share Posted August 9, 2011 $sql = "INSERT INTO tableName (imageName, userid) VALUES('" .$uploadfile ."','" .$_SESSION['username']."')"; http://www.w3schools.com/PHP/php_mysql_insert.asp You're simply doing all the work for him, let him learn and try it before asking us to type for him. P.S, Stay away from w3schools. Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1254751 Share on other sites More sharing options...
searls03 Posted August 15, 2011 Author Share Posted August 15, 2011 I have this and it still does not update database, it uploads though........ <?php if($_POST['submit']){ // Where the file is going to be placed $target_path = "images/".$_SESSION['username']."/"; foreach ($_FILES["uploadedfile"]["name"] as $key => $value) { $uploadfile = $target_path . basename($_FILES[uploadedfile][name][$key]); //echo $uploadfile; if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$key], $uploadfile)) { $sql = mysql_query("UPDATE pics SET link='$target_path', name='$uploadedfile' WHERE id='$userid1'")or die(mysql_error()); echo $value . ' uploaded<br>'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1257869 Share on other sites More sharing options...
voip03 Posted August 15, 2011 Share Posted August 15, 2011 thanks phpSensei pl let me know why w3school not good? Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1257891 Share on other sites More sharing options...
conan318 Posted August 16, 2011 Share Posted August 16, 2011 P.S, Stay away from w3schools. whats wrong with 3 schools? php manual is pretty good Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1257975 Share on other sites More sharing options...
phpSensei Posted August 16, 2011 Share Posted August 16, 2011 Even before this link this should have signal some warnings... http://w3fools.com/ Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1257976 Share on other sites More sharing options...
chintansshah Posted August 16, 2011 Share Posted August 16, 2011 I have this and it still does not update database, it uploads though........ <?php if($_POST['submit']){ // Where the file is going to be placed $target_path = "images/".$_SESSION['username']."/"; foreach ($_FILES["uploadedfile"]["name"] as $key => $value) { $uploadfile = $target_path . basename($_FILES[uploadedfile][name][$key]); //echo $uploadfile; if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$key], $uploadfile)) { $sql = mysql_query("UPDATE pics SET link='$target_path', name='$uploadedfile' WHERE id='$userid1'")or die(mysql_error()); echo $value . ' uploaded<br>'; } } } ?> Can you print your query on screen, if you get correct value then you should identify some other problem. I think, your query should be $sql = mysql_query("UPDATE pics SET link='".$target_path."', name='".$uploadedfile."' WHERE id='".$userid1."'")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/244294-file-upload/#findComment-1257986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.