Dynamically generate update and delete form in PHP.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phopman
    New Member
    • Aug 2007
    • 26

    Dynamically generate update and delete form in PHP.

    Hi there! Thanks for all the help so far. When my boss said I should be up to date as soon as possible, he meant last week. So I got a good rollicking when I came to work. Nice way to start my day....

    Back to my problem:

    I have a Text in Mysql: The code for fetching it in php is:
    [PHP]
    $id = intval( mysql_real_esca pe_string( $_GET['id'] ));
    $query = "SELECT * FROM jos_issues WHERE jos_issues.cont entid= '$id' ORDER BY text desc";
    $result = mysql_query($qu ery);

    while($row= mysql_fetch_arr ay($result)) {
    echo"$row[text]<BR>";
    }
    [/PHP]

    What I am looking for is a way to
    a) press a link for update
    b) dynamically print out a form for doing that.

    All I am after is a way to edit and delete all the texts that are in my database. They are all linked to a content $id. So my table looks like this: id | text | contentid

    I tried this code, but all I get is a very blank page with nothing on:

    [PHP]<? include("c:/db.inc"); error_reporting (E_ALL);
    ini_set('displa y_errors', '1');
    //$id = intval( mysql_real_esca pe_string( $_GET['id'] ));
    $id = 12; echo "id = $id";

    $query = "SELECT * FROM jos_issues WHERE jos_issues.cont entid= '$id'";
    $result = mysql_query($qu ery);
    $num = mysql_numrows($ result);
    //mysql_close();

    $i = 0;
    while($i<$num) {
    $text = mysql_result($r esult,$i,"text" );
    <form action="updated .php" method="post">
    <input type="hidden" name="ud_id" value="<? echo $id; ?>">
    Text: <input type="text" name="ud_text" value="<? echo $text; ?>"><br>
    <input type="Submit" value="Update">
    </form>

    ++$i;
    }
    ?>[/PHP]

    Let me know if you can help me with this one. There's no lunch for me today. I have to have finished at one o' clock, or I am screwed...


    Thanks in advance.


    Phopman
    Last edited by phopman; Aug 30 '07, 08:50 AM. Reason: error in code, and my name :-)
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Are you trying to open a separate page for each "text links"?
    for a example
    Link1
    Link2
    Link3

    Once I select Link2 the corresponding id will pass with the URL String to another Page and from that the dynamic form will print for do the updation for the Text Link.

    If this is the thing Why this " ++$i" ?
    And use the relative path for the include function, Why you using this OS related File path here? This is a web Application. So if you are want to deploy it to remote server, can expect the same paths ? No way. Always use the Web Server Related Paths. Here you can use the relative path to the db.php file.

    Comment

    • prosad
      New Member
      • Jul 2007
      • 27

      #3
      hi,

      you are using 'POST' for your form method instead of 'GET'. 'GET' will send through url for update on processing to your dynamically generated form.
      Advise: Always use
      Code:
      <?php
      for your php opening tags.

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by prosad
        hi,

        you are using 'POST' for your form method instead of 'GET'. 'GET' will send through url for update on processing to your dynamically generated form.
        Advise: Always use
        Code:
        <?php
        for your php opening tags.
        <?Php
        ?>
        Its a good point. Nice work.
        But, How come this GET and POST came here ?
        As I can see first code snippet in the original post is working.
        Then once click the links Form will print.(Hope we can fix It)
        Then only we need to use the POST or GET to submit the values to updated.php. So what is the wrong with using POST here?

        Comment

        • phopman
          New Member
          • Aug 2007
          • 26

          #5
          I solved the problem, albeit not in a very smooth and good way to be honest.

          How do you use include so its not a OS adddress?

          The way I did it was:

          made three files:

          1. List up all the content, add an update button
          2. Called update. Loaded a white, ugly page with a text area, and a submit button. Captured the content from page 1.
          3. update_ac Updates the database with the data from update.php.

          I found the code on the Internet, and it's working, even though it's seriously ugly and so far from elegant it hurts.

          But at least the boss isn't that pissed on me anymore. I hope.

          Phopman

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Glad you got it working.

            This
            [CODE=php]include("c:/db.inc");[/CODE]

            With this.
            [CODE=php]include 'db.inc';[/CODE]

            Change this according to document relative path.
            but php can read the Files in out side the web root.

            Good luck with your projects!

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              I usually think that office romances are a bad idea; they usually end badly, and then they cause disruptions in morale and productivity.

              Oh, you said UP to date. Gotcha :)

              [EDIT: (see OP)]

              Comment

              • phopman
                New Member
                • Aug 2007
                • 26

                #8
                Originally posted by ajaxrand
                Glad you got it working.

                This
                [CODE=php]include("c:/db.inc");[/CODE]

                With this.
                [CODE=php]include 'db.inc';[/CODE]
                Thanks for the tip. Will do that from now on.

                Phopman

                Comment

                • prosad
                  New Member
                  • Jul 2007
                  • 27

                  #9
                  Originally posted by phopman
                  Thanks for the tip. Will do that from now on.

                  Phopman
                  pbmods

                  I offered the suggestion on "GET' cos wat i thought from 1st post he wanted to update by passing a url visibly which i assume cant be done using 'POST'.
                  What I am looking for is a way to
                  a) press a link for update
                  i guess link is not meant as HREF link
                  correction? thnks.

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Heya, Prosad.

                    You can pass _GET variables even though your form is posting:
                    [code=html]
                    <form ... method="post" action="script. php?var1=value1 &var2=value2 ">
                    <input ... />
                    .
                    .
                    .
                    </form>
                    [/code]

                    Note that you can't pass _POST variables in the address bar, since that defeats half of the purpose of being able to post in the first place.

                    Comment

                    • phopman
                      New Member
                      • Aug 2007
                      • 26

                      #11
                      Thanks for all the help so far. The Project is nearing completion. I have decided to put update.php in a popup window, and it's working nicely!

                      Comment

                      Working...