Jump to content

php forum posting and image uploading beside the post


prakash911

Recommended Posts

hey i am trying to create a forum where user can write description of their book they are trying to sell and upload image of this book and post it on a forum i got most of the code done, but i have no clue on how to use images with php and mysql if anyone can complete this code with it having images uploaded and posting it on the side of the forum the user posted it would be great. here is my code so far:

 

<?php

require("db_connect.php");

/* ------------------------------------------------------------------

  Web interface to a MySQL notes database using PHP.

  This version of notes.php doesn't use the Perl-style here documents

  -----------------------------------------------------------------*/

 

// Database table name

 

$database = "bookstore";

$table = "forum";

 

// Get form parameters and database primary key ($id). Non-existent

// values are mapped to the empty string

 

$choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : "";

$forum = isset($_REQUEST['content']) ? $_REQUEST['content'] : "";

$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "";

 

// Convert choice (button label) to lower case

 

$choice  = strtolower($choice);

 

// Escape some special characters such as quotes and double quotes

// before storing string in database

 

$forum = addslashes($forum);

 

// create an html page for the form or retrieve submitted data

 

start_html();

 

// Connect to the database and return a link

 

$db_link = db_connect($database);

 

// First time the script is run we just display an entry form

 

if ($choice == "")

{

  display_entry_form();

}

 

// If submit button was clicked insert new note in database table

 

elseif ($choice == "submit")

{

  insert_note($db_link, $table, $forum);

  display_entry_form();

  display_notes($db_link, $table);

}

 

// If display button was clicked display all the notes in the table

 

elseif ($choice == "display")

{

  display_entry_form();

  display_notes($db_link, $table);

}

 

// If search button was clicked display search results

 

elseif ($choice == "search")

{

  display_entry_form();

  display_search_hits($db_link, $table, $forum);

}

 

 

// If delete link was clicked delete the note from the table

 

elseif ($choice == "delete")

{

  if ( delete_note($db_link, $table, $id) )

  {

      display_entry_form();

      display_notes($db_link, $table);

  }

  else

  {

      display_entry_form();

      echo "<p><strong>A post with ID# $id was not found</strong></p>\n";

      display_notes($db_link, $table);     

  }

}

 

// If edit link was clicked display the edit form

 

elseif ($choice == "edit")

{

  if ( $row = select_note($db_link, $table, $id))

  {

      display_edit_form($row);

  }

  else

  {

      echo "<p><strong>A post with ID# $id was not found</strong></p>\n";

      display_notes($db_link, $table);

  }

}

 

// If update button on edit form was clicked so update the note in the table

 

elseif ($choice == "update")

{

  update_note($db_link, $table, $id, $forum);

  display_entry_form();

  display_notes($db_link, $table);

}

 

// Cancel button on edit form was clicked so discard any changes

 

elseif ($choice == "cancel")

{

  display_entry_form();

  display_notes($db_link, $table);

}

else

{

  echo "<p>Logic error, unknown choice: $choice</p>";

}

 

mysql_close(); // close DB connection

 

end_html();

exit;

 

// -------------------- End of main script ---------------------------

 

function start_html()

{

?>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">

 

<head>

<link href="books.css" rel="stylesheet" type="text/css">

<link href="reset.css" rel="stylesheet" type="text/css">

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>SC BookStore</title>

</head>

<body>

<div id="pageWrap">

  <div id="header">

  </div>

  <div id="navBar">

    <ul>

      <li><a href="Bookstore.php" title="Homw">Home</a></li>

      <li><a href="search.html" title="Search">Search</a></li>

      <li><a href="login.html" title="Forum">Login</a></li>

      <li class="end">                                                                                                                           

      <a href="signup.php" title="signup">Sign Up</a></li>

      <li></li>

    </ul>

  </div>

</div>

<h2>Forums</h2>

<?php

}

 

function end_html()

{

?>

</body>

</html>

<?php

}

 

/* ------------------------------------------------------------------

  Display all the notes in the database table

  -----------------------------------------------------------------*/

 

function display_notes($db_link, $table)

{

  echo "<hr>\n<h2>Current Notes</h2>\n<hr>\n";

 

  $query =

            "SELECT id, DATE_FORMAT(time, '%W, %M %D, %Y at %r') AS time2,"

          . "content FROM $table ORDER BY time DESC";

 

  $result = mysql_query($query, $db_link) or die("Query failed");

  $num_rows = mysql_num_rows($result);

 

 

  if ( $num_rows == 0 )

  {

      echo "The database is empty";

  }

  else

  {

      while ( $row = mysql_fetch_assoc($result))

      {

        display_note($row);

      }

  }

  mysql_free_result($result);

}

 

/* ----------------------------------------------------------------------

  Display table entries that match search criteria

  ---------------------------------------------------------------------*/

 

function display_search_hits($db_link, $table, $search_string)

{

  $search_string = trim($search_string);

 

  if ($search_string == "")

  {

      echo "<p><strong>Please enter a search term.</strong></p>\n";

      return;

  }

 

  $query =

        "SELECT id, DATE_FORMAT(time, '%W, %M %D, %Y at %r') AS time2,"

      . "content FROM $table WHERE content LIKE '%$search_string%' "

      . "ORDER BY time DESC";

 

  $result = mysql_query($query, $db_link) or die("Query failed");

  $num_rows = mysql_num_rows($result);

 

  echo "<h2>Search found ",

      $num_rows == 0 ? "nothing" : "$num_rows note",

      $num_rows > 1 ? "s" : "",

      " matching \"", stripslashes($search_string), "\"</h2>\n<hr>\n";

 

  if ($num_rows > 0)

  {

      while ( $row = mysql_fetch_assoc($result) )

      {

        display_note($row);

      }

  }

  mysql_free_result($result);

}

 

