Jump to content

Insering Multple files


Go to solution Solved by requinix,

Recommended Posts

I have a code  that takes multiple text fields and 2 file uploads.

First before   people say i need to limit file types and images that i am working on once i get the problems i have now fixed

My issues

1. I am getting an undefined variable on line 31 even though all the names match with my db

2. The files arent uploading to server which i am testing on a private pc

3 I think this has to do  with issue of  the undefined variable problem but I want the  image links to go into the db under chat log and screenshot headings

the php code 

<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
Your SHG Player Name(required):<input type="text" name="commentfrom" required="required" /></br>
Your Email Address(required):  <input type="text" name="email" required="required" /></br>
Player Or Host this is about: <input type="text" name="about" /></br>
Do you Require a Director to contact you?:<p>
<input type="radio" name="reply" value="yes">Yes  <br/>
<input type="radio" name="reply" value="no">No <br/>
</p>
Please include your comments here:</br><textarea name="comments" rows="10" cols="25"></textarea></br>
Select Chatlog to upload:<input type="file" name="chatlog" id="chatlog"></br>
Select Screenshot to upload:<input type="file" name="screenShot" id="screenShot"></br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST["submit"])){
$hostname='localhost';
$username='root';
$password='';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=Directors",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$folder = "upload/";
$chatlog = $folder . basename($_FILES["chatlog"]["name"]);
$screenShot = $folder . basename($_FILES["screenShot"]["name"]);
$sql = "INSERT INTO comments (commentfrom, email, about, reply, comments, chatlog, screenShot  )
VALUES ('".$_POST["commentfrom"]."','".$_POST["email"]."','".$_POST["about"]."','".$_POST["reply"]."','".$_POST["comments"]."','".$_POST["chatlog"]."','".$_POST["screenShot"]."')";
if ($dbh->query($sql)) {
echo "New Record Inserted Successfully'";
}
else{
echo "Data not successfully Inserted.";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
</body>
</html>

Here is the table structure

s_fulltext.png ID commentfrom email about Reply Date Submitted comments chatlog screenshot NNN read Habs Read Directors comments   ID commentfrom  email  about  Reply  Date Submitted  comments  chatlog  screenshot  NNN read Habs Read  Directors comments 

 

Link to comment
https://forums.phpfreaks.com/topic/304923-insering-multple-files/
Share on other sites

Your problem isn't arrays. Your problem is that you missed out on, like, most of what the documentation is telling you to do:

- You aren't validating the upload, though you say you'll do that eventually (if I had a nickel...)

- You aren't moving the temporary file to a new location

- You are still using $_POST for, presumably, the file name

 

And you've got a big SQL injection problem.

 

- You aren't validating the upload, though you say you'll do that eventually (if I had a nickel...

 

 

ok i am still working on adding a file upload size limit but have   added an extension check

 

 

- You are still using $_POST for, presumably, the file name

yes I am trying to use that to post the file name to later link it back in a comments section

I am now having an issue with  an error replying

Notice: Undefined index: TR-2_Wham_Bam_-09042017_190057.rtf in C:\xampp\htdocs\form.php on line 51

where TR-2_Wham_Bam_-09042017_190057.rtf is the file name

and its still not uploading

 

here is the updated  code

<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
Your SHG Player Name(required):<input type="text" name="commentfrom" required="required" /></br>
Your Email Address(required):  <input type="text" name="email" required="required" /></br>
Player Or Host this is about: <input type="text" name="about" /></br>
Do you Require a Director to contact you?:<p>
<input type="radio" name="reply" value="yes">Yes  <br/>
<input type="radio" name="reply" value="no">No <br/>
</p>
Please include your comments here:</br><textarea name="comments" rows="10" cols="25"></textarea></br>
Select Chatlog to upload:<input type="file" name="chatlog" id="chatlog"></br>
Select Screenshot to upload:<input type="file" name="screenshot" id="screenshot"></br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST["submit"])){
$hostname='localhost';
$username='root';
$password='';
$dbh = new PDO("mysql:host=$hostname;dbname=Directors",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
 $chatlog= $_FILES['chatlog']['name'];
 $target_dir = "upload/";
 $target_file = $target_dir . basename($_FILES["chatlog"]["name"]);

 // Select file type
 $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

 // Valid file extensions
 $extensions_arr = array("rtf","doc","docx","txt");

 // Check extension
 if( in_array($imageFileType,$extensions_arr) ) 
 $screenshot= $_FILES['screenshot']['name'];
 $target_dir = "upload/";
 $target_file = $target_dir . basename($_FILES["screenshot"]["name"]);

 // Select file type
 $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

 // Valid file extensions
 $extensions_arr = array("jpg","jpeg","png","gif");

 // Check extension

$sql = "INSERT INTO comments (commentfrom, email, about, reply, comments, chatlog, screenshot  )
VALUES ('".$_POST["commentfrom"]."','".$_POST["email"]."','".$_POST["about"]."','".$_POST["reply"]."','".$_POST["comments"]."','".$_POST["$chatlog"]."','".$_POST["$screenshot"]."')";

}
$dbh = null;

?>
</body>
</html>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.