3

I am working on classified website in which user will post his ads to sell something. It is basically my first web project. I have problem in making self creating pages in which when user post his ad it will generate an HTML page automatically and link and little glimpse of his ad show on a proper well designed another ad page.

Here I have described my problem properly:

  1. When user presses submit ad button an html page will be created automatically and user will be directed to that page (how to create that page)?
  2. How to place link of ad page on the top of other previous ads those are too on self-created page dynamically?

Here is random code of inserting and displaying database on the page:

<!DOCTYPE html>
<html>
<body>

<form action="zain.php" method="post">
    Topic: <input type="text" name="topic"><br />
    <br />
    Name: <input type="text" name="name"><br /><br />
    Attendance: <input type="text" name="attendance"><br />
    <br />
    <input type="reset" name="reset">
    <input type="submit" name="submit" value="Go">
</form>

<?php

$user = 'root';
$password = 'zz224466';
$db = 'Zain';

// Create connection
$conn = mysqli_connect('localhost', $user, $password, $db);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "";
mysqli_select_db($conn, "zain");
$insert = "INSERT INTO lectures (Topic,Name,Attendence) VALUES('$_POST[topic]','$_POST[name]','$_POST[attendance]')";
mysqli_query($conn,$insert);


///////////Write records on the Screen//////////////
$sql = "SELECT * FROM lectures";
$myData = mysqli_query($conn,$sql);
while($record=mysqli_fetch_array($myData)){

    echo  $record['Topic']. " "
     ."  ". $record['Name']." ".$record['Attendence'];
    echo "<br>";
}
mysqli_close($conn);

?>

</body>
</html>

Can you please show me some hints to solve my problem?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
  • That's not the way to do that. When you create a post in stackoverflow a html is not created. The data is saved on the database and then is showed on the web using a "template" page. So you should save the info you want to show somewhere and then use the same page to show all the different adds. – Gonzalo Dec 12 '15 at 14:39
  • But how to create that template page having different link each time dynamically? – Zain Farooq Dec 12 '15 at 14:42
  • Have you ever seen urls like this `site.com?page.php?id=5`? The site is passing the page id on the url, this is known as `GET` mehotd. You can retrieve that id and search for the page in the DB to show it. – Gonzalo Dec 12 '15 at 14:48
  • Gonzalo.. Looking good.. I'll try this – Zain Farooq Dec 12 '15 at 14:51

3 Answers3

1

As gonzalo commented, you don't need to create an html page. Rather save the details into DB and create a dynamic (.php) file; and let this file do all the work. ie. Pass on the id of the ad to that file; fetch the details from the db and present it to the user using html markup. That being said, I strongly recommend to use a framework.

musafar006
  • 931
  • 11
  • 22
1

After adding lecture you can get id of record in database by function

$id = mysqli_insert_id($conn);

You can prepare new page with id on it for example:

page.com/lecture.php?id=3

Where id is your lecture id to generate. Under this site you should prepare php code to show exactly this lecture.

To redirect to new lecture page you can check this post: How to make a redirect in PHP?

Links to other ads can be easly added by changing id in url.

PS. You shouldn't put $_POST parameters directly to database, there is a possibility of SQL injection, use http://php.net/manual/en/mysqli.real-escape-string.php

Community
  • 1
  • 1
pmaniora
  • 76
  • 5
  • You are saying that I should not use POST method Overall in this scenario? – Zain Farooq Dec 12 '15 at 14:55
  • no, POST method is ok, but if you put this variables directly without checking there is a security issue. You can read about it here: https://en.wikipedia.org/wiki/SQL_injection – pmaniora Dec 12 '15 at 15:01
0

If at all you want to create fresh page each time (though i too wouldn't recommend that), you can store your data into variables and create a page with fopen() and fwrite() and then add header('location:file.ext') to redirect it to that url

user3526204
  • 509
  • 5
  • 22