/* ----------------------------------------------------------------------

  Display Image on forum

  ---------------------------------------------------------------------*/

function display_image($db_link, $table,

 

/* -------------------------------------------------------------------------

  Display a row from the database table as an HTML table

  ------------------------------------------------------------------------*/

 

function display_note($row)

{

  // extract data from table row

 

  $id = $row['id'];

  $forum = htmlspecialchars(stripslashes($row['content']));

  $time = $row['time2'];

 

  // store command and record id in the url as a query string

 

  $this_url = $_SERVER['PHP_SELF'];

  $edit_url = "$this_url?choice=edit&id=$id";

  $delete_url = "$this_url?choice=delete&id=$id";

 

  // Each note is a table with one row and three columns

 

?>

<table width="100%">

<tr valign="top">

<td width="80%"><b>Time:</b> <?php echo $time?>

<pre style="margin-left:2em;margin-top:0.5em;margin-bottom:0.5em">

<?php echo $forum?>

</pre>

</td>

<td width="10%"><a href="<?php echo $edit_url?>">[ Edit ]</a></td>

<td width="10%"><a href="<?php echo $delete_url?>">[ Delete ]</a></td>

</tr>

</table>

<hr>

<?php

}

 

 

 

/*-------------------------------------------------------------------------

  Display a blank data entry form

  ------------------------------------------------------------------------*/

 

function display_entry_form()

{

  $script_url = $_SERVER['PHP_SELF']; # url of this script

 

?>

  <form method="post" action="<?php echo $script_url?>" enctype="multipart/form-data">

      Enter Your Details, Description and Overview of the book your selling.

  <p>

  <textarea name="content" rows="5" cols="50" wrap="hard"></textarea>

  </p>

  <p>

  <input type="submit" name="choice" value="Submit">

  <input type="submit" name="choice" value="Display">

  <input type="submit" name="choice" value="Search">

 

/*-------------------------------------------------------------------------

  THIS IS WHERE I NEED TO IMPLEMENT IMAGE UPLOAD AND THE PROCESSING SHOULD

  TAKE PLEASE IN ONE OF FUNCTIONS SEPERATLY BELOW.

  ------------------------------------------------------------------------*/

 

 

  </p></form>

<?php

 

}

 

/* ------------------------------------------------------------------------

  Display form for editing a note

  $row is the table row to edit

  -----------------------------------------------------------------------*/

 

function display_edit_form($row)

{

  // extract data from the array $row (record).

  // Slashes added by addslashes are now removed

  // Also it is necessary to convert html tags to their entities

  // (For example, < is converted to <)

 

  $script_url = $_SERVER['PHP_SELF'];

  $id = $row['id'];

  $forum = htmlspecialchars(stripslashes($row['content']));

 

  // Display the form with the current note so that the content can be edited.

  // We keep track of the id number using a hidden field.

 

?>

<strong>Edit Your Post Here. </strong><br />

<form method="POST" action="<?php echo $script_url?>">

<input type="hidden" name="id" value="<?php echo $id?>">

<textarea name="content" rows="5" cols="50" wrap="hard">$forum</textarea>

<p>

<input type="submit" name="choice" value="Update">

<input type="submit" name="choice" value="Cancel">

</p>

</form>

<?php

}

 

// DATABASE FUNCTIONS ----------------------------------------------------

 

/* -----------------------------------------------------------------------

    Delete a note given the note id

    Returns true if sucessful

  ----------------------------------------------------------------------*/

 

function delete_note($db_link, $table, $id)

{

  $query = "DELETE FROM $table WHERE id = '$id'";

  @mysql_query($query, $db_link);

  $result = mysql_affected_rows($db_link);

  return $result > 0;

}

 

/* -----------------------------------------------------------------------

    Insert a note into the table.

  ----------------------------------------------------------------------*/

 

function insert_note($db_link, $table, $forum)

{

  if (trim($forum) != "")

  {

      $query = "INSERT INTO $table SET time = NOW(), content = '$forum'";

      $result = mysql_query($query, $db_link)

                  or die("<p>Insertion failed</p>\n");

  }

}

 

 

/* ------------------------------------------------------------------------

    Select a note with a given id (find).

    Return the record found (row)

  -----------------------------------------------------------------------*/

 

function select_note($db_link, $table, $id)

{

  $query = "SELECT * FROM $table WHERE id = '$id'";

  $result = mysql_query($query, $db_link)

                or die("<p>Select note failed</p>\n");

  $row = mysql_fetch_assoc($result);

  return $row;

}

 

/* -----------------------------------------------------------------------

    Update note content

  ----------------------------------------------------------------------*/

 

function update_note($db_link, $table, $id, $forum)

{

  $query = "UPDATE $table SET content = '$forum' WHERE id = '$id'";

  $result = mysql_query($query, $db_link)

                or die("<p>Update note failed</p>\n");

}

 

?>

Link to comment
Share on other sites

Using the advice above, you can make up your own naming convention for your images.  I sometimes name them based on their unique ID in the database.  So when the user uploads the file, and it's the correct type, etc, then you can add a row to the database, and then use php's move_uploaded_file() to save the file with the name according to your naming convention,

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.