Jump to content

Invidia

New Members
  • Posts

    4
  • Joined

  • Last visited

Posts posted by Invidia

  1. I'm working on an existing project, it's a mess but for the moment I just have to get the delete function working, but keep getting an error and I'm not sure what I'm missing.

    The query string is a mess, but that's a headache for another day. The target shouldn't ge part of the query string, but that's how someone else had set it up so for now I'm stuck trying to work with it as it is currently and just have to get some basic CRUD functionality going. The other minor snag is inconsistencies with the name of the primary key between tables. It's not consistently called 'ID" so I need to get both the key and value for the where part of the DELETE statement. The query that I currently have is not working. Error 500, can't even get a simple var_dump to work.

     

    Query string as it is in its present state:

    Quote

    somewbsite.com/endpoints/delete.php?target=TABLENAME&primaryKey=7

    <?php
    
    include('includes/config.php');
    $table = $_GET['target'];
    
        parse_str($_SERVER['QUERY_STRING'], $data);
        array_shift($data);
    
        $cols = array_keys($data);
        $vals =  array_values($data);
        $idType = $cols[0];
        $id = $vals[0];
    
        $stmt = $pdo->prepare("DELETE FROM SOME_DATABASE.$table WHERE $idType = '$id';");
        $status = $stmt->execute();
        if($status)
        {
    		echo "Success";
        }
        else
        {
            echo "Fail";
        }
    ?>

     

  2. The reason I'm not totally certain those are variables is because I don't understand why there are two of them inside the parenthesis separated by the word as. I'm trying my best to make sense of it with what I have read. I've been trying to use codecademy and it seems that whatever is in the first spot gets stuffed inside whatever is inside the second spot. Like renaming it. The examples I went through on codecademy made it seem like you used a variable to make an array then then put that variable name in the first spot and use the word as then give it a name in the second spot to use. But in this case I'm not using an array that is initialized with a string or anything like that. I know that arrays start at position 0 in Java and other languages and I think it's the same in php. I'm VERY new to programming, but php in particular and being given virtually no instruction and deadlines with little clue as to how anything works is frustrating. 

    foreach is a loop. I got that. How is that supposed to work with a database? How am I supposed to make an array with a database? How do I connect it in? I have minimal understanding how POST works, so how does that get incorporated in from a textbox in order to get info from the db to put into an array?

    I say "I think" because I'm new and not certain. I'm doing what I can to try to understand from what I've read and done on codecademy and watched on youtube and searched on php.net

    I don't understand at all what the -> in the code snippet I posted is supposed to do. Bind Value from what I can tell seems to take an SQL column name and stick it to a variable. Where I'm getting lost at is that I'm not quite understanding how these things fit together or where they're supposed to go.

  3. From what I can tell, using the try catch it does say that the database is connected. So I think I managed to get that much working. But I have no clue how to actually make data show up based on what's put in the textboxes or where stuff needs to actually go. So confused. :(

  4. I had a different account on here but forgot my info. Sorry about that. Anyway.

    I'm a programming student with a teacher that doesn't really teach. She just reads pre-made powerpoint slides and often seems surprised to learn about things that she was unaware you could do prior to reading the slides. Basically, I think the school needed a PHP teacher and just kinda said "Eh. close enough. You'll do." She's not really friendly and asking for help is usually met with a rude reply, a condescending tone and an answer that isn't really an answer. I've asked classmates for help to no avail - they're just as confused. There are literally no PHP tutors as it's just a community college. I had the same lady for SQL and knew what to expect, but she's the only professor for these classes. She's an ex-marine, she's probably in her 60s or so and is close to retiring so I kinda get the feeling that she doesn't really care at this point, but it does a huge disservice to us - her students. I've never actually seen her code a single thing in class and she often gets confused and has to stop to thumb through her book or power points in order for her to figure out how to even import a database into xampp just to show us what it looks like. So this is my last hope. My chosen major is programming, like OOP with VB and Java and such, but we're still required to take PHP and SQL. I'm working on talking to the dean about the issues with the prof, but until something if anything can be done I have to deal with what's at hand.
     
    With that said, here my assignment specifications just so you all have an idea what I'm actually trying to do. Below I'll post code showing what I've done so far. I'm very confused, very frustrated and struggling so much with this thing. Please understand, I'm not asking anyone to do my homework for me. I need help. I have some random code snippets garnered from my book and the crap power-point, but I don't understand how they work or where to put them or anything.
     
    ==============================================
     
    INSTRUCTIONS


     
    Deliverables: Zipped project folder containing the following files:
     
    Index.html containing a form
    Main.css for formatting
    Display_data.php to display the data retrieved from the database
    Database_error.php to display error messages
     
     
     
    If necessary:
     
    Use the SQL tab in PHPAdmin to write and run the following query:
                CREATE DATABASE ZipcodeData
    Use the Import tab to import the file zipcodes2014 to create the tables and import the data for the ZipcodeData database.
     
     
     
    Create an HTML file with a form that allows a user to enter a city and a two letter state abbreviation. Use POST and your display_data.php file.
    Create a PHP file that will create and run a query against the zipcodes table. The query will retrieve the following columns from the table, for the city and state entered in the form:
    Zipcode
    Latitude
    Longitude
    Estimated population
     
    Try/Catch must be used around the following pieces of code:
     
    Creating the PDO object to connect to a database
    Preparing, binding values and executing the query
     
    Each Catch clause will assign a different appropriate message to a variable, and will then include the database_error.php file (which will display the error message) and exit display_data.php.
     
    The HTML page created and returned by the PHP will contain the city and state as <h1> headers. The PHP must display the four columns of data in a table. Because the number of rows to be returned may be different each time the code should use a foreach loop that will loop through the array returned by the query, creating a new row for the table and displaying the data for each iteration of the loop.
     

     

     

     
    ===========================================
    MY HTML PAGE
    <!--Indicates page is HTML5 compliant-->
    <!DOCTYPE html>
    <html>
    <head>
        <title>Find Zipcode Data</title>
        <link rel="stylesheet" type="text/css" href="main.css">
    </head>
    
    
    <body>
        <main>
    
    
            <h1>Zip code data by city and state</h1>
    
    
            <form action="Display_data.php" method="post">
    
    
    <!-- User input labels -->
                <div id="data">
                    <label>City:</label>
                    <input type="text" name="city"><br>
    
    
                    <label>State (two letter abbreviation):</label>
                    <input type="text" name="state"><br>
                </div>
    
    
    <!-- Submit and Reset buttons -->
                <div id="buttons">
                    <label> </label>
                    <input type="submit" value="Display Zipcodes"><br>
                    <input type="reset" value="Clear Form"><br>
                </div>
    
    
            </form>
        </main>
    </body>
    </html>
     
    ==============================================
    MY CSS PAGE
    /*CSS for general page look*/
    main {
        width: 450px;
    height: 190px;
        margin: 0 auto;
        padding: 1em;
        background: white;
        border: 2px solid navy;
    }
    
    
    body {
        background-color: #fff;
    font-family: Arial, Helvetica, sans-serif;
    text-align: center
    }
    
    
    
    
    /* Header */ 
    h1 {
    margin-top: 0;
    color: navy;
    }
    
    
    /*CSS for label positioning*/
    label {
        width: 12em;
        float: left;
        padding-bottom: .5em;
    }
    
    
    /*corresponds to div tag on index.html to adjust spacing around textboxes*/
    #data input {
        float: left;
        width: 11em;
        margin-bottom: .5em;
    }
    
    
    /*corresponds to div tag on index.html to adjust spacing below buttons*/
    #buttons input {
        margin-bottom: .5em;
       left:50%;
    }
     
     
    ===========================================
     
    MY DISPLAY_DATA.PHP PAGE
    <?php
    $city = $_POST['city'];
    $state = $_POST['state'];
    
    
    $dsn = 'mysql:host=localhost;dbname=zipcodedata';
    $username = 'mgs_user';
    $password = 'pa55word';
    
    
    try {
        $db = new PDO($dsn, $username, $password);
        echo '<p>You are connected to the database!</p>';
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        echo "<p>An error occurred while connecting to
                 the database: $error_message </p>";
    exit();
    }
    
    
    // creates PDO object
    $db = new PDO($dsn, $username, $password);
    
    
    ?>
    
    
    <!--Indicates page is HTML5 compliant-->
    <!DOCTYPE html>
    <html>
    <head>
    <!--Titles the page at the top such as on the browser tab (w/ Chrome)-->
        <title>Display zip code data</title>
    <!--Pulls the CSS styling from the main.css page-->
        <link rel="stylesheet" type="text/css" href="main.css">
    </head>
    <body>
        <main>
    
    
        </main>
    </body>
    </html>
     
    ======================================
     
    MY DATABASE_ERROR.PHP PAGE
    <?php
    
    
    ?>
    
    
    <!--Indicates page is HTML5 compliant-->
    <!DOCTYPE html>
    <html>
    <head>
    <!--Titles the page at the top such as on the browser tab (w/ Chrome)-->
        <title>Error</title>
    <!--Pulls the CSS styling from the main.css page-->
        <link rel="stylesheet" type="text/css" href="main.css">
    </head>
    <body>
        <main>
    
    
        </main>
    </body>
    </html>
     
    =================================================
     
    RANDOM CODE SNIPPETS
     
    I don't know how to use this. I see that there's things like $query which I'm guessing is a variable because it has a $ in front of it, but does it need to be declared somewhere? How would I go about doing that? What kind of stuff needs to be attached to it?
     
    $query = 'SELECT * FROM products
              WHERE categoryID = :category_id';
    $statement = $db->prepare($query);
    $statement->bindValue(':category_id', $category_id);
    $statement->execute();

     

     

     

    In the instructions it says that I need to Preparing, binding values and executing the query, but I have no clue what that means. Also we were told that part also needs to be in a try catch statement. As far as I can tell the one try catch thing I have in my code so far at least makes the "Database is Connected" string appear when I run the HTML code and click the submit button.

     

    This is another code snipped I got from the powerpoint. Again I have no idea how most of it works. I know that foreach is a loop, but I don't know what the stuff in the parenthesis is. I guess it's aruments, but beyond that I don't understand what ________ as _______ is supposed to actually do. In Java I absolutely HATE for loops. I prefer to use do until or do while. For loops just don't seem to click very well for me. And somehow I'm supposed to use POST to get the data. I vaguely understand POST. In my understanding, it takes the name associated with an input box on the html form and whatever stuff is typed into it and stuffs it into a variable on the php page by putting the same name inside single quotes and setting it equal to a variable using POST. Ok, but beyond that I don't know what do do with that to make whatever the user types into the textbox actually work to retrieve data from a database and display it in a table.

    <?php foreach ($products as $product) : ?>
    <tr>
        <td><?php echo $product['productCode']; ?></td>
        <td><?php echo $product['productName']; ?></td>
        <td><?php echo $product['listPrice']; ?></td>
    </tr>
    <?php endforeach; ?>

    Please go easy on me. It's my 4th week in this hellish class. So far the little bit of code I've managed to gather up has taken me about 14 hours thus far and I don't even know if any of it is right. 

     

    =============================================

     

    Rather than actually show us how to code, the prof simply pulled up MS Paint and created pictures to show us what she wants this thing to look like. If I could post photos on here I would at the very least share them so people can have an idea of the goal. 

     

    The "Good Data" Picture she drew has a table kinds like this:

     


    Zipcode data for Memphis, TN

    =================================================

    |  ZIPCODE | LATITUDE | LONGITUDE | EST. POPULATION|

    |     37501    |     35.12     |        -89.97     |               15,517      |

    |     38104    |     35.12     |        -89.97     |               17,217      |

    |     38016    |     35.12     |        -89.97     |               10,799      |

    |     38109    |     35.12     |        -89.97     |               12,867      |

     

     

     

    The error picture she drew looks kinda like this:

     

    DATABASE ERROR

     

    System message: SQLSTATE[HY000] [1045] Access Denied for user 'mgs_user@localhost' (using password: YES)

    There was an error connecting to the Database.

    Please try back later.

     

     

     

    The input page:

    And the HTML + CSS I posted shows how the input page should look. But I have no clue how to make stuff that a person types in to a textbox actually get data from a database and make it populate a table with the results. That's all we got, was premade instructions, premade powerpoints and MS Paint pictures. I've been trying to make sense of this with my textbook, but most of it seems to be written from the perspective that the reader already has a base understanding of what this stuff actually is. PDO is PHP Data Object, but beyond a textbook definition I don't know what that actually means. So many issues coming into play that's making this miserably difficult. I didn't even want to take a PHP course much less with this lady, but I had no choice.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.