Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2003
    Location
    Belgium
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problems with form in newsscript

    ok I'm following a tutorial on a news script with php, but instead of using one table, I want to use more tables. so everytime $table comes in the script, I have to create a form to let the script know which table I'll use.
    This worked pretty well until I came to the edit page:

    list.php (edited)
    PHP Code:
    <form name="form2" method="post">

      <table width="100%" border="0" cellspacing="3" cellpadding="0">
        <tr> 
          <td colspan="2"><strong><font size="3">Posting form...</font></strong></td>
        </tr>
        <tr> 
          <td width="8%">Table:</td>
          <td width="92%"><input name='table' type='text' id='table' size='30'></td>
        </tr>
        
        <tr> 
          <td>&nbsp;</td>
          <td width="92%"><input type="submit" name="Submit" value="Submit"></td>
        </tr>
      </table>
    </form>
    <?php

    if($_POST['table'] == td_news) {

    include(
    'db.php');
    $table $_POST['table'];

    $q mysql_query("SELECT * FROM $table ORDER BY id, date DESC");



    echo 
    "<table with='100%' cellspacing='2'>";

    while(
    $r=mysql_fetch_array($q)){

    $id $r["id"];

    $title $r["title"];



    echo 
    "<tr><td><b>$title - options: <a href=\"edit.php?id=$id\" target=\"_blank\">Edit post</a> | 

    <a href=\"delete.php?id=$id\" target=\"_blank\">Delete Post</a></td></tr>"
    ;

    }

    echo 
    "</table>";

    mysql_close($cnx);
    }
    ?>
    ok I can see the list with news if I enter the table, but then comes edit.php:

    PHP Code:
    <?

    include('db.php');

    $table $_COOKIE['table'];

    $id $_GET['id'];

    $q mysql_query("SELECT * FROM $table WHERE id='$id'");

    $r mysql_fetch_array($q);

    $news $r["news"];

    echo 
    "<form action=\"editprocess.php?id=$id\" method=\"post\">

    <textarea id=\"news\" name=\"news\">$news</textarea><input type=\"submit\" 

    id=\"submit\"></form>"
    ;

    ?>
    I tried about everything to get the table variable here, even cookies

    can someone please help me?

    also: Instead of the type form I want to have a list form so you can just select the right table. how can I do that?

    help is appreciated
    g0liatH

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    620
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I would create a master table, with rows containing the names of the different tables.

    ie:

    Code:
    # Table: 'tbl_news_tables'
    #
    CREATE TABLE `tbl_news_tables` (
    
      `table_id` int(11) NOT NULL auto_increment,
    
      `table_name` varchar(255) default '',
    
      `table_description` varchar(255) default '',
    
        PRIMARY KEY  (`table_id`),
    
        UNIQUE KEY `table_id` (`table_id`)
    
    ) TYPE=MyISAM;

    Then I would do the following:

    list.php:
    PHP Code:
    <form name="form2" method="post">

      <table width="100%" border="0" cellspacing="3" cellpadding="0">
        <tr> 
          <td colspan="2"><strong><font size="3">Posting form...</font></strong></td>
        </tr>
        <tr> 
          <td width="8%">Table:</td>
          <td width="92%">
              <select name="news_table" id="news_table">
              <?php
          
                 
    include('db.php');
                 
    $query "SELECT * FROM tbl_news_tables";
             
                 
    $result mysql_query($query);
                 if (!
    $result) { die( "The following Error Occurred: " .mysql_error()); }

                 if (
    mysql_num_rows($result) != 0) {
             
                     while(
    $row mysql_fetch_assoc($result)) {
                         print 
    "<option value=\"" $row["table_name"] . "\">" $row["table_description"] . "</option>\n";
                     }
                 
                 } else {
                     print 
    "We were unable to pull data from the database.<br/>";
                 }
             
    ?>
          
              </select>
          </td>
        </tr>
        
        <tr> 
          <td>&nbsp;</td>
          <td width="92%"><input type="submit" name="Submit" value="Submit"></td>
        </tr>
      </table>
    </form>



    <?php


    if (isset($_POST["news_table])) {

        $news_table = $_POST["
    news_table"];
        $q = mysql_query("
    SELECT FROM $news_table ORDER BY iddate DESC");

        echo "
    <table with='100%' cellspacing='2'>";

        while($r=mysql_fetch_array($q)){

            $id = $r["
    id"];
            $title = $r["
    title"];

            echo "
    <tr><td><b>$title options: <a href="edit.php?id=$id&news_table=" $news_table "\" target=\"_blank\">Edit post</a> |
            <a href=\"delete.php?id=$id\" target=\"_blank\">Delete Post</a></td></tr>"
    ;

        }

        echo 
    "</table>";
    }

    mysql_close($cnx);
    ?>

    and for edit.php :
    PHP Code:
    <?php

        
    include('db.php');

        
    $id         $_GET['id'];
        
    $news_table $_GET['news_table'];



        
    /* generate the select box, populated with table names. defaults to current table */
        
    $select_news_table "<select name=\"news_table\">\n";
        
    $query_select_table mysql_query("SELECT * FROM tbl_news_tables");
        if (!(
    $query_select_table) { die( "The follwowing error occurred: " mysql_error()); }
        if (
    mysql_num_rows($query_select_table) != 0) {
        
           while (
    $row mysql_fetch_assoc($query_select_table)) {
               (
    $row["table_name"] == $news_table) ? $selected "SELECTED" $selected "";
       
               
    $select_news_table .= "<option value=\"" $row["table_name"] . "\" . $selected . ">" . $row["table_description"] . "</option>n";
           }
        }
        $select_news_table = "
    </select>";
        
        
        /* generate the edit form, including the table-select <select> field */
        $q = mysql_query("
    SELECT FROM $table WHERE id='$id'");
        
        if (!$q) { die( "
    The following Error Occurred" . mysql_error()); }

        if (mysql_num_rows($q) != 0) {

            $r = mysql_fetch_array($q);
            $news = $r["
    news"];

            echo "
    <form action="editprocess.php?id=$id&news_table=" $news_table "\" method=\"post\">\n";
            echo 
    "News Table: " $select_news_table "\n<br/>";
            echo 
    "News: <textarea id=\"news\" name=\"news\">$news</textarea>";
            echo 
    "<input type=\"submit\" id=\"submit\" value=\"Edit News\">\n
            echo "
    </form>";
                
        }

    ?>

    Now i didn't actually test any of that code, so don't be surprised if it throws up a few Parse errors, whatever.

    Basically what I did was:
    1. create a master index of all tables
    2. To generate the select box, simply grab all possible tables from the master index, setting the default on the edited page.
    3. To pass the table name to use, simply append it to the url, and access it via the $_GET superglobal



    I hope that helps. I'll try it whenever I get around to it... :-P

    notes:
    • You could also get the list of tables my executing a schema sql query, but that doesn't seem like the best method, to me.
    • Again, I didn't actually test any code.
    • I renamed a the generic "table" variable, as it was too non-descriptive
    • I added some mysql_error checking ---- VERY BASIC
    • I dropped the use of the cookie.



    DO NOT DELETE YOUR ORIGINALS...lol. At least, i wouldn't. You can if you want, but make sure you have backups, as I don't know what the exact consequences of running my code could be lol :-P

    HTH!

    -Celt

  • #3
    New Coder
    Join Date
    Aug 2003
    Location
    Belgium
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm thanks, but there are indeed some parse errors in the code. I tried to get them out but I don't really know how to fix this:

    PHP Code:
    print "<option value=\"" $row["table_name"] . "\">" $row["table_description"] . "</option>\n"
    and some more

    I'm kinda new to php so...

    and if I use that new table, then I have to edit all the other files too, right?
    g0liatH


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •