WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Read data from a PHP form and store them in a database (4 posts)

  1. _OMEGA_
    Member
    Posted 4 years ago #

    Hi all, I hope someone could help me to understand how to do a plugin.
    I'm creating my first plugin for my personal use at the moment.
    I've created two files, called:
    file_admin.php (where I'm putting the functions of activation and administration menu creation);
    page_add_new.php (where I want to put a PHP form visible only from the admin backend).

    My main goal is:
    1. create a plugin that read from a specific database and not from the one where WordPress is installed;
    2. create a table in my own database;
    3. create a PHP form with 1 text field, 1 Image Uploader, 1 List and some Check boxes that an user can fill and store inserted datas in my own database.

    What I've done:
    1. I've created a function that read from a specific database:

    function myplugin_activate()
    {
     global $wpdbtest_otherdb;
     $hostname = "MyHostname";
     $db_user = "MyUser";
     $db_password = "MyPass";
     $database = "My_DB_Name";
    
     $wpdbtest_otherdb = new wpdb($db_user, $db_password, $database, $hostname);
     $wpdbtest_otherdb->show_errors();
    
     $table = $wpdbtest_otherdb->prefix."myplugin";
    
     $structure = "CREATE TABLE $table (id INT(9) NOT NULL AUTO_INCREMENT, id_field1 VARCHAR(80) NOT NULL, id_field2 VARCHAR(80) NOT NULL, UNIQUE KEY id (id));";
     $wpdbtest_otherdb->query($structure);
    }
    
    add_action('activate_myplugin/file_admin.php', 'myplugin_activate');

    2. The above function read from my own Database and create a new table called "myplugin".

    3. I've created a simple form in another file called "page_add_new.php" but I don't any idea how to store the inserted data in my database.

    Hope in some help form you.

  2. _OMEGA_
    Member
    Posted 4 years ago #

    I forgot to say that I trying to put the "function myplugin_activate()" in the "file_admin.php" and the form in the file "page_add_new.php"

  3. _OMEGA_
    Member
    Posted 4 years ago #

    Any hint pls?
    This thing is driving me crazy!

  4. _OMEGA_
    Member
    Posted 4 years ago #

    I'm writing some code in the "page_add_new.php", that's cause I want to keep the things separate.
    I don't think to create a function in it, so I've done so:

    <?
    	if (isset($_POST['submit']) and $_POST['submit']==TRUE) {
    	$id_field1 = ($_POST['id_field1']);
    	$id_field2 = ($_POST['id_field2']);
    
    	####################################################################
    	# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
    	$wpdbtest_otherdb = mysql_connect($hostname, $db_user, $db_password);
    	mysql_select_db($database,$wpdbtest_otherdb);
    
    	$sqlQuery = "INSERT INTO " .
    	$wpdbtest_otherdb->prefix."gpedia(id_field1, id_field2) VALUES ('$id_field1', '$id_field2')";
    	$wpdbtest_otherdb->query($sqlQuery);
    } else { ?>
    
    <?php if ( !empty($_POST ) ) : ?>
    <div id="message" class="updated fade">
    <strong>Your setting has been saved message goes here....</strong>
    </div>
    <?php endif; ?>
    <div class="wrap">
    <h2>Admin - Add New</h2>
    <!-------------begin form------------>
    <form method="post" action="">
    Field_1: <br />
    <input type="text" name="id_field1">
    <br />
    Field_2: <br />
    <input type="text" name="id_field2">
    <br /><br />
    <input type="submit" name="submit" id="submit" value="send">
    </form>
    <!-------------end form------------>
    </div>
    
    <?  } ?><br />

    Please help me, dunno what to do! :(

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.