Jump to content

PLEASE HELP with uploading image...


dan_ormerod

Recommended Posts

Hi,

 

This is my first post of PHP Freaks and your help would be greatly appreciated.

 

I am currently creating a pinball fansite and I am having a really big problem. I have a page where users can input a pinball machine into my mysql database. Below is the code for this.

 

The question I am asking is how do I edit this script so that I can upload an image with it?

 

I have gone through various tutorials for days and days and days but cant seem to make it work with the code I have below.

 

PLEASE CAN YOU HELP??

 

I have the following code for creating the pinball machine...

 

<?php
//create_topic.php
include 'connect.php';
include 'header.php';

echo '<h2>Create a Pinball Machine</h2>';
if($_SESSION['signed_in'] == false)
{
   //the user is not signed in
   echo 'Sorry, you have to be <a href="signin.php">signed in</a> to create a pinball machine.';
}
else
{
   //the user is signed in
   if($_SERVER['REQUEST_METHOD'] != 'POST')
   {   
      //the form hasn't been posted yet, display it
      //retrieve the categories from the database for use in the dropdown
      $sql = "SELECT
               cat_id,
               cat_name,
               cat_description
            FROM
               categories_pinballmachines";
      
      $result = mysql_query($sql);
      
      if(!$result)
      {
         //the query failed, uh-oh :-(
         echo 'Error while selecting from database. Please try again later.';
      }
      else
      {
         if(mysql_num_rows($result) == 0)
         {
            //there are no categories, so a topic can't be posted
            if($_SESSION['user_level'] == 1)
            {
               echo 'You have not created categories yet.';
            }
            else
            {
               echo 'Before you can post a topic, you must wait for an admin to create some categories.';
            }
         }
         else
         {
      
            echo '<form method="post" action="",enctype="multipart/form-data">
               Name: <input type="text" name="topic_subject"><br />
               Type:';
            
            echo '<select name="topic_cat">';
               while($row = mysql_fetch_assoc($result))
               {
                  echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
               }
            echo '</select><br />';   
               
            echo 'Manufacturer: <input type="text" name="post_manufacturer"><br />
            Release Date: <input type="text" name="post_releasedate"><br />
            Number of Players: <input type="text" name="post_numberofplayers"><br />
            Production: <input type="text" name="post_production"><br /><br /><br />
            Concept by: <input type="text" name="post_conceptby"><br />
            Design by: <input type="text" name="post_designby"><br />
            Art by: <input type="text" name="post_artby"><br />
            Dots/Animation by: <input type="text" name="post_dotsanimationby"><br />
            Mechanics by: <input type="text" name="post_mechanicsby"><br />
            Music by: <input type="text" name="post_musicby"><br />
            Sound by: <input type="text" name="post_soundby"><br />
            Software by: <input type="text" name="post_softwareby"><br />
            
               <input type="submit" value="Create Pinball Machine" />
             </form>';
             
             
         }
      }
   }
   else
   {
      //start the transaction
      $query  = "BEGIN WORK;";
      $result = mysql_query($query);
      
      if(!$result)
      {
         //Damn! the query failed, quit
         echo 'An error occured while creating your topic. Please try again later.';
      }
      else
      {
   
         //the form has been posted, so save it
         //insert the topic into the topics table first, then we'll save the post into the posts table
         $sql = "INSERT INTO 
                  topics_pinballmachines(topic_subject,
                        topic_date,
                        topic_cat,
                        topic_by)
               VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
                        NOW(),
                        " . mysql_real_escape_string($_POST['topic_cat']) . ",
                        " . $_SESSION['user_id'] . "
                        )";
                
         $result = mysql_query($sql);
         if(!$result)
         {
            //something went wrong, display the error
            echo 'An error occured while inserting your data. Please try again later.<br /><br />' . mysql_error();
            $sql = "ROLLBACK;";
            $result = mysql_query($sql);
         }
         else
         {
            //the first query worked, now start the second, posts query
            //retrieve the id of the freshly created topic for usage in the posts query
            $topicid = mysql_insert_id();
            
            $sql = "INSERT INTO
                     posts_pinballmachines(post_manufacturer,
                          post_releasedate,
                          post_numberofplayers,
                          post_production,
                          post_conceptby,
                          post_designby,
                          post_artby,
                          post_dotsanimationby,
                          post_mechanicsby,
                          post_musicby,
                          post_soundby,
                          post_softwareby,
                          post_date,
                          post_topic,
                          post_by)
                  VALUES
                     ('" . mysql_real_escape_string($_POST['post_manufacturer']) . "',
                                                   '" . mysql_real_escape_string($_POST['post_releasedate']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_numberofplayers']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_production']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_conceptby']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_designby']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_artby']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_dotsanimationby']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_mechanicsby']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_musicby']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_soundby']) . "',
                                                  '" . mysql_real_escape_string($_POST['post_softwareby']) . "',
                          NOW(),
                          " . $topicid . ",
                          " . $_SESSION['user_id'] . "                 
                            )";
                     
            $result = mysql_query($sql);
            
            if(!$result)
            {
               //something went wrong, display the error
               echo 'An error occured while inserting your post. Please try again later.<br /><br />' . mysql_error();
               $sql = "ROLLBACK;";
               $result = mysql_query($sql);
            }
            else
            {
               $sql = "COMMIT;";
               $result = mysql_query($sql);
               
               //after a lot of work, the query succeeded!
               echo 'You have succesfully created <a href="topic_pinballmachines.php?id='. $topicid . '">your new pinball machines</a>.';
            }
         }
      }
   }
}

