Hello I have been writing a code for uploading a file to server
and store the values on the Mysql database
I am able to upload the file but facing issues in inserting the values in MYSQL server
I'm retrieving the values from an HTML Form and I'm successfully able to get the values and was able to echo from the file
Help is needed in inserting into table part of the code
<?php
require "test.php";
$username=$_POST['username'];
$filename=$_FILES['uploadedfile']['name'];
$language=$_POST['language'];
$comment=$_POST['comment'];
$user_id=$_POST['user_id'];
$filenames=$_FILES['uploadedfile']['name'];
$category=$_POST['category'];
$subcategory=$_POST['subcategory'];
$comment=$_POST['comment'];
$language=$_POST['language'];
$duration=$_POST['duration'];
$domain ='example.com/store/upload/';
$path=$domain.$category.'/'.$filenames;
//$path=$domain.$category.'/'.$filenames;
// Where the file is going to be placed
$target_path = "upload/".$category.'/';
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['uploadedfile']['name']);
echo "target_path: " .$target_path;
}
echo $filenames."<br />";
echo $domain."<br />";
echo $category."<br />";
echo $path."<br />";
echo $filename."<br />";
echo $language."<br />";
echo $comment."<br />";
echo $subcategory."<br />";
echo $duration."<br />";
echo $user_id."<br />";
// query
try{
$sql="INSERT INTO vup_file(filename,path,category,sub-category,user_id,comment,language,duration)
VALUES (:filename,:path,:category,:subcategory,:user_id,:comment,:language,:duration)";
$query=$conn->prepare($sql);
$query->execute(array(':filename'=>$filename,':path'=>$path,':category'=>$category,':subcategory'=>$subcategory,':user_id'=>$user_id,':comment'=>$comment,':language'=>$language,':duration'=>$duration));
echo 'Inserted';
}catch(PDOException $e)
{
echo 'ERROR OCCURED : '.$e->getMessage();
}
?>
Your sub-category column in your query, contains a hyphen. It needs to be escaped with backticks.
`sub-category`
Add $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened, which would have signaled the error.
SQL is evaluating it as a mathematical problem (minus).
Which translates to "sub" minus "category".
Another option you have is to simply rename your column to sub_category with an underscore, without the need to escape it.
An insight
If by chance your column is called sub_category instead of sub-category as shown in your query/question, then you will need to change it to sub_category in your query.
Or, if it's called subcategory. Only you know what your column is called.
Related
I know this is a long shot to ask but is their anyone that can show me how to upload an image to folder and image name to database? I have looked and everything I find is mysql. Mysql doesn't work for me I get many errors. Here is a code that I have but it will not work for me
<?php
$hostname_connect= "localhost";
$username_connect="torcdesi_barron7";
$password_connect= "Tazmania9292";
$database_connect="torcdesi_shirt";
// Create connection
$connect_solning = mysqli_connect($hostname_connect, $username_connect, $password_connect, $database_connect) or trigger_error(mysqli_error(),E_USER_ERROR);
mysqli_select_db($connect_solning ,$database_connect) or die (mysqli_error($connect_solning));
if($_POST)
{
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "/>";
}
else
{
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{ //move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file"] ["tmp_name"],"images/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$query_image = "insert into shirt_table (image) values ('".$_FILES['file']['name']."', 'display','')";
if(mysqli_query($query_image))
{
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}
}
}
?>
You have invalid query, you should use:
INSERT INTO table (field1, field2, field3) VALUES (value1, value2, value3)
You use instead:
INSERT INTO table (field1) VALUES (value1, value2, value3)
And empty quotes '' is a value too
In this php code I want to customize the image upload destination. with this php file, I have directory called uploads. I want to add all my uploaded images to this directory and store path in db. how can I do this?
<?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "image_upload";
$username_connect= "root";
$password_connect= "";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
#mysql_select_db($database_connect) or die (mysql_error());
if($_POST) {
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
if ($_FILES["file"]["error"] > 0) {
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
//move_uploaded_file function will upload your image. if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"])) {
// If file has uploaded successfully, store its name in data base
$query_image = "insert into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
if(mysql_query($query_image)) {
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
} else {
echo 'File name not stored in database';
}
}
}
}
}
?>
currently when I run the upload
I am getting warnings
Warning: move_uploaded_file(images/1409261668002.png): failed to open stream: No such file or directory in D:\xampp\htdocs\image-upload\index.php on line 29
Warning: move_uploaded_file(): Unable to move 'D:\xampp\tmp\php1C1F.tmp' to 'images/1409261668002.png' in D:\xampp\htdocs\image-upload\index.php on line 29
You must specify a correct path, the 'images/1409261668002.png' path doesn't exist if you dont create them and don't specify them.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))) { .... }
You must specify the absolute path
You can use below code:
$image=basename($_FILES['file']['name']);
$image=str_replace(' ','|',$image);
$tmppath="images/".$image;
if(move_uploaded_file($_FILES['file']['tmp_name'],$tmppath))
{...}
Let me know if you have any query/concern regarding this.
I am receiving a file via (sqlite file) and trying to connect to it using sqlite3. I tested this all using a sqlite file that was already on there on the server but now that im trying to do it when it gets uploaded I am trying to do operations to it.
I am getting the error that not table exists but I know it does because i open it sqlite manager and verify it is there. here is a snippet of code
$folder = '/var/www/uploads/';
if ( !file_exists($folder) ) {
mkdir ($folder, 0777);
}
$uploadfile = $folder.rand(). '-'. basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
$db = new SQLite3($uploadfile);
if ($db->lastErrorMsg() != 'not an error') {
print "Database Error: " . $db->lastErrorMsg() . "<br />";
}
$sql ="SELECT * FROM projects";
$result = $db->query($sql);
if ($db->lastErrorMsg() != 'not an error') {
print "Database Error: " . $db->lastErrorMsg() . "<br />";
}
this is my error
/var/www/uploads/79316479-db.sqlite
Database Error: no such table: projects
Im beginner as php programmer,so ive got various problem , one of them after I made an easy upload system,when I want to see the picture which I uploaded to the server,doesnt shown.
Here's the code:
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']); //the target path that the file will move
/*Moving the picture,to the server if successed the condition will be true*/
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
{
echo"התמונה הועלתה בהצלחה!";
echo"</br></br>";
echo $target_path;
$show_photo = "<img src='$target_path' alt='Picture' class='photo' />";
function AddToAllPhotosList()
{
$myFile = "photosorder.php"; //ListOfAllPhotos
$fileHander = fopen($myFile,'a') or die ("אין אפשרות לפתוח את הקובץ");
}
}
else
echo "Error.";
?>
The Line -"<img src='$target_path' alt='Picture' class='photo' />" , Doesn't shows the photo why ?
$show_photo = "<img src='$target_path' alt='Picture' class='photo' />";
Replace To:
echo "<img src='$target_path' alt='Picture' class='photo' />";
The HTML is being saved to variable $show_photo, but is not being echoed.
...
$show_photo = "";
echo $show_photo;
...
It looks like you aren't actually outputting the tags. You are currently storing them in the $show_photo variable, which is causing you problems. Either do
echo $show_photo;
after you've declared it, or replace line 10 with the following:
echo "<img src='$target_path' alt='Picture' class='photo' />";
Note - the type of quotes used are important!
Add an echo of $show_photo which should show the html you intend to show to appear.
I am trying to create a directory using PHP this works:
<?php
$uid = "user_615";
$thisdir = getcwd();
if(mkdir($thisdir ."/userpics/" . $uid , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
}
?>
but this does not work
<?php
session_start();
$uid = $_SESSION['username'];
$thisdir = getcwd();
if(mkdir($thisdir ."/userpics/" . $uid , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
}
?>
Yes the session variable is populated with the exact same thing as above 'user_615' so why would the second one be failing?
EDIT:
So I took the suggestion of #stefgosselin and re-designed the code to be
<?php
session_start();
$uid = $_SESSION['username'];
$thisdir = getcwd() . "/userpics" . $uid;
if(mkdir($thisdir , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
echo "Your thisdir Variable is:'" . $thisdir . "'" ;
}
?>
And the output is
Failed to create directory...Your thisdir Variable is:'/unified/b/bis/www.mysite.com/jou/userpics/user_615
Any other ideas on what would cause the a Session variable not to be able to used in creating a directory?
As a small tip, I would simply put all of $thisdir in a variable and check if the output of that adds up to the result you are expecting.
IE: Having $thisdir ."/userpics/" . $uid defined in a variable would give you the possibility to easily output and validate the argument value you are passing to mkdir.
Edit: Adjusted minor phrasing for better english translation. Sorry above wasn't clear, Wesley understood the simple point I was clumsily trying to make.