SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Hybrid View

  1. #1
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to contruct a simple .php file that will all people to view, edit and add files...depending on the $action variable...IE: script.php?action=add

    Here's the code I've got...I've got two problems...1: the add form won't work...it simply loads the original .php pages upon submission without sending the data to the DB...and 2: I'm trying to use an else-if command so that I can display a record from the DB in this format: script.php?action=view&id=1 - I can't quite get this to work...I have the command set like this:

    else if ( $action == "view" && $ID == "$ID" ) {stuff here}

    I'm aware of why this won't work, but can't figure out how to fix it...

    Here's my code in full:

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;?php

    // If the action is "view"...

    if ( $action == "view" ) {

    $db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
    if (!$db) {
    echo( "&lt;p&gt;Unable to connect to the " .
    "database server at this time.&lt;/p&gt;" );
    }

    mysql_select_db("mycodingdb",$db);
    if (! @mysql_select_db("mycodingdb") ) {
    echo( "&lt;p&gt;Unable to locate the " .
    "database at this time.&lt;/p&gt;" );
    }

    $sql = "SELECT * FROM programmers";

    // Perform the query

    $result = mysql_query($sql,$db);
    if (!$result) {
    echo( "&lt;P&gt;Error performing query: " .
    mysql_error() . "&lt;/P&gt;" );
    }

    while ($row = mysql_fetch_array($result)){

    $ID = $row["ID"];

    printf("&lt;b&gt;&lt;a href='http://www.mycoding.com/programmers.php?action=view&id=$ID'&gt;%s&lt;/a&gt;&lt;/b&gt; - %s years old" .
    " - %s experience&lt;br&gt;%s" .
    "&lt;br&gt;&lt;p&gt;\n", $row[1], $row[2], $row[3], $row[4]);
    }
    }


    else if ( $action == "view" && $ID == "$ID" ) {

    // Connect to and select the database

    $db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
    if (!$db) {
    echo( "&lt;p&gt;Unable to connect to the " .
    "database server at this time.&lt;/p&gt;" );
    exit();
    }
    mysql_select_db("mycodingdb",$db);
    if (! @mysql_select_db("mycodingdb") ) {
    echo( "&lt;p&gt;Unable to locate the " .
    "database at this time.&lt;/p&gt;" );
    exit();

    }

    // SQL query to retrieve all information
    // associated with any entries in the
    // database that have an ID equal to $id

    $sql = "SELECT * FROM programmers " .
    "WHERE ID=$id";

    // Perform the query

    $result = mysql_query($sql,$db);
    if (!$result) {
    echo( "&lt;P&gt;Error performing query: " .
    mysql_error() . "&lt;/P&gt;" );
    exit();
    }

    // Just in case there is more than one
    // entry with the given ID, loop through
    // "all of them" (there will usually only
    // be one) so that only the values from
    // the last one are retained.

    while ($row = mysql_fetch_array($result)) {
    $ID = $row["ID"];
    $Name = $row["Name"];
    $Age = $row["Age"];
    $Experience = $row["Experience"];
    $Languages = $row["Languages"];
    $Comments = $row["Comments"];
    }


    // Print out the values

    echo("&lt;b&gt;$Name&lt;/b&gt; - $Age years old - " .
    "$Experience experience&lt;br&gt;" .
    "&lt;b&gt;Languages: &lt;/b&gt;$Languages&lt;br&gt;" .
    "&lt;b&gt;Comments: &lt;/b&gt;$Comments&lt;br&gt;&lt;p&gt;\n");
    }


    // If the action equals "add"...

    else if ( $action == "add" ) {


    ?&gt;

    &lt;form action="&lt;?php $PHP_SELF ?&gt;" method=post&gt;

    Name: &lt;input type="Text" name="Name" size="30"&gt;&lt;br&gt;
    Age: &lt;input type="Text" name="Age" size="2"&gt;&lt;br&gt;
    Experience: &lt;input type="Text" name="Experience" value="in years" size="30"&gt;&lt;br&gt;
    Languages: &lt;input type="Text" name="Languages" size="30"&gt;&lt;br&gt;
    Comments: &lt;textarea name="Comments" rows="7" col="35"&gt;&lt;/textarea&gt;&lt;br&gt;
    Phone: &lt;input type="Text" name="Phone" size="15"&gt;&lt;br&gt;
    City: &lt;input type="Text" name="City" size="30"&gt;&lt;br&gt;
    Country: &lt;input type="Text" name="Country" size="20"&gt;&lt;br&gt;
    Date: &lt;input type="Text" name="Date" size="20"&gt;&lt;br&gt;

    &lt;input type="submit" name="submit" value="Submit"&gt;
    &lt;/form&gt;

    &lt;?php

    }


    // If the action equals "edit"...

    else if( $action == "edit" ) {

    echo( "This is the edit page!");

    }


    // Else...

    else {

    echo( "Welcome to the Programmers For Hire database. Please make a selection:&lt;p&gt;
    &lt;b&gt; --- &lt;a href='$PHP_SELF?action=view'&gt;View All Records&lt;/a&gt;&lt;/b&gt;&lt;br&gt;
    &lt;b&gt; --- &lt;a href='$PHP_SELF?action=add'&gt;Add a Record&lt;/a&gt;&lt;/b&gt;&lt;br&gt;
    &lt;b&gt; --- &lt;a href='$PHP_SELF?action=edit'&gt;Edit a Record&lt;/a&gt;&lt;/b&gt;&lt;br&gt;");

    }

    ?&gt;
    [/code]

    Anyone know what's wrong?

    Thanks.


    ------------------
    Chris Bowyer
    MyCoding.com: Join our mailing list for launch notification!
    "I'm not an insomniac, I'm a web designer."

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Cork, Ireland
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The else if for view and SID can never happen because the else will only occur when action does not equal view, and this can never be the case.
    e.g.

    if ($action = "view")
    Do Stuff
    else
    Do stuff when action is not equal to view

    You might want to use else if this way for the add form (I always use it this way, it's harder to make mistakes.)

    if (view)
    { if (view and SID)
    { viewAndSID;
    }
    else
    { viewRecord;
    }
    }
    else
    { if (add)
    { addRecord;
    }
    else
    { if (edit)
    { editRecord;
    }
    else
    { Normal page stuff
    }
    }
    }


    This should fix both problems if you do it like this. (I Hope)....

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Cork, Ireland
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry the formating went funny maybe this will look more readable, if it doesn't I'm sure you'll figure it out.

    if (view) {
    if (view and SID) {
    viewAndSID;
    }
    else {
    viewRecord;
    }
    }
    else {
    if (add) {
    addRecord;
    }
    else{
    if (edit) {
    editRecord;
    }
    else {
    Normal page stuff
    }
    }
    }

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks...that helped some of the problems...but I've got something else now...

    I can't just use the two if commands back to back...because in the case of the second if, both are true...so it displays the output from both...so I need a way to narrow the 1st if (if $action == "view") down so that it won't apply in any other scenario...I'm using this, to try to display the "view" page is the "action" variable is set to "view" and there is a blank ID variable (none specified in the URL):

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    if ( $action == "view" && empty($ID)) {
    [/code]

    I'm pretty sure this is how it works...but I get an SQL query error...any ideas?



    ------------------
    Chris Bowyer
    MyCoding.com: Join our mailing list for launch notification!
    "I'm not an insomniac, I'm a web designer."

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    if ($action=="view" && !isset($ID)) {


    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!

    [This message has been edited by kyank (edited June 29, 2000).]

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kevin: gave that a try, but I get the same problems as before...

    ...also, does anyone have any idea why the addpage won't work? That's the big problem for me: dead stuck.



    ------------------
    Chris Bowyer
    MyCoding.com: Join our mailing list for launch notification!
    "I'm not an insomniac, I'm a web designer."

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Cork, Ireland
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show the sode you have now....

    It might help me find the solution.....

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Cork, Ireland
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That should have been "code" not "sode".

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Cork, Ireland
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh! and you'll have to echo($PHP_SELF) in the form action you just have &lt;?php $PHP_SELF ?&gt;

    It should be &lt;?php echo($PHP_SELF); ?&gt;

  10. #10
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried changing that...and I made Kevin's change, but I still have the same two problems I mentioned at first...perhaps the PHP_SELF path won't work because at the page the form is at the URL is programmers.php?action=add ?

    Here's the code:

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;?php

    // If the action is "view"...

    if ($action=="view" && !isset($ID)) {

    $db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
    if (!$db) {
    echo( "&lt;p&gt;Unable to connect to the " .
    "database server at this time.&lt;/p&gt;" );
    }

    mysql_select_db("mycodingdb",$db);
    if (! @mysql_select_db("mycodingdb") ) {
    echo( "&lt;p&gt;Unable to locate the " .
    "database at this time.&lt;/p&gt;" );
    }

    $sql = "SELECT * FROM programmers";

    // Perform the query

    $result = mysql_query($sql,$db);
    if (!$result) {
    echo( "&lt;P&gt;Error performing query: " .
    mysql_error() . "&lt;/P&gt;" );
    }

    while ($row = mysql_fetch_array($result)){

    $ID = $row["ID"];

    printf("&lt;b&gt;&lt;a href='http://www.mycoding.com/phptests/programmers.php?action=view&id=$ID'&gt;%s&lt;/a&gt;&lt;/b&gt; - %s years old" .
    " - %s experience&lt;br&gt;%s" .
    "&lt;br&gt;&lt;p&gt;\n", $row[1], $row[2], $row[3], $row[4]);
    }
    }


    if ( $action == "view" && $ID == "$ID" ) {

    // Connect to and select the database

    $db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
    if (!$db) {
    echo( "&lt;p&gt;Unable to connect to the " .
    "database server at this time.&lt;/p&gt;" );
    exit();
    }
    mysql_select_db("mycodingdb",$db);
    if (! @mysql_select_db("mycodingdb") ) {
    echo( "&lt;p&gt;Unable to locate the " .
    "database at this time.&lt;/p&gt;" );
    exit();

    }

    // SQL query to retrieve all information
    // associated with any entries in the
    // database that have an ID equal to $id

    $sql = "SELECT * FROM programmers " .
    "WHERE ID=$id";

    // Perform the query

    $result = mysql_query($sql,$db);
    if (!$result) {
    echo( "&lt;P&gt;Error performing query: " .
    mysql_error() . "&lt;/P&gt;" );
    exit();
    }

    // Just in case there is more than one
    // entry with the given ID, loop through
    // "all of them" (there will usually only
    // be one) so that only the values from
    // the last one are retained.

    while ($row = mysql_fetch_array($result)) {
    $ID = $row["ID"];
    $Name = $row["Name"];
    $Age = $row["Age"];
    $Experience = $row["Experience"];
    $Languages = $row["Languages"];
    $Comments = $row["Comments"];
    }


    // Print out the values

    echo("&lt;b&gt;$Name&lt;/b&gt; - $Age years old - " .
    "$Experience experience&lt;br&gt;" .
    "&lt;b&gt;Languages: &lt;/b&gt;$Languages&lt;br&gt;" .
    "&lt;b&gt;Comments: &lt;/b&gt;$Comments&lt;br&gt;&lt;p&gt;\n");
    }

    // If the action equals "add"...

    else if ( $action == "add" ) {


    ?&gt;

    &lt;form action="&lt;?php echo($PHP_SELF); ?&gt;" method=post&gt;

    Name: &lt;input type="Text" name="Name" size="30"&gt;&lt;br&gt;
    Age: &lt;input type="Text" name="Age" size="2"&gt;&lt;br&gt;
    Experience: &lt;input type="Text" name="Experience" value="in years" size="30"&gt;&lt;br&gt;
    Languages: &lt;input type="Text" name="Languages" size="30"&gt;&lt;br&gt;
    Comments: &lt;textarea name="Comments" rows="7" col="35"&gt;&lt;/textarea&gt;&lt;br&gt;
    Phone: &lt;input type="Text" name="Phone" size="15"&gt;&lt;br&gt;
    City: &lt;input type="Text" name="City" size="30"&gt;&lt;br&gt;
    Country: &lt;input type="Text" name="Country" size="20"&gt;&lt;br&gt;
    Date: &lt;input type="Text" name="Date" size="20"&gt;&lt;br&gt;

    &lt;input type="submit" name="submit" value="Submit"&gt;
    &lt;/form&gt;

    &lt;?php

    }


    // If the action equals "edit"...

    else if( $action == "edit" ) {

    echo( "This is the edit page!");

    }


    // Else...

    else {

    echo( "Welcome to the Programmers For Hire database. Please make a selection:&lt;p&gt;
    &lt;b&gt; --- &lt;a href='$PHP_SELF?action=view'&gt;View All Records&lt;/a&gt;&lt;/b&gt;&lt;br&gt;
    &lt;b&gt; --- &lt;a href='$PHP_SELF?action=add'&gt;Add a Record&lt;/a&gt;&lt;/b&gt;&lt;br&gt;
    &lt;b&gt; --- &lt;a href='$PHP_SELF?action=edit'&gt;Edit a Record&lt;/a&gt;&lt;/b&gt;&lt;br&gt;");

    }

    ?&gt;
    [/code]

    Any ideas, ya'll?



    ------------------
    Chris Bowyer
    MyCoding.com: Join our mailing list for launch notification!
    "I'm not an insomniac, I'm a web designer."

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Cork, Ireland
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I try to put everything in one script normally. If programmers.php is another script then PHP_SELF will not work. If this is all one script there is no code to add the record to the DB.

    if ($action = "view" && !isset($ID)) {
    do ordinary view stuff
    }
    if ($action = "view" && isset($ID)) {
    do view stuf when $ID is set
    }
    if ($action = "add") {
    do add stuff
    }
    if ($action = "edit) {
    do edit stuff
    }

    or you could use else if statements. like this ..

    if ($action = "view" && !isset($ID)) {
    do ordinary view stuff
    }
    else if ($action = "view" && isset($ID)) {
    do view stuf when $ID is set
    }
    else if ($action = "add") {
    do add stuff
    }
    else ($action = "edit) {
    do edit stuff
    }

  12. #12
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Location
    Cork, Ireland
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first thing you do in your page should be connect to DB, before any if statements otherwise your writing code too many times, when once will suffice.

    Also before the first if statement put an
    if ($submit = "Submit") {
    Add new record to db
    }

    this will be checked each time the script is entered, note the value for each submit on you page will need to be different so you can differenciate between them.

  13. #13
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Variable names in PHP are case sensitive. Thus, if you pass ?id=5 in the query string of your URL, the value of $id will be 5, but $ID is not set to anything. Be sure you are using consistent cases in your variable names.

    If this still fails to resolve your problem, could you tell us what SQL error you are getting?


    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!


Bookmarks

Posting Permissions

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