Need help on a very simple PHP/MySQL function!

5 replies
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!
#function #php or mysql #simple
  • Profile picture of the author Andrew H
    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).
    Signature
    "You shouldn't come here and set yourself up as the resident wizard of oz."
    {{ DiscussionBoard.errors[7844768].message }}
    • Profile picture of the author viescripts
      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).
      {{ DiscussionBoard.errors[7849595].message }}
      • Profile picture of the author zmorris
        Originally Posted by viescripts View Post

        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).
        You are right. That's exactly what I'm trying to do.

        Here is how I entered everything into the database:

        $sql="INSERT INTO $tbl_name(number, message)VALUES(SUBSTRING(MD5(RAND()) FROM 1 FOR 10), '$message')";
        $result=mysql_query($sql);

        I figured out how to get them all to display on a page, but I'd like to know how to get just the one that person entered. Sorry if I'm bad at explaining what I'm trying to do.

        Thanks,
        James
        {{ DiscussionBoard.errors[7858188].message }}
        • Profile picture of the author viescripts
          Originally Posted by zmorris View Post

          I figured out how to get them all to display on a page, but I'd like to know how to get just the one that person entered. Sorry if I'm bad at explaining what I'm trying to do.
          Friend Andrew H, explained pretty well what you should do.
          I don't know what to add here, just trying to explain it again:

          1. your members fill in a form on page: fillform.php
          2. after form submission you send them to a page: saveform.php
          (btw saveform.php can also be the same fillform.php or can be a different page).

          on this page you save data to mysql:
          $sql="INSERT INTO $tbl_name(number, message)VALUES(SUBSTRING(MD5(RAND()) FROM 1 FOR 10), '$message')";
          $result=mysql_query($sql);

          this table ($tbl_name) must have a id (you can name it anyway) field, that is unique and is PRIMARY KEY. - Make sure it's exactly unique and Primary - this is mentioned in phpmyadmin table structure.

          after your line:
          $result=mysql_query($sql);
          add
          $id = mysql_insert_id();

          3. when redirecting to display page, redirect to: display.php?id=$id

          4. Follow instructions of: Andrew H
          {{ DiscussionBoard.errors[7858476].message }}

Trending Topics