#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    16
    Rep Power
    0
    Does anyone know how I can accomplish the following:

    I have a php page that queryies a database and returns the results to the screen as an html table. I want to append two columns to that table, one called [Edit] and the other [Delete]. So, each row would have both [Edit] and [Delete] at the end. [Edit] needs to store the value of field1 and pass that to another page. [Delete] needs to ONLY delete the record you have chosen [Delete] for. Any ideas?

    Background: PHP3 on NT 4.0 with MS-SQL 7.0
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Seaside, CA
    Posts
    84
    Rep Power
    15
    <?php

    // create connection
    $connection = mysql_connect("server","user","password")
    or die("Couldn't make connection.");

    // select database
    $db = mysql_select_db("dbname", $connection)
    or die("Couldn't select database.");


    // create SQL statement
    $sql = "SELECT * FROM yourtable
    ORDER BY whatever";

    // execute SQL query and get result
    $sql_result = mysql_query($sql,$connection)
    or die("Couldn't execute query.");

    // start results formatting
    echo "<center><font size='3' face='helvetica,arial,verdana'>
    <b>Here's Your List<BR></b>
    <TABLE width=500 BORDER=0 cellpadding=4 cellspacing=3'>";
    echo "<TR bgcolor='LightSteelBlue'>
    <TH width=50><font size='-3' face='helvetica,arial,verdana'><b>ID</TH>
    <TH width=180><font size='-3'
    face='helvetica,arial,verdana'><b>Col2</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col3</TH>
    <TH width=70><font size='-3'
    face='helvetica,arial,verdana'><b>Options</TH>
    </TR>
    ";

    // format results by row
    while ($row = mysql_fetch_array($sql_result)) {
    $ID = $row["ID"];
    $category = $row["col2"];
    $class = $row["col3"];
    $ID = $row["ID"];
    $ID = $row["ID"];
    echo "<TR bgcolor='LightSteelBlue'>
    <TD><font size='-3' face='helvetica,arial,verdana'>$ID</TD>
    <TD><font size='-3'
    face='helvetica,arial,verdana'>$col2</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col3</TD>
    <TD ALIGN='center'><font
    face=arial,helvetica size=-3>
    <A HREF="modify.php?ID=$ID"><I>Modify</I></A>
    <A HREF="delete.php?ID=$ID"><I>Delete</I></A>
    </TD></TR>
    </TR>
    ";
    }

    echo "</TABLE></font></center>";

    // free resources and close connection
    mysql_free_result($sql_result);
    mysql_close($connection);

    ?>

    ------------------
    From the day we're born, we're running out of time.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    16
    Rep Power
    0
    Joel,

    Forgive this newbie. I understand most of what you have put, and I believe it is exactly what I am looking for. Could you help just a little more? I have edited my file to include some of your input:

    <TITLE>John's PHP3 SQL Tester Program</TITLE>
    <HEAD></HEAD>

    <BODY>
    <!-- sqltester_action.php3 -->

    <?php
    $server = "#####";
    $user = "#####";
    $pswd = "#####";

    $connect = mssql_pconnect($server, $user, $pswd);
    $query = stripSlashes($query);
    $result = mssql_query($query, $connect);

    ?>

    Results of query <B><?php echo($query); ?></B><HR><BR><BR>

    <?php
    //set a variable for error testing
    $data1 = mssql_num_rows($result);

    //temp code to help with debugging
    echo ("We found " . $data1 . " rows" . "<BR><BR>");


    // start results formatting
    echo "<center><font size='3' face='helvetica,arial,verdana'>
    <b>Here's Your List<BR></b>
    <TABLE width=500 BORDER=0 cellpadding=4 cellspacing=3'>";
    echo "<TR bgcolor='LightSteelBlue'>
    <TH width=50><font size='-3' face='helvetica,arial,verdana'><b>ID</TH>
    <TH width=180><font size='-3'
    face='helvetica,arial,verdana'><b>Col2</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col3</TH>
    <TH width=70><font size='-3'
    face='helvetica,arial,verdana'><b>Options</TH>
    </TR>
    ";

    // format results by row
    while ($row = mssql_fetch_array($result)) {
    $ID = $row["ID"];
    $category = $row["col2"];
    $class = $row["col3"];
    $ID = $row["ID"];
    $ID = $row["ID"];
    echo "<TR bgcolor='LightSteelBlue'>
    <TD><font size='-3' face='helvetica,arial,verdana'>$ID</TD>
    <TD><font size='-3'
    face='helvetica,arial,verdana'>$col2</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col3</TD>
    <TD ALIGN='center'><font
    face=arial,helvetica size=-3>
    <A HREF="modify.php?ID=$ID"><I>Modify</I></A>
    <A HREF="delete.php?ID=$ID"><I>Delete</I></A>
    </TD></TR>
    </TR>
    ";
    }

    echo "</TABLE></font></center>";

    ?>

    </BODY>
    </HTML>


    I have cut some stuff out, error checking, for simplicity sake. In the previous form I pass the query Select * from tblSurvey. There are 10 fields to this table. What needs to be done to your script to make this work?

    HOGI!
    (alternate email: hogihung@yahoo.com)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Seaside, CA
    Posts
    84
    Rep Power
    15
    hogi,

    Below is an expanded version for 10 columns. Note that you'll want to replace "col1" "col2" etc. with your own field names. Also, you'll want to adjust the table width and column widths (or take them out altogether) since these obviously won't work. Just left them in 'cause I'm lazy, I suppose.

    // start results formatting
    echo "<center><font size='3' face='helvetica,arial,verdana'>
    <b>Here's Your List<BR></b>
    <TABLE width=500 BORDER=0 cellpadding=4 cellspacing=3'>";
    echo "<TR bgcolor='LightSteelBlue'>
    <TH width=50><font size='-3' face='helvetica,arial,verdana'><b>Col1</TH>
    <TH width=180><font size='-3'
    face='helvetica,arial,verdana'><b>Col2</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col3</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col4</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col5</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col6</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col7</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col8</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col9</TH>
    <TH width=200><font size='-3'
    face='helvetica,arial,verdana'><b>Col10</TH>
    <TH width=70><font size='-3'
    face='helvetica,arial,verdana'><b>Options</TH>
    </TR>
    ";

    // format results by row
    while ($row = mssql_fetch_array($result)) {
    $col1 = $row["col1"];
    $col2 = $row["col2"];
    $col3 = $row["col3"];
    $col4 = $row["col4"];
    $col5 = $row["col5"];
    $col6 = $row["col6"];
    $col7 = $row["col7"];
    $col8 = $row["col8"];
    $col9 = $row["col9"];
    $col10 = $row["col10"];
    $ID = $row["ID"];
    $ID = $row["ID"];
    echo "<TR bgcolor='LightSteelBlue'>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col1</TD>
    <TD><font size='-3'
    face='helvetica,arial,verdana'>$col2</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col3</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col4</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col5</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col6</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col7</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col8</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col9</TD>
    <TD><font size='-3' face='helvetica,arial,verdana'>$col10</TD>
    <TD ALIGN='center'><font
    face=arial,helvetica size=-3>
    <A HREF="modify.php?ID=$ID"><I>Modify</I></A>
    <A HREF="delete.php?ID=$ID"><I>Delete</I></A>
    </TD></TR>
    </TR>
    ";
    }

    echo "</TABLE></font></center>";

    Hope this helps.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Seaside, CA
    Posts
    84
    Rep Power
    15
    Oh, almost forgot. Notice that the last two variables reference an "ID." So, one of the columns will need to be your record ID, or some primary or unique key. Change "ID" to whatever your key is called.

    You don't have to display the id in the html table but you do have to pull it out of the database for this example to be able to reference it.

    Also note that this "id" number is going to wind up in the url to your delete or modify script, as in "http://www.modify.php?ID=203," or whatever. If that number is sensitive, best not to use this.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    16
    Rep Power
    0
    Joel,

    Thanks for all your help. I'm going to take a crack at this, this morning. I do have a field called SID which is unique and my pk. It is not sensitive info, so no problem.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    16
    Rep Power
    0
    Joel,

    Just wanted to give another BIG THANKS! I appreciate your assistance and am glad that there are people like you available to help us newbies! This Forum has become an invaluable resource to my learning experience along with my PHP Programming book. U Da Man!

    HOGI!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Seaside, CA
    Posts
    84
    Rep Power
    15
    You're welcome. I've certainly got enough help here that I definitely need to give something back when I can. If anyone's interested, the script, which I use a lot, is a modified version of code found in "Professional PHP Programming," a great book.

    ------------------
    From the day we're born, we're running out of time.

Similar Threads

  1. Replies: 5
    Last Post: July 29th, 2011, 09:21 PM
  2. Select one resultrow from query result?
    By driz in forum PHP Development
    Replies: 1
    Last Post: February 10th, 2004, 10:59 AM
  3. saving query result as date
    By mega1 in forum Visual Basic Programming
    Replies: 0
    Last Post: January 28th, 2004, 04:21 AM
  4. Replies: 2
    Last Post: January 19th, 2004, 02:12 PM
  5. Replies: 12
    Last Post: January 15th, 2004, 05:25 PM

IMN logo majestic logo threadwatch logo seochat tools logo