0

Site Description: My site allows users to upload open source PHP web projects.

Hello I would like to know how I can generate a URL like sitename.com/projects.html?id=x after a user submits the form. How do I go about this? I don’t know where to start.

new-project.html: lets users upload new open source web projects.

<?php
    include_once('includes/header.php'); ?>
    <?php 
    include_once 'dbconnect.php';

    // fetch files
    $sql = "select filename from tbl_files";
    $result = mysqli_query($con, $sql);
    ?>
    <?php
    session_start();
    include_once "vendor/autoload.php";
    $page = new membership\Page(1);
    if ($page->isValid() == true) {
        ?>




    <div id="container">
      <div class="wrapper">
        <div id="content">
          <h2>New Project</h2>
          <p><center>

    <form action='upload.php' method='post' enctype='multipart/form-data'>
                            <legend>Select File to Upload:</legend>
                            <div class='form-group'>
    Title: <br /><input type='text' name='title' maxlength="255"/><br /><br />
    Description: <br /><textarea type='text' name='description' maxlength="2000"></textarea><br /><br />

                                <input type='file' name='file1' />
                            </div>
                            <div class='form-group'><br />
                                <input type='submit' name='name' value="Submit" class='btn btn-info'/>
                            </div>

                            <?php if (isset($_GET['st'])) { ?>
                                <div class='alert alert-danger text-center'>
                                    <?php
                                    if ($_GET['st'] == "success") {
                                        echo "File Uploaded Successfully!";
                                    } else {
                                        echo 'Invalid File Extension!';
                                    }
                                    ?>
                                </div>
                            <?php } ?>
                        </form></center>

    </p><?php } ?>

    <br /></div>
    </p>

        <div id="column">
          <div class="holder">
            <h2>Project Upload Rules</h2>
            <ul id="latestnews">
              This is this rules you must follow for uploading a project.<br /><br />
     - You must own the project / script.<br />
     - Must be 100% clean / safe.<br />
     - Code must be easy to read.<br />
     - No outdated code.<br />
    <br />
    If you don’t follow the rules your account who be banned. 
              <br />
          </p>

        <br /></p>
              </li>
            </ul>
          </div>
        </div>
        <br class="clear" />
      </div>
    </div>

    <?php include_once('includes/footer.php'); ?>

Any help would be nice.

EDIT:

projects.html

<?php include("includes/header.php"); ?>
<?php
include_once 'dbconnect.php';

// fetch files
$sql = "select filename, title, description from tbl_files LIMIT 4";
$result = mysqli_query($con, $sql);
?>


<div id="container">
  <div class="wrapper">
    <div id="content">
      <h2>Lastest 5 Projects <button style="float: right;"><a href="new-project.html">New Project</a></button></h2>
      <p><table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>File Name</th>
<th>Description</th>
                        <th>Download</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $i = 1;
                while($row = mysqli_fetch_array($result)) { ?>
                <tr>
                    <td><?php echo $i++; ?></td>
                    <td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
                    <td><a href="uploads/<?php echo $row['filename']; ?>" download>Download</td>
                </tr>
                <?php } ?>
                </tbody>
            </table>
</div>
</p>

    <div id="column">
      <div class="holder">
        <h2>Welcome!</h2>
        <ul id="latestnews">

          <li class="last"> <p><?php
    session_start();
   include_once "vendor/autoload.php";
    $page = new membership\Page(1);
    if ($page->isValid() == true){
         echo "Hello " . $_SESSION["username"] . "!<br /><br />

<a href='logout.html'>Logout</a>\n";
        } elseif ($page->isValid() == false) { echo "<center>Please <a href='login.php'>Log in</a> to share projects.<br /> <a href='register.php'>Need A Account?</a></center>";}
?><br />
      </p>

    <br /></p>
          </li>
        </ul>
      </div>
    </div>
    <br class="clear" />
  </div>
</div>

<?php
error_reporting(E_ALL & ~E_NOTICE);
include('includes/memberlistconfig.php');

// call this file only after database connection
require_once 'functions.php';
?>


<div id="container">
  <div class="wrapper">
    <div id="content">
      <h2>Categories</h2>
      <p>
        <div class="height20"></div>
        <?php echo $emsg; ?>

  <article>
          Click on one of the categories to see what’s inside.
          <ul>
            <?php
            $res = fetchCategoryTreeList();
            foreach ($res as $r) {
              echo  $r;
            }
            ?>
          </ul>
         </article>
</div></p>
<br class="clear" />
  </div>
</div>

  <?php include("includes/footer.php"); ?>

upload.php

<?php include('dbconnect.php'); ?>
<?php
//check if form is submitted
if (isset($_POST['submit']))
{
    $filename = $_FILES['file1']['name'];

    //upload file
    if($filename != '')
    {
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $allowed = ['zip', 'rar', 'php', 'html', 'sql'];

        //check if file type is valid
        if (in_array($ext, $allowed))
        {
            // get last record id
            $sql = 'select max(id) as id from tbl_files';
            $result = mysqli_query($con, $sql);
            if (count($result) > 0)
            {
                $row = mysqli_fetch_array($result);
                $filename = ($row['id']+1) . '-' . $filename;
            }
            else
                $filename = '1' . '-' . $filename;

            //set target directory
            $path = 'uploads/';

            $created = @date('Y-m-d H:i:s');
            move_uploaded_file($_FILES['file1']['tmp_name'],($path . $filename));

$title = '';
if(!empty($_POST['title']))
{
   $title = mysqli_real_escape_string($con, $_POST['title']);
}
$description = '';
if(!empty($_POST['description']))
{
   $description = mysqli_real_escape_string($con, $_POST['description']);
}
            // insert file details into database
            $sql = "INSERT INTO tbl_files(filename, created, title, description) VALUES('$filename', '$created', '$title', '$description')";
            mysqli_query($con, $sql);
            header("Location: new-project.html?st=success");
        }
        else
        {
            header("Location: new-project.html?st=error");
        }
    }
    else
        header("Location: new-project.html");
}
?>

2 Answers 2

1

Right now, your form submits to upload.php. So, just create upload.php and handle POST request and save the info in your Database then redirect user to sitename.com/projects.html?id=x. You can redirect users from PHP using header('location: /projects.html').

Now instead of projects.html you need projects.php (unless you have sufisticated routing system or server setup, .html pages are not parsed by PHP). You need PHP for /projects.php?id=3 because you want to dynamically generate that page.

To dynamically generate /project.php?id=3 page based on the provided ID. You can access the value of the ID using $_GET. It works like this:

if(isset($_GET['id']) && ctype_digit($_GET['id'])){
    # user have requested the page with a valid id (only number)
    # now you can use $_GET['id'] to generate this page dynamically.
    // your code here
    // you probably want to authenticate users too
    $requested_id = $_GET['id'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have it set up to run php inside .html files also I updated the post with projects.html and upload.php code.
0

When a user submits a new project, generate a random number and save it to the database as an id with the details of the project. So when someone goes to sitename.com/projects.php?id=x it will look up the id in the database and serve the details.

projects.php would look like this:

<?php
$id = $_GET['id'];
$sql = "SELECT * FROM projects WHERE `id`='{$id}'";

...
?>

6 Comments

I already have everything set up the database creates a id for the project and everything. Can you give me more details like how to get it up and working. Also do you want me to include projects.html code?
Unless your server is configured to process PHP codes inside a .html file, you should use a .php extension instead of .html . And yes it'll be helpful if you include the projects.html code
Ok I will edit the post in a second and yes I have it set up to run php inside .html files
Added projects.html and upload.php
My code for projects.html has a WHERE clause. Yours doesnt. You need to use the first 2 lines of my code to select the project the user is looking for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.