Jump to content

multipart/form-data


guym

Recommended Posts

Hi all I've been having issues making this code, I'm a huge noob and have been watching youtube videos for the past couple of days and trying to create a website with a database of my movies.(I know there are programs for this but I would like to make my own) This site is only going to be running on localhost. I'm having issues uploading my info(mainly the image for Poster) and if anyone could look at my code/database setup and help me out that would be great

First here is a screenshot of what my database looks like.

http://i.imgur.com/14DQTCh.jpg

 

Next here is my index.html file

<html>

<head><title>Add Movie or Tv show</title></head>
<body>


<form enctype="multipart/form-data" id="myForm" action="addedinfo.php" method="post">
<h1 align="center"><strong>Add Movie or TV Show to Database</strong></h1>
<p> </p>
<p>Movie Name:
  <input type="text" name="Movie_Name" /><br />
</p>
<p><label> Poster</label>
          <input type="FILE" name="Poster"> ><br>
  </p>
<p>Genre:
  <select multiple name="Genre" id="Genre">
    <option value="All" style="text-indent: 0px;" selected="selected">All</option>
    <option value="Action" style="text-indent: 0px;">Action</option>
    <option value="Adventure" style="text-indent: 0px;">Adventure</option>
    <option value="Animation" style="text-indent: 0px;">Animation</option>
    <option value="Biography" style="text-indent: 0px;">Biography</option>
    <option value="Comedy" style="text-indent: 0px;">Comedy</option>
    <option value="Crime" style="text-indent: 0px;">Crime</option>
    <option value="Documentary" style="text-indent: 0px;">Documentary</option>
    <option value="Drama" style="text-indent: 0px;">Drama</option>
    <option value="Family" style="text-indent: 0px;">Family</option>
    <option value="Fantasy" style="text-indent: 0px;">Fantasy</option>
    <option value="Film-Noir" style="text-indent: 0px;">Film-Noir</option>
    <option value="History" style="text-indent: 0px;">History</option>
    <option value="Horror" style="text-indent: 0px;">Horror</option>
    <option value="Mystery" style="text-indent: 0px;">Mystery</option>
    <option value="Romance" style="text-indent: 0px;">Romance</option>
    <option value="Sci-Fi" style="text-indent: 0px;">Sci-Fi</option>
    <option value="Short" style="text-indent: 0px;">Short</option>
    <option value="Sport" style="text-indent: 0px;">Sport</option>
    <option value="Thriller" style="text-indent: 0px;">Thriller</option>
    <option value="War" style="text-indent: 0px;">War</option>
    <option value="Western" style="text-indent: 0px;">Western</option>
  </select>
</p>
<p>
  <label>IMDB Rating:</label>
  <select name="IMDB_Rating" id="IMDB_Rating">
    <option value="0" style="text-indent: 0px;" selected="selected">All</option>
    <option value="9.9" style="text-indent: 0px;">10</option>
    <option value="9" style="text-indent: 0px;">9+</option>
    <option value="8" style="text-indent: 0px;">8+</option>
    <option value="7" style="text-indent: 0px;">7+</option>
    <option value="6" style="text-indent: 0px;">6+</option>
    <option value="5" style="text-indent: 0px;">5+</option>
    <option value="4" style="text-indent: 0px;">4+</option>
    <option value="3" style="text-indent: 0px;">3+</option>
    <option value="2" style="text-indent: 0px;">2+</option>
    <option value="1" style="text-indent: 0px;">1+</option>
  </select>
  </p>
<p>Quality:
  <label class="label">Quality:</label>
  <select name="Quality" id="Quality">
    <option value="All" style="text-indent: 0px;" selected="selected">All</option>
    <option value="1080p" style="text-indent: 0px;">1080p</option>
    <option value="3D" style="text-indent: 0px;">3D</option>
    <option value="480p" style="text-indent: 0px;">480p</option>
    <option value="720p" style="text-indent: 0px;">720p</option>
    <option value="DVD" style="text-indent: 0px;">DVD</option>
    <option value="HDRip" style="text-indent: 0px;">HDRip</option>
  </select>
</p>
<p>
  Year: 
  <input type="text" name="Year" /><br />
  
  </p>
<p>Trailer:
  <input type="text" name="Trailer" /><br />
  
</p>
<button id="sub">Submit</button>
</form>


<span id="result"></span/


</body>
</html>

As you can see I'm trying to post the following information Movie Name, Poster of the movie, Genre, Rating, Quality, Year, and the Trailer

Next I have my connection script db.php
<?php
$conn = mysql_connect("localhost", "root", "spencer");
$db = mysql_select_db('movies');
?>

Next my addinfo.php

<?php
include_once('db.php');

$Movie_Name = $_POST['Movie_Name'];
$Poster = $_POST['Poster'];
$Genre = $_POST['Genre'];
$IMDB_Rating = $_POST['IMDB_Rating'];
$Quality = $_POST['Quality'];
$Year = $_POST['Year'];
$Trailer = $_POST['Trailer'];

if (mysql_query ("INSERT INTO movieinfo VALUES ('','$Movie_Name','$Poster', '$Genre', '$IMDB_Rating', '$Quality', '$Year', '$Trailer')"))
echo "Successfull";
else
echo "Failed"
?>
and lastly I have a little javascript my_script.js
$("#sub").click( function() {
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info){ $("#result").html(info);
});
clearInput();
});

$("#myForm").submit( function() {
return false;
});

function clearInput() {
$("#myForm :input").each( function() {
$(this).val('');
});
}

The error I get is:

Notice: Array to string conversion in C:\xampp\htdocs\movies\addedinfo.php on line 12
Successfull

When I look at the database I notice a couple of things: 1 the image is 5B so I don't think it uploads right, along with in the Genre only seems to pick up the last selection of the multiple selection 
 

My overall goal is to make a website that I can use to have a database of all my movies and tv shows on that I can add too and search through to find something to watch. So right now I'm making code to upload movies to the database, later I will make a site that will show all of the different movies I have and will be searchable through the movie name, genre, rating, quality and or year.

 

 I appreciate any help very much!

Edited by fenway
code tags
Link to comment
Share on other sites

Sorry I'll do that next time. I know I'll be asking for more help with this project, and I really do appreciate help I receive. 

What I would like to do is to if possible is have the information I submit fill in a blank template page. 

That way when I submit the movie info it would create a new page for that movie with the information that's being submitted.  Would you be able to point me in the right direction to learn how I should go about this? 

Also I'm still returning an error of 

Notice: Array to string conversion in C:\xampp\htdocs\movies\addedinfo.php on line 12
Successfull

and when I look in my database I notice I only get one selection from Genre  where I can select multiple in the selection box. 

 

Thank you again.

 

Link to comment
Share on other sites

I've updated my addinfo.php - I'm not receiving any error, now however I'm also not getting anything sent to my database now. 
 

 <?php
include_once('db.php');
 
$Movie_Name = $_POST['Movie_Name'];
$Poster = $_FILES['Poster'];
if (isset ($_POST['Genre'])){
$skillsArray = $_POST['Genre'];
$i = 0;
foreach ($skillsArray as $key => $value) {
$i++;
}
exit();
}
$IMDB_Rating = $_POST['IMDB_Rating'];
$Quality = $_POST['Quality'];
$Year = $_POST['Year'];
$Trailer = $_POST['Trailer'];
 
if (mysql_query ("INSERT INTO movieinfo VALUES ('','$Movie_Name','$Poster', '$Genre', '$IMDB_Rating', '$Quality', '$Year', '$Trailer')"))
echo "Successfull";
else
echo "Failed"
?>
Edited by guym
Link to comment
Share on other sites

you need to slow down and work on one thing at a time. your last posted code isn't actually checking if your form was submitted at all and contains an exit() statement that is killing your code at that point, which is probably why you aren't getting any output.

 

1) to start with, you need a conditional statement around all your form processing code so that it only executes when a form has been submitted. because you are trying to upload a file, the $_POST and $_FILES arrays can be empty due to one particular upload size error and you need to test some other value besides a $_POST or $_FILES element to detect that a form was submitted. the best way is to check if($_SERVER['REQUEST_METHOD'] == 'POST'){ all your form processing code goes here... }

 

2) next, for ALL of your form fields, you must validate that they contain expected values before you use any of the data. for the upload file field you will also need to verify that the upload worked before you can test or use any of the uploaded file information. you should report back to the visitor exactly what is wrong with the data they submitted for each form field.

 

3) your insert query statement should be of the form that list the columns. this will prevent a bunch of problems, will let you leave out the id field value, and let anyone looking at your code know what your table/query is actually doing - INSERT INTO table_name (columns,...) VALUES (values,...)

 

4) you either need to escape your string data before putting it into the query or use a prepared query, to prevent query errors and sql injection.

 

5) lastly, the mysql_ database library is depreciated starting in php5.5 and all new code, or anyone learning how to do this, should be using either the mysqli or PDO database libraries.

 

since uploading files seems to be a popular subject lately, see the following thread for info about uploaded files - http://forums.phpfreaks.com/topic/282596-how-to-handle-file-from-multiple-upload/?do=findComment&comment=1452535

Edited by mac_gyver
Link to comment
Share on other sites

as to your Notice: Array to string conversion ... error message, the reason it went-a-way is because you added an exit(); statement to stop your code from running. the error is because $_FILES['Poster'] is an array and you are trying to put it into your query statement. you are going to need to research more about what happens when php receives an uploaded file before you will be able to do this in your code. here's the php.net section on processing uploaded files (reading the whole section would be useful) - http://us1.php.net/manual/en/features.file-upload.php

Link to comment
Share on other sites

Thanks for the help, I'm trying to learn more before I ask anything else. 

I believe I have everything saving in the database right now. 

Now I'm just trying to learn how to make my submit insert the information saved into a template page with place holders for that information, so each time new data is submitted a new page is created with all that movie data that was just added. 

Link to comment
Share on other sites

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.