Need help on a very simple PHP/MySQL function!

by 5 replies
7
I am extremely new to PHP so pardon my lack of knowledge!

Here's what I am trying to do. On one page, you enter a message into a form and after you submit the message, it takes you to a page with that message formatted and given a unique url (based on its table entry's id). I have no problem getting the form to input things into the database, but I don't know how to have it then spit out all of the form submissions on separate pages with unique urls.

For example, if you enter the first message in the form, you are then taken to mysite.com/1 and you can view your message. The second person would do it and would then be taken to the page mysite.com/2 and they can see their message.

I know this is super simple for all of you expert programmers, but I have just started learning PHP in the last few days.

Thanks for any help guys!
#programming #function #php or mysql #simple
  • Hmm grab the unqiue ID from the url and use that in a query to get the info you want from the DB, pretty simple.

    //this appended value on your url is called a $_GET variable
    mysite.com/index.php?id=2

    //this gets the id from the url, so now the $id variable is equal to 2 from the url
    $id = $_GET['id'];

    //now run a query, this assumes you are using PDO.. which you should be
    $stmt = $DBH->prepare("SELECT * FROM table WHERE id = :id");
    $stmt->bindParam(":id ", $id );
    $stmt->execute();
    $result = $stmt->fetch();

    this is in no way secure because someone could just manually type in the ID's into the URL, so you should hash the number with a unique salt to make it more secure (or if your already utilizing a authorization system just use that).
    • [1] reply
    • Sound like you want to do the following:

      1. have a form with data input elements
      2. after form is filled in and submitted all data go to a database
      3. submitting form person is redirected to another page with data they submitted on it

      If I got you right, items 1 and 2 are done, and you don't know how to implement part 3.

      If so, you need to pass to another page database record ID (the record you stored based on form submission). Based on the record ID, you pull out the database the info and display it on the final page.



      The above is just theory, but how you will do it, depends on how is done your previous coding (just pure php or any classes, how do you operate with database, what database type etc).
      • [ 1 ] Thanks
      • [1] reply

Next Topics on Trending Feed

  • 7

    I am extremely new to PHP so pardon my lack of knowledge! Here's what I am trying to do. On one page, you enter a message into a form and after you submit the message, it takes you to a page with that message formatted and given a unique url (based on its table entry's id). I have no problem getting the form to input things into the database, but I don't know how to have it then spit out all of the form submissions on separate pages with unique urls.