include 'footer.php';
?>

and the following code is where the information is displayed...

<?php
//create_cat.php
include 'connect.php';
include 'header.php';

$sql = "SELECT
         topic_id,
         topic_subject
      FROM
         topics_pinballmachines
      WHERE
         topics_pinballmachines.topic_id = " . mysql_real_escape_string($_GET['id']);
         
$result = mysql_query($sql);

if(!$result)
{
   echo 'The topic could not be displayed, please try again later.';
}
else
{
   if(mysql_num_rows($result) == 0)
   {
      echo 'This topic doesn′t exist.';
   }
   else
   {
      while($row = mysql_fetch_assoc($result))
      {
         //display post data
         echo '<h2>PINBALL MACHINES</h2>';
         echo '<table>
               <tr>
                  <th colspan="2">' . $row['topic_subject'] . '</th>
               </tr>';
      
         //fetch the posts from the database
         $posts_sql = "SELECT
                  posts_pinballmachines.post_topic,
                  posts_pinballmachines.post_manufacturer,
                  posts_pinballmachines.post_releasedate,
                  posts_pinballmachines.post_numberofplayers,
                  posts_pinballmachines.post_production,
                  posts_pinballmachines.post_conceptby,
                  posts_pinballmachines.post_designby,
                  posts_pinballmachines.post_artby,
                  posts_pinballmachines.post_dotsanimationby,
                  posts_pinballmachines.post_mechanicsby,
                  posts_pinballmachines.post_musicby,
                  posts_pinballmachines.post_soundby,
                  posts_pinballmachines.post_softwareby,
                  posts_pinballmachines.post_date,
                  posts_pinballmachines.post_by,
                  users.user_id,
                  users.user_name
               FROM
                  posts_pinballmachines
               LEFT JOIN
                  users
               ON
                  posts_pinballmachines.post_by = users.user_id
               WHERE
                  posts_pinballmachines.post_topic = " . mysql_real_escape_string($_GET['id']);
                  
         $posts_result = mysql_query($posts_sql);
         
         if(!$posts_result)
         {
            echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
         }
         else
         {
         
            while($posts_row = mysql_fetch_assoc($posts_result))
            {         
echo '
               
                       <tr class="topic-post">
                       <td class="post-content2"><b>Manufacturer: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_manufacturer'])) . '</br></tr>
                     <td class="post-content2"><b>Release Date: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_releasedate'])) . '</br></tr>
                     <td class="post-content2"><b>Number of Players: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_numberofplayers'])) . '</br></tr>
                     <td class="post-content2"><b>Production: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_production'])) . '</br></tr>
                     <td class="post-content2"><b>Concept by: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_conceptby'])) . '</br></tr>
                     <td class="post-content2"><b>Design by: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_designby'])) . '</br></tr>
                     <td class="post-content2"><b>Art by: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_artby'])) . '</br></tr>
                     <td class="post-content2"><b>Dots/Animation by: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_dotsanimationby'])) . '</br></tr>
                     <td class="post-content2"><b>Mechanics by: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_mechanicsby'])) . '</br></tr>
                     <td class="post-content2"><b>Music by: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_musicby'])) . '</br></tr>
                     <td class="post-content2"><b>Sound by: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_soundby'])) . '</br></tr>
                     <td class="post-content2"><b>Software by: </b>
                     <td class="post-content">' . htmlentities(stripslashes($posts_row['post_softwareby'])) . '</br></tr>
                     
                     
                     </td>
                    </tr>';
            }
         }
         
         if(!$_SESSION['signed_in'])
         {
            echo '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to comment. You can also <a href="signup.php">sign up</a> for an account.';
         }
         else
         {
            //show reply box   
            echo '<tr><td colspan="2"><h2>Comment on this Pinball Machine:</h2><br />
               <form method="post" action="reply.php?id=' . $row['topic_id'] . '">
                  <textarea name="reply-content"></textarea><br /><br />
                  <input type="submit" value="Submit reply" />
               </form></td></tr>';
         }
         
         //finish the table
         echo '</table>';
      }
   }
}


include 'footer.php';
?>

 

Any questions please don't hesitate to ask?

 

Thanks in advance

 

Dan

 

MOD EDIT: code tags added.

Link to comment
https://forums.phpfreaks.com/topic/238150-please-help-with-uploading-image/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.