code adding blank info into database! help

Discussion in 'PHP' started by mezner, Dec 21, 2010.

  1. #1
    I recently decided to integrate a php page that I had the web owner use to update the database information. Since switching to Wordpress, I figured it would be best to just put that page INTO the wordpress administrative panel menu, just like where he goes to add pages. I got really far, but once I thought it was all good to go, I get an error. Here is the code and below it I will explain what's wrong...

    <?php
    if (isset($_POST['date'])) {
            mysql_connect("deleted info");
            mysql_select_db("deleted info");
            $ref = $_SERVER['HTTP_REFERER'];
            $msg = "<script type='text/javascript'>
    function delay() {
    setTimeout(\"window.location = 'post.php'\", 1000);
    }
    document.onload = delay();
    </script>
    Thanks for posting - Update added to mainpage!";
    
            $sku = addslashes($_POST['date']);
            $name = addslashes($_POST['stock']);
            $price = addslashes($_POST['profit']);
       
            $result = mysql_query("INSERT INTO recent (date, stock, profit) VALUES ('$date', '$stock', '$profit');");
            if ($result) {
                echo $msg;
                exit;
            } else {
                echo "There was an error adding your entry - please try again, filling in all fields.";
            }
        }
    // mt_add_pages() is the sink function for the 'admin_menu' hook
    function mt_add_pages() {
        // Add a new top-level menu:
       // The first parameter is the Page name(Site Help), second is the Menu name(Help)
       //and the number(5) is the user level that gets access
        add_menu_page('Recent Picks', 'Recent Picks', 5, __FILE__, 'mt_toplevel_page');
    }
    
    // mt_toplevel_page() displays the page content for the custom Test Toplevel menu
    function mt_toplevel_page() {
        echo '
    
    <p>Add in the oldest entiest FIRST. Newest entries last.</p>
    <table width="33%">
    	<tr>
    		<th align="center">date</th>
    		<th align="center">stock</th>
    		<th align="center">Profit</th>
    	</tr>
    	<tr>
    		<td align="center">example:</td>
    		<td align="center">example:</td>
    		<td align="center">example:</td>
    	</tr>
    	<tr>
    		<td align="center">6-28 / 11-10</td>
    		<td align="center">JDMR</td>
    		<td align="center">15% (put in %)</td>
    	</tr>
    	<tr>
    <form method="post" action="post.php">
    <td align="center"><input type="text" name="date" size="5" maxlength="5"/></td>
    <td align="center"><input type="text" name="stock" size="4" maxlength="4"/></td>
    <td align="center"><input type="text" name="profit" size="4" maxlength="4"/><td>
    	</tr>
    </table>
    
    <input type="submit" value="Post" />
    </form>
    </div>
    ';
    }
    // Insert the mt_add_pages() sink into the plugin hook list for 'admin_menu'
    add_action('admin_menu', 'mt_add_pages');
    ?>
    PHP:
    Everything is displayed fine. There is it's own seperate menu tab on the left hand side, the form renders perfecty, and the message confirming it worked is displayed afterwards. I tried it twice to see no results on the home page. hmmm. I went to make sure nothing was changed about the database and noticed that there was 2 new entries made (by me), but both were 100% blank! What am I doing wrong?

    Right now I have this as a .php file as a plugin that I uploaded (not sure how much that helps).
     
    mezner, Dec 21, 2010 IP
  2. IAreOwn

    IAreOwn Active Member

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #2
    probably because

    
    if (isset($_POST['date'])){
    ..
    }
    
    PHP:
    is always true and thus run your mysql query everytime you refresh the page...
     
    IAreOwn, Dec 21, 2010 IP
  3. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm really not sure what to do. I also redid the plugin so it looked like this...

    <?php
         if (isset($_POST['date'])) {
            mysql_connect("localhost", "deleted", "deleted");
            mysql_select_db("deleted");
            $ref = $_SERVER['HTTP_REFERER'];
            $msg = "<script type='text/javascript'>
    function delay() {
    setTimeout(\"window.location = 'post.php'\", 1000);
    }
    document.onload = delay();
    </script>
    Thanks for posting - Update added to mainpage!";
    
            $sku = addslashes($_POST['date']);
            $name = addslashes($_POST['stock']);
            $price = addslashes($_POST['profit']);
       
            $result = mysql_query("INSERT INTO recent (date, stock, profit) VALUES ('$date', '$stock', '$profit');");
            if ($result) {
                echo $msg;
                exit;
            } else {
                echo "There was an error adding your entry - please try again, filling in all fields.";
            }
        }
    // mt_add_pages() is the sink function for the 'admin_menu' hook
    function mt_add_pages() {
        // Add a new top-level menu:
       // The first parameter is the Page name(Site Help), second is the Menu name(Help)
       //and the number(5) is the user level that gets access
        add_menu_page('Site Help', 'Help', 5, __FILE__, 'mt_toplevel_page');
    }
    
    // mt_toplevel_page() displays the page content for the custom Test Toplevel menu
    function mt_toplevel_page() {
        echo '
    <p>Add in the oldest entiest FIRST. Newest entries last.</p>
    <table width="33%">
    	<tr>
    		<th align="center">date</th>
    		<th align="center">stock</th>
    		<th align="center">Profit</th>
    	</tr>
    	<tr>
    		<td align="center">example:</td>
    		<td align="center">example:</td>
    		<td align="center">example:</td>
    	</tr>
    	<tr>
    		<td align="center">6-28 / 11-10</td>
    		<td align="center">JDMR</td>
    		<td align="center">15% (put in %)</td>
    	</tr>
    	<tr>
    <form method="post" action="post.php">
    <td align="center"><input type="text" name="date" size="5" maxlength="5"/></td>
    <td align="center"><input type="text" name="stock" size="4" maxlength="4"/></td>
    <td align="center"><input type="text" name="profit" size="4" maxlength="4"/><td>
    	</tr>
    </table>
    
    <input type="submit" value="Post" />
    </form>
    </div>
        ';
    }
    
    // Insert the mt_add_pages() sink into the plugin hook list for 'admin_menu'
    add_action('admin_menu', 'mt_add_pages');
    ?>
    PHP:
    The only problem with this is the fact that I can't get access to the Database because I get this error...
    Basically all I'm trying to do is have a seperate page on the left side of the wordpress admin like you see for themes, or posts. Inside that page I want to have 3 inputs where once you click submit they are added into a database. Thats it. Seems to be harder than expected...
     
    mezner, Dec 21, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    
    <?php
    /*
    Plugin Name: Test
    Plugin URI: http://www.google.com
    Description: A simple test
    Author: mezner
    Version: 1
    Author URI: http://www.google.com
    */ 
        global $wpdb;
        if (isset($_POST['date'])) {
            $ref = $_SERVER['HTTP_REFERER'];
            $msg = "<script type='text/javascript'>
                    function delay() {
                        setTimeout(\"window.location = 'post.php'\", 1000);
                    }
                    document.onload = delay();
                    </script>
                    Thanks for posting - Update added to mainpage!";
            
            $sku = addslashes($_POST['date']);
            $name = addslashes($_POST['stock']);
            $price = addslashes($_POST['profit']);
       
            $result = $wpdb->query( $wpdb->prepare( "INSERT INTO recent (date, stock, profit) VALUES (%s, %d, %f)", $date, $stock, $profit ));
    
            if ($result) {
                echo $msg;
                exit;
            } else {
                echo "There was an error adding your entry - please try again, filling in all fields.";
            }
        }
        
        // mt_add_pages() is the sink function for the 'admin_menu' hook
        function mt_add_pages() {
            if(function_exists('add_menu_page'))
            {
                add_menu_page('Site Help', 'Help', 8, __FILE__, 'mt_toplevel_page');
            }
        }
        
        // mt_toplevel_page() displays the page content for the custom Test Toplevel menu
        function mt_toplevel_page() {
            echo '  <p>Add in the oldest entiest FIRST. Newest entries last.</p>
                    <table width="33%">
                    <tr>
                        <th align="center">date</th>
                        <th align="center">stock</th>
                        <th align="center">Profit</th>
                    </tr>
                    <tr>
                        <td align="center">example:</td>
                        <td align="center">example:</td>
                        <td align="center">example:</td>
                    </tr>
                    <tr>
                        <td align="center">6-28 / 11-10</td>
                        <td align="center">JDMR</td>
                        <td align="center">15% (put in %)</td>
                    </tr>
                    <tr>
                        <form method="post" action="post.php">
                        <td align="center"><input type="text" name="date" size="5" maxlength="5"/></td>
                        <td align="center"><input type="text" name="stock" size="4" maxlength="4"/></td>
                        <td align="center"><input type="text" name="profit" size="4" maxlength="4"/><td>
                    </tr>
                    </table>
                    
                    <input type="submit" value="Post" />
                    </form>
                    </div>';
        }
        
        add_action('admin_menu', 'mt_add_pages');
    ?>
    
    PHP:
     
    Last edited: Dec 21, 2010
    tvoodoo, Dec 21, 2010 IP
  5. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Still getting the same error which is...

    Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\wordpress\wp-content\plugins\testmenu.php:8) in C:\xampplite\htdocs\wordpress\wp-includes\pluggable.php on line 868
    PHP:
    The only difference between this code and the one I actually tested on the live site is the locations are for the actual site not the localhost. I got the original skeleton of the menu from here. Some of the users also dropped the code right into the functions file, but when I did that, wordpress admin login and every page was blank.

    My other option, which is not what I really want, is to have an additional admin menu that he can log into and do all the data from there. Since he is usually logged into wordpress, it only made sense I try that first.

    edit: Would it be because I need to include this table into one of the wordpress databases (ie wrdp1, wbst1)? Right now it's off in its own place.
     
    mezner, Dec 22, 2010 IP
  6. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #6
    I tested it and it worked fine for me. ( the code I've paste in here )
     
    tvoodoo, Dec 22, 2010 IP
  7. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Where do you have the "recent" table located? Mine is located inside a database called "pennyp". I copied your code word for word, saved it as test.php, loaded it and activated it. It looks nice, but the same error occurs. I believe you that it works for you, but something has to be different.
     
    mezner, Dec 22, 2010 IP
  8. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #8
    the table needs to be in the same database as the wordpress tables.
     
    tvoodoo, Dec 22, 2010 IP
  9. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I moved it into the right database, no errors now, BUT the data entered was blank. I got nothing for the date, "0" for stock and "0.000" for the profit. Here is the structure of the table:

    id; auto increment
    date: varchar 10
    stock: varchar 4
    profit: varchar 5
    nothing fancy. Still doesn't work though, but very close!
     
    mezner, Dec 22, 2010 IP
  10. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #10
    Well its either because of the format : %s, %d, %f change to => %s, %s, %s , or because your form isn't posting correctly.
     
    tvoodoo, Dec 22, 2010 IP