Jump to content

cpd

Members
  • Posts

    883
  • Joined

  • Last visited

  • Days Won

    5

Posts posted by cpd

  1. If you're using PDO you can use a different data structure. What you have at the minute is really confusing and can be massively simplified. I'd be happy to assist if you like...

     

    And no I wasn't about to suggest PDO, that's an object for interfacing with a database... 

  2. Are you programming procedurally? If not I suggest you look at using a different data structure. 

     

    If you are, use a two dimensional array as already suggested.

     

    E.g.

    $a = array(
       "tile1" => array(
          "card1", "card2", "card3"
       ),
       "tile2" => array(
          ...
       )
    );
  3. To directly answer your question: it's because of the array. It doesn't resolve $$something where $something is an array element as you have, to a variable variable. 

     

    Now, why are you doing it this anyway? I've not once had to use a variable variable and have always advised to avoid it as there are cleaner methods.

  4. http://www.html-form-guide.com/php-form/php-form-action-self.html

     

    Scroll down to "What are PHP_SELF exploits and how to avoid them". I wouldn't use PHP_SELF as you have.

     

    Have you connected to the database? Have you even created a database? What is the field type you're trying to insert to in the database as this dictates the format you use?

     

    Do your research, come back with a specific problem once you've attempted it yourself.

  5. Change 

    if(preg_match("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){

    to

    if(preg_match("#^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$#",$email )){

    And thanks.

  6. Your if(preg_match... has no ending delimiter and from the look of it no starting one either. I assume you've used ^ to signify the beginning of the string.

     

    Insert delimiters, e.g. preg_match("#([A-Z0-9]+)#", $string) where the # is the delimiter.

  7. So, in a single sentence:

     

    "When the XML file exists but has no contents, i.e. does not contain an 'entries' element, I can't write to it".

     

    And once again, I still stand by my original comments regarding breaking the problem down into smaller ones and this being a simple problem.



    <?php
    
    
    $file = "entries.xml";
    $default = "entries_default.xml";
    
    
    
    
    // Does the file exist? Does it contain data?
    if(!file_exists($file) || ($xml = file_get_contents($file)) == "") {
        $xml = file_get_contents($default);
    }
    
    
    // Looad the XML in
    $dom = new DOMDocument("1.0", "UTF-8");
    
    
    
    
    $dom->loadXML($xml);
    
    
    
    
    // Was the form submitted?
    if(isset($_POST['xml_append'])) {
        // Get the root node
        $root = $dom->getElementsByTagName("entries");
        
        // Was the root node found? No, so create the file again using the default
        if($root->length == 0) {
            $dom->loadXML(file_get_contents($default));
        }
        
        $root = $dom->getElementsByTagName("entries")->item(0);
        
        // Unset the submit button post data
        unset($_POST['xml_append']);
        
        // Create variables for each $_POST element
        extract($_POST);
    
    
    
    
        // Create the report element
        $report = $dom->createElement("report");
        $report->appendChild($dom->createElement("timestamp", gmdate("Y-m-d H:i:s")));
        $report->appendChild($dom->createElement("fname", $fname));
        $report->appendChild($dom->createElement("lname", $lname));
        $report->appendChild($dom->createElement("location", $location));
        $report->appendChild($dom->createElement("type", $type));
        $report->appendChild($dom->createElement("description", $description));
    
    
    
    
        // Append the report
        $root->appendChild($report);
    
    
    
    
        // Save as XML
        $dom->save("entries.xml");
    }
    
    
    
    
    // Get the last piece of data saved
    $reports = $dom->getElementsByTagName("report");
    if($reports->length > 0) {
        $data = array();
        $children = $reports->item(($reports->length-1))->childNodes;
        foreach($children as $child) {
            $data[$child->nodeName] = $child->textContent;
        }
    }
    
    
    
    
    ?>
    <html>
        <body>
            <form method="post">
                <fieldset style="display: inline-block;">
                    <label>Fname</label><input type="text" name="fname" value="<?=(isset($data) ? $data["fname"] : "");?>" /><br />
                    <label>Lname</label><input type="text" name="lname" value="<?=(isset($data) ? $data["lname"] : "");?>" /><br />
                    <label>Location</label><input type="text" name="location" value="<?=(isset($data) ? $data["location"] : "");?>" /><br />
                    <label>Type</label><input type="text" name="type" value="<?=(isset($data) ? $data["type"] : "");?>" /><br />
                    <label>Description</label><input type="text" name="description" value="<?=(isset($data) ? $data["description"] : "");?>" /><br />
                    <input type="submit" name="xml_append" value="Save" />
                </fieldset>
            </form>
        </body>
    </html>

    A less than 5 minute edit to do what you want. This now performs two additional checks: 1) Checks if anything is in the XML file and if not uses the default; 2) Checks for an "entries" node and if it doesn't exist, uses the default.

  8. I'm either seriously missing something, or have interpreted your posts correctly and you're failing to match the dots up.

     

    <?php
    
    
    
    
    $file = "entries.xml";
    
    
    // Does the file exist?
    if(file_exists($file)) {
        $xml = file_get_contents($file);
    } else {
        $xml = file_get_contents("entries_default.xml");
    }
    
    
    // Looad the XML in
    $dom = new DOMDocument("1.0", "UTF-8");
    $dom->loadXML($xml);
    
    
    // Was the form submitted?
    if(isset($_POST['xml_append'])) {
        // Get the root node
        $root = $dom->getElementsByTagName("entries")->item(0);
        
        // Unset the submit button post data
        unset($_POST['xml_append']);
        
        // Create variables for each $_POST element
        extract($_POST);
    
    
        // Create the report element
        $report = $dom->createElement("report");
        $report->appendChild($dom->createElement("timestamp", gmdate("Y-m-d H:i:s")));
        $report->appendChild($dom->createElement("fname", $fname));
        $report->appendChild($dom->createElement("lname", $lname));
        $report->appendChild($dom->createElement("location", $location));
        $report->appendChild($dom->createElement("type", $type));
        $report->appendChild($dom->createElement("description", $description));
    
    
        // Append the report
        $root->appendChild($report);
    
    
        // Save as XML
        $dom->save("entries.xml");
    }
    
    
    // Get the last piece of data saved
    $reports = $dom->getElementsByTagName("report");
    if($reports->length > 0) {
        $data = array();
        $children = $reports->item(($reports->length-1))->childNodes;
        foreach($children as $child) {
            $data[$child->nodeName] = $child->textContent;
        }
    }
    
    
    ?>
    <html>
        <body>
            <form method="post">
                <fieldset style="display: inline-block;">
                    <label>Fname</label><input type="text" name="fname" value="<?=(isset($data) ? $data["fname"] : "");?>" /><br />
                    <label>Lname</label><input type="text" name="lname" value="<?=(isset($data) ? $data["lname"] : "");?>" /><br />
                    <label>Location</label><input type="text" name="location" value="<?=(isset($data) ? $data["location"] : "");?>" /><br />
                    <label>Type</label><input type="text" name="type" value="<?=(isset($data) ? $data["type"] : "");?>" /><br />
                    <label>Description</label><input type="text" name="description" value="<?=(isset($data) ? $data["description"] : "");?>" /><br />
                    <input type="submit" name="xml_append" value="Save" />
                </fieldset>
            </form>
        </body>
    </html>

    Default XML file is as before. 

     

    This uses a form to write data via PHP to an XML file. It does not submit a form to an XML file, that's just silly. If there are no entries, it still writes to the file. If there are entries, it still writes to the file. It also attempts to get the last entry and populates the fields with the data.

     

    I'm still standing by my original comments. And once again this was modified in less than 5 minutes.

  9. You never said you needed to populate the form using data from the XML file. The problem you specified was that you had an issue appending to an XML file.

     

    Does this mean you want to populate the form with the first entry or the last entry? 

    Do you want to overwrite what's already in the XML file or append to it as they are two completely different tasks?

  10. Read it again, my previous answer still stands. I took your two posts combined and wrote the following script, tested it, and confirmed it works as you want it too; unless I've completely misinterpreted your OP.

     

    $file = "entries.xml";
    
    
    // Does the file exist?
    if(file_exists($file)) {
        $xml = file_get_contents($file);
    } else {
        $xml = file_get_contents("entries_default.xml");
    }
    
    
    // Looad the XML in
    $dom = new DOMDocument("1.0", "UTF-8");
    $dom->loadXML($xml);
    $root = $dom->getElementsByTagName("entries")->item(0);
    
    
    // Create the report element
    $report = $dom->createElement("report");
    
    // Add the reports children.
    $report->appendChild($dom->createElement("timestamp", gmdate("Y-m-d H:i:s")));
    $report->appendChild($dom->createElement("fname", "First name"));
    $report->appendChild($dom->createElement("lname", "Last name"));
    $report->appendChild($dom->createElement("location", "Location"));
    $report->appendChild($dom->createElement("type", "Report type"));
    $report->appendChild($dom->createElement("description", "This is my description"));
    
    
    // Append the report
    $root->appendChild($report);
    
    
    // Save as XML
    $dom->save($file);

     

    entries_default.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <entries>
        
    </entries>

     

    Written in 5-10 minutes at 2:30am. Like I said, the problem is easy to solve if you break it down.

     

    Take the advice next time.

  11. You really don't make sense but I think I've got the gist of what you're asking. If you want responses to a problem you should really spend more time writing your post, use code tags for any code you include and not describe a bulk of the problem in the topic title.

     

    The problem

    You want to return a user to the "add product" page after they've just added a new product.

     

    The process

    1. User is on the addprod.php page that displays a form with an action value of "add_prod.php" or similar.
    2. add_prod.php creates a new record in the database for the new product and redirects to the addprod.php after the process is complete.
    3. addprod.php displays a success/error message dependent on the outcome.

     

    Is this correct?

     

    Don't use JavaScript to display a success message. Instead, use a session to store a success message and display it on the next page. You can then use the header function to redirect, i.e. header("Location: ... ").

     

    Furthermore, you need to sanitise your data as you are open to SQL injection at the minute. I strongly suggest you reconsider your file names as well as they're very confusing. A large system with this sort of file naming would confuse the crap out of me.

×
×
  • 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.