DeluXe Network Forums
March 22, 2010, 07:14:29 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Please do not add ads in your signature!
 
   Home   Help Search Members Login Register  
Pages: [1]
  Print  
Author Topic: Help with a Form  (Read 1435 times)
aquatsr
Sr. Member
****

Honor: 0
Offline Offline

United States United States

Posts: 436


View Profile
« on: June 29, 2008, 10:22:08 am »

So I'm trying to add some movies via a form to my database. Here's the form code:

Code:
echo "<center>";
  echo "Please enter the movie information, following the example below...<br /><br />\n";
  echo "<form action = \""; echo $_SERVER["PHP_SELF"];echo "\" method = \"post\" />\n";
  for($i = 0; $i < $quantity; $i = $i + 1)
  {
    echo "Title<br /><input type = \"text\" size = \"50\" value = \"Ocean's Thirteen\" name = \"title[$i]\" /><br />\n";
    echo "Director<br /><input type = \"text\" size = \"50\" value = \"Steven Soderbergh\" name = \"director[$i]\" /><br />\n";
    echo "Genre<br /><input type = \"text\" size = \"50\" value = \"Thriller, Crime, Comedy\" name = \"genre[$i]\" /><br />\n";
    echo "Release Year<br /><input type = \"text\" size = \"50\" value = \"2007\" name = \"year[$i]\" /><br />\n";
    echo "Length (minutes)<br /><input type = \"text\" size = \"50\" value = \"122\" name = \"length[$i]\" /><br />\n";
    echo "Language<br /><input type = \"text\" size = \"50\" value = \"English\" name = \"language[$i]\" /><br />\n";
    echo "Memorable Quote<br /><input type = \"text\" size = \"50\" value = \"You shook Sinatra's hand. You should know better.\" name = \"quote[$i]\" /><br />\n";
    echo "Description<br /><textarea rows = \"6\" cols = \"50\" \" name = \"description[$i]\">Danny Ocean again runs the game, so no rough stuff. No one gets hurt. Except for double-crossing Vegas kingpin Willy Bank (Al Pacino). Ocean's crew will hit him where it hurts: in his wallet. On opening night of Bank's posh new casino tower The Bank, every turn of a card and roll of the dice will come up a winner for bettors. And they'll hit him in his pride, making sure the tower doesn't receive a coveted Five Diamond Award. That's just the <i>start</i> of the flimflams. The boys are out to break The Bank. Place your bets!</textarea><br />\n";
    echo "Rating (1 is worst, 5 is best)<br /><input type = \"text\" size = \"50\" value = \"5\" name = \"rating[$i]\" /><br />\n";
    echo "Available to Borrow<br /><input type = \"text\" size = \"50\" value = \"Yes\" name = \"available[$i]\" /><br />\n";
    echo "Property of<br /><input type = \"text\" size = \"24\" value = \"My\" name = \"fname[$i]\" /> <input type = \"text\" size = \"24\" value = \"Name\" name = \"lname[$i]\" /><br /><br />-----<br /><br />\n";
  }
  echo "<input type = \"hidden\" value = \"$quantity\" name = \"quantity\" />\n";
  echo "<input type = \"hidden\" value = \"movieadd\" name = \"action\" />\n";
  echo "<input type = \"hidden\" value = \"one\" name = \"auth\" />\n";
  echo "<input type = \"submit\" value = \"Add Movie(s)\" />\n";
  echo "</form>\n";
  echo "</center>\n";

and here's the code for the form processing:

Code:
  $movie_title = htmlentities($_POST['title']);
  $movie_director = htmlentities($_POST['director']);
  $movie_genre = htmlentities($_POST['genre']);
  $movie_year = htmlentities($_POST['year']);
  $movie_length = htmlentities($_POST['length']);
  $movie_language = htmlentities($_POST['language']);
  $movie_quote = htmlentities($_POST['quote']);
  $movie_description = htmlentities($_POST['description']);
  $movie_rating = htmlentities($_POST['rating']);
  $movie_status = htmlentities($_POST['available']);
  $movie_owner_fname = htmlentities($_POST['fname']);
  $movie_owner_lname = htmlentities($_POST['lname']);
  $quantity = $_POST['quantity'];
 
  echo $movie_director[0];
 
  for($j = 0; $j < $quantity; $j = $j + 1)
  {

    $count = 0;
 
    if($movie_title[$j] == "") { $count++; }
    if($movie_director[$j] == "") { $count++; }
    if($movie_genre[$j] == "") { $count++; }
    if($movie_year[$j] == "") { $count++; }
    if($movie_length[$j] == "") { $count++; }
    if($movie_language[$j] == "") { $count++; }
    if($movie_quote[$j] == "") { $count++; }
    if($movie_description[$j] == "") { $count++; }
    if($movie_rating[$j] == "") { $count++; }
    if($movie_status[$j] == "") { $count++; }
    if($movie_owner_fname[$j] == "") { $count++; }
    if($movie_owner_lname[$j] == "") { $count++; }
 
    if($count > 0)
    {
      die("All fields are required. Go back and fill in the necessary information<br />\n");
    }

    $property_of = $fname[$j] . " " . $lname[$j];

    $query = "INSERT INTO `movies` (`movie_id`, `title`, `director`, `genre`, `released`, `length`, `language`, `quote`, `description`, `rating`, `property_of`, `date_added`, `available`) VALUES ('', '$movie_title[$j]', '$movie_director[$j]', '$movie_genre[$j]', '$movie_year[$j]', '$movie_length[$j]', '$movie_language[$j]', '$movie_quote[$j]', '$movie_description[$j]', '$movie_rating[$j]', '$property_of', NOW(), '$movie_status[$j]')";

    $result = mysql_query($query);
   
    if(!$result)
    {
      die("Could not query the database...");
 
    }
   
    $movies .= $movie_title[$j] . " " . "($movie_year[$j])<br />\n";

  }
 
  echo "Successfully added<br /><br />$movies<br />to the database!<br />\n";
  exit;

Now, the fields that are hidden are being sent over, but the actual movie information isn't being extracted. Can someone help me out as to why not? Thanks. (When I submit the form I get "All fields are required....etc" where count = 12.
Logged

jjb123
Global Moderator
Hero Member
*****

Honor: 2
Offline Offline

United States United States

Posts: 1493


View Profile WWW
« Reply #1 on: July 02, 2008, 04:05:36 pm »

Are you sure you can use arrays like that (In the field names)? That part isn't php, just the html part and you are assigning those variables to post objects that I don't think are arrays.
Logged

aquatsr
Sr. Member
****

Honor: 0
Offline Offline

United States United States

Posts: 436


View Profile
« Reply #2 on: July 02, 2008, 07:02:57 pm »

I'll double check and get back on that.
Logged

aquatsr
Sr. Member
****

Honor: 0
Offline Offline

United States United States

Posts: 436


View Profile
« Reply #3 on: July 03, 2008, 04:02:53 am »

I got it to work  Cheesy

Yes, arrays can be used like that.

I now have a different problem (see thread below)

http://dhost.info/forums/index.php/topic,11830.0.html
Logged

jjb123
Global Moderator
Hero Member
*****

Honor: 2
Offline Offline

United States United States

Posts: 1493


View Profile WWW
« Reply #4 on: July 03, 2008, 04:41:40 am »

That query syntax is a little different than what I normally use, try this:

Code:
mysql_query("INSERT INTO movies (movie_id,title,...) VALUES ('', '$movie_title[$j]','...');

Replace the dots with other variables, obviously. Besides that I am at a loss.
Logged

aquatsr
Sr. Member
****

Honor: 0
Offline Offline

United States United States

Posts: 436


View Profile
« Reply #5 on: July 03, 2008, 07:25:36 pm »

Problem was I wasn't escaping the ' in Ocean's Thirteen, along with ' in other places in the data query. I used htmlentities to convert ' to its html encoding.
Logged

Pages: [1]
  Print  
 
Jump to:  

 

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.234 seconds with 18 queries.