SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 45
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me turn words into code!

    I have the following code, and I need to make the following words/code to be translated into the code provided in the bottom. THANKS GUYS.

    If you press submit UPDATE authorize SET LockStatus ='LOCKED' WHERE ='$_SESSION['user_name'])'. Else - Select LockStatus from authorize is = to LOCKED show <input type="submit" value="Edit" disabled="disabled" /> Else If - Select LockStatus from authorize is not equal to LOCKED, then show <input type="submit" value="submit" name="submit">

    PHP Code:
    <?php 
    $db_host 
    "localhost"
    $db_user "lefteh"
    $db_pwd "1234"
    $db_name "MyDB"
    mysql_connect($db_host$db_user$db_pwd); 
    mysql_select_db($db_name); 
    ?> 
    <html> 
    <head> 
    <title>My first MySQL form submission</title> 
    </head> 
    <body> 
    <?php 
    if (!isset($_POST['submit'])) { 
    ?> 
    <form action="" method="post"> 
    Name: <input type="text" name="name"><br> 
    Favorite Color: <input type="text" name="color"><br> 
    <input type="submit" name="submit" value="Submit!"> 
    </form> 
    <?php 
    } else { 
    $name $_POST['name']; 
    $color $_POST['color']; 
    mysql_query("INSERT INTO `colors` (name, favoriteColor) VALUES ('$name', '$color')"); 
    echo 
    "Success! Your favourite colour has been added!"

    ?> 
    </body> 
    </html>

  2. #2
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you think you could show the tables so we know what we're working with? I'm a bit confused on what you're trying to do.

    Also, you might want to sanitize data going into the database.

    So:
    $name = mysql_real_escape_string($_POST['name']);
    $color = mysql_real_escape_string($_POST['color']);

  3. #3
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    which table are you referring too?

  4. #4
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    authorize and colors

    --edit--

    Also, I noticed you had the form action not set to anything. If you want that page to take care of that as well, you should:

    action="<?php echo $_SERVER['PHP_SELF']; ?>"

    But yeah, could you give a little information on what's going on? It's hard to figure what to do if the details are a bit fuzzy

  5. #5
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    authorize

    columns: username, password, lock

    colors

    columns: name, favoritecolors

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    something like:
    PHP Code:
    <?php
    /*
    If you press submit UPDATE authorize SET LockStatus ='LOCKED' WHERE ='$_SESSION['user_name'])'. Else - Select LockStatus from authorize is = to LOCKED show <input type="submit" value="Edit" disabled="disabled" /> Else If - Select LockStatus from authorize is not equal to LOCKED, then show <input type="submit" value="submit" name="submit">
    */

    if(isset($_POST['submit'])) {
        
    $sql mysql_query("
            update
                authorize
            set
                LockStatus = 'LOCKED'
            where
                user_name = '"
    $_SESSION['user_name'] ."'
        "
    );
        

        
    } else {
        
    $sql mysql_query("
            select * from
                authorize    
            where
                user_name = '"
    $_SESSION['user_name'] ."'
            "
    );
            
        
    $rows mysql_fetch_assoc($sql);
            
        if(
    $rows['LockStatus'] == 'LOCKED') {
            echo 
    '<input type="submit" value="Edit" disabled="disabled" />';
        } else {
            echo 
    '<input type="submit" value="submit" name="submit" />';
        }
            
    }


    ?>
    perhaps....
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, from the code in the first post:
    Code:
    UPDATE authorize SET LockStatus ='LOCKED' WHERE ='$_SESSION['user_name'])'. Else - Select LockStatus from authorize is = to LOCKED
    That would mean you're trying to update a field named "LockStatus" to the text 'LOCKED'. From the post right, I can't see where that field would be in the authorize table. You do have a field called lock, so you would have to either change lock to LockStatus, or vice versa in your code. Also, in the first post you have "favoriteColor" and in the post above you have "favoritecolors" (plural). Check through to make sure all of the names line up between your fields in the tables and your queries (SELECT, UPDATE, etc) to the database.

    Again, this would be a bit easier if you explained the context. I can get the user's favorite color part, but not sure how you want the lock part to function, and what circumstances you're wanting for this.

  8. #8
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    spikez,

    I'm a little confused as per if(isset($_POST['submit'])) { if this is listed in the above code can I place this where the submit button needs to be in the table as well?

  9. #9
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When a user fills out a form, and the method is set to "post", when they click the submit button (or whatever you've named it), all of the fields in that form are set to the $_POST variable, and accessible through $_POST['fieldname'].

    In the code above, the submit has a name of 'submit', so if you want to check if the user clicked the button, you check if there is a variable set in $_POST named 'submit' (which would be $_POST['submit']).

    isset() checks if there is a variable named whatever you pass to it.
    So
    PHP Code:
    if ( isset($_POST['submit']) ) { 
    Means "If the submit button was clicked, then do this..."

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    personally I would rearrange the code so that you are checking FOR the submit rather than checking IF IT'S NOT THERE.

    so, pseudo code
    Code:
    if(isset($_POST['submit'])) {
    
    // run all the queries
    // so the code I gave you goes here
    
    } else {
    
    // display the form
    
    }
    I dont think there is any technical reason but it is just a little more logical.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it doesn't submit

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    post your code
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    //DB Stuff goes here
    <html> 
    <head> 
    <title>My first MySQL form submission</title> 
    </head> 
    <body> 
    <?php 
    if (isset($_POST['submit'])) { 

    $name mysql_real_escape_string($_POST['name']);
    $color mysql_real_escape_string($_POST['color']);

    $maha_gogo mysql_query("INSERT INTO `colors` SET `name` = '$name', `favoritecolor` = '$color' ");

    echo 
    "Your Colors have been added";
    } else { 
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
    Name: <input type="text" name="name"><br> 
    Favorite Color: <input type="text" name="color"><br> 
    <input type="submit" name="submit" value="Submit!"> 
    </form> 

    <?php 
    // End Else
     
    }
    ?> 
    </body> 
    </html>
    Change the field names after SET to the actual names you're using since I'm still not sure.

  14. #14
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 
    $db_host 
    "localhost"
    $db_user "lefteh"
    $db_pwd "1234"
    $db_name "MyDB"
    mysql_connect($db_host$db_user$db_pwd); 
    mysql_select_db($db_name); 
    ?> 
    <html> 
    <head> 
    <title>My first MySQL form submission</title> 
    </head> 
    <body> 
    <?php 
    if (!isset($_POST['submit'])) { 
    ?> 
    <form action="" method="post"> 
    Name: <input type="text" name="name"><br> 
    Favorite Color: <input type="text" name="color"><br> 
    <?php 

    if(isset($_POST['submit'])) { 
         
    $sql mysql_query(
            update 
                authorize 
            set 
                LockStatus = 'LOCKED' 
            where 
                user_name = '"
    $_SESSION['user_name'] ."' 
        "
    ); 
         

         
    } else { 
        
    $sql mysql_query(
            select * from 
                authorize     
            where 
                user_name = '"
    $_SESSION['user_name'] ."' 
            "
    ); 
             
        
    $rows mysql_fetch_assoc($sql); 
             
        if(
    $rows['LockStatus'] == 'LOCKED') { 
            echo 
    '<input type="submit" value="Edit" disabled="disabled" />'
        } else { 
            echo 
    '<input type="submit" value="submit" name="submit" />'
        } 
             



    ?>

    </form> 
    <?php 
    } else { 
    $name $_POST['name']; 
    $color $_POST['color']; 
    mysql_query("INSERT INTO `colors` (name, favoriteColor) VALUES ('$name', '$color')"); 
    echo 
    "Success! Your favourite colour has been added!"

    ?> 
    </body> 
    </html>

  15. #15
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't really know what I'm talking about but I think there is some confusion with the 2 if (isset($_POST['submit'])) {

  16. #16
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    use what Chousho posted and see if it works
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  17. #17
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    unfortunately that doesn't seem to be working either.

  18. #18
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have phpMyAdmin? If you do, could you click on the 2 tables I asked about before, and for each one go to the Export tab, click Go, then copy and paste that so we can see?

    I'm still a bit confused on the table set up, and if you could do that we would be able to see exactly what would need to be done (or just about exactly, haha).

    Also, if you used the code above, did you add the database info where I commented to (first line), and wrapped it in php tags?


    It seems you might not be a bit familiar with how to code, I highly suggest picking up Kevin Yank's book Build Your Own Database Driven Website Using PHP & MySQL

  19. #19
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have phpmyadmin. I just need a sample string and I can make the proper modifications myself. I just don't know the syntax for something such as this. I think spikez code would work, there is just a conflict of interest with the two if submit's.

  20. #20
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sample string ahoy!
    All of these will by within mysql_query(" ");

    Adding a new row:

    INSERT INTO `tablename` SET `fieldname` = '$variable', `otherfield` = '$anothervariable'

    Updating a current row (ie: editing):
    UPDATE `tablename` SET `fieldname` = '$variable' WHERE `somefield` = '$value'

    Viewing:

    SELECT * FROM `tablename` WHERE `fieldname` = '$value'


    When doing a SELECT, you'll have to draw out the field values using mysql_fetch_array() (or mysql_fetch_assoc as I prefer).


    So let's say you have a table (ignore underlines, they're to keep the spacing)

    People (table)
    -id -name -age (fields)
    1 Bobby__ 24
    2 Sally___ 45
    3 Martha _100
    PHP Code:
    // Gets all of the records where age is less than 50
    $getppl mysql_query("SELECT * FROM `people` WHERE `age` < 50");

    //Sets variables to the $person var based on the fieldname
    while ($person mysql_fetch_assoc($getppl) ) {
    echo 
    "Name: $person['name']<br />
    Age: 
    $person['age']<br />";


  21. #21
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But what about the complications with the if submit?

  22. #22
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There really is nothing complicated with it. When your script loads, it checks the if statement. If it's true, it loads the one, if not, it loads the other. By moving the if statement below the submit button, it doesn't do anything other than move where the logic is.

    IE - by moving the if statement below the submit button, that doesn't mean that the bottom part of the page will only load to check the if statement. Once a page is loaded, it's done (unless you're doing something like AJAX).

    You need to set the action in the form like was mentioned above.

  23. #23
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So do I need to remove the top one?

  24. #24
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well when I remove the one on the top the whole application bombs out

  25. #25
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anyway to split spikez code up?


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
  •