Skip to:
Content
Pages
Categories
Search
Top
Bottom

Function to update databse fields


  • martinbeaulne
    Participant

    @martinbeaulne

    Hi ! I would like to know how to create a simple form to update a field.

    Let’s say I put this form on member-header.php

    <form>
    <div>Some text</div><div>Some field</div>
    <div>Some button to submit –> Some function to update the field</div>

    I guess it must be quite easy, but I’m not a php programmer… Having a simple template form could help me build the rest…

    Thanks !

Viewing 14 replies - 1 through 14 (of 14 total)
  • In the nicest way possible, assuming that something you don’t know how to do is easy is a fairly common misconception about how hard or time-consuming a thing is.

    Likewise, this question is too general and vague to answer; if you can give details about what exactly your end goal is, perhaps someone here knows of an existing solution or plugin you can use.


    martinbeaulne
    Participant

    @martinbeaulne

    By “easy”, I meant “There must be simple functions for posting things to the database; but I don’t know them”. I often see people answering to questions with pages and pages of code from outer space, so I thought my little question would be a very simple one….

    Well.. Let’s make my question “less general”.

    I want to get rid of the bottom tabs of the member profile page. Still, I want the members to be able to edit their profile. I want to style that directly in the member-header.php page.

    I want that kind of thing:

    
    <div><?php 
    if ( bp_is_my_profile() )
      echo "Hi there, since it's your profile, you can edit the value of field number 46";
      echo '<form id="formtochangefield46" name="updatefield46" onSubmit=" " action=" "  method="post" >
      <div>New value</p></div>
      <div><input type="text" id="updatefield46" name="field46" size="20" style="width:150px;"   maxlength="150" value="">
      </div>
      <div><input type="submit" name="submit" value="Submit"></div>
      </form>';
    endif;
    $embed = wp_oembed_get( xprofile_get_field_data('46'), array( ‘width’ => 400 ) );
    echo $embed;
    ?><br></div>
    

    I guess there must already exist wp functions to… call the database and update a field from a form’s submitted values… I mean, how does one creates a custom register page ? It must be the same functions after all…

    I hope my question is more precise… And sorry for my english, I don’t really speak it.


    danbp
    Moderator

    @danbp

    @martinbeaulne,

    Gloabally, each BP member can, by default, modify his profile. So why do you want to add a form for doing this ?

    If you speak french, ask on bp-fr.net, i’ll answer you there.


    martinbeaulne
    Participant

    @martinbeaulne

    If i hide the bottom tabs and the admin bar, each user can’t modify their profile.


    xKroniK13x
    Participant

    @xkronik13x

    If you’re planning on hiding all access to the profile, I’d suggest just adding a link somewhere that takes you to the default edit page. It’s going to be much easier than essentially recoding the entire page – why reinvent the wheel?


    martinbeaulne
    Participant

    @martinbeaulne

    So I guess your answer is « No, there is no simple function to post values in the database ».

    Thanks anyways..


    yasser_148
    Participant

    @yasser_148


    xKroniK13x
    Participant

    @xkronik13x

    Well if you have access to a database, you can easily change the values. The coding would look something like this, I wrote it but didn’t test it. You’d need BP global enabled to fetch the User ID, and you’d also have to set the field ID to change as you needed it to, or just make a loop that iterates through all the available fields. But this will get you on the right path, I think.

    
    <?php
    if(isset($_POST['update']))
    {
    $dbhost = 'DB_HOST'; //Host of BuddyPress Database
    $dbuser = 'DB_USER'; //Username of BuddyPress Database
    $dbpass = 'DB_PASS'; //Password of BuddyPress Database
    $dbname = 'DB_NAME'; //Name of BuddyPress Database
    $field_id = '2'; //ID of the field you are updating
    $user_id = $bp->loggedin_user->userdata->ID; //Must have $bp global enabled, can probably grab the user ID in a different fashion if needed
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    
    $input = $_POST['input'];
    
    //This query will update the field set with the ID set above, you could have easily change it via coding, I just made it static for ease
    $sql = "UPDATE wp_bp_xprofile_data ".
           "SET value = $input ".
           "WHERE field_id = $field_id AND user_id = $user_id" ;
    
    mysql_select_db($dbname);
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not update data: ' . mysql_error());
    }
    echo "Updated data successfully\n";
    mysql_close($conn);
    }
    else
    {
    ?>
    <form method="post" action="<?php $_PHP_SELF ?>">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
    <tr>
    <td width="100">Field data:</td>
    <td><input name="input" type="text" id="input" value="<?php xprofile_get_field_data($field_id); ?>"></td>
    </tr>
    <tr>
    <tr>
    <td width="100"> </td>
    <td> </td>
    </tr>
    <tr>
    <td width="100"> </td>
    <td>
    <input name="update" type="submit" id="update" value="Update">
    </td>
    </tr>
    </table>
    </form>
    <?php
    }
    ?>

    martinbeaulne
    Participant

    @martinbeaulne

    Thanks. I’ll start from there.


    xKroniK13x
    Participant

    @xkronik13x

    I wrote this while I was tired last night, you should cleanse the input on line 15 with:
    $input = htmlspecialchars($_POST['input']);

    That should prevent SQL injections by converting quotes and other special characters into their HTML values… See here for more details.

    Any questions, feel free to ask, I’ll keep an eye on this thread for ya.


    shanebp
    Moderator

    @shanebp

    @xkronik13x’s code is all kinds of bad. @shanebp points out one of the reasons 🙂


    xKroniK13x
    Participant

    @xkronik13x

    My apologies, I’m relatively new to BP and figured it was generic database altering. I’ll have to read over that documentation, I appreciate it!


    martinbeaulne
    Participant

    @martinbeaulne

    Well well. I finally found, and I’m surprised the answer didn’t come from this thread.

    It is quite simple, in fact. Just need to use the function:

    
    xprofile_set_field_data($field, $member-id,  $value);
    

    So, with a simple form sending those three values, one can update a single field anywhere. As I wanted to let members update their fields directly from their member profile ( without using the very bad and obscure edit.php ), I put a form and the function in the member-header.php

    My form and function updates three fields.

    Here is the function, somewhere in the member-header.php file:

    
    <?php
    function updatedemo() {
        $id = $_POST['id-member'];
        $liendemo = $_POST['liendemo'];
        $champdemo = $_POST['champdemo'];
        $titre = $_POST['titre'];
        $champtitre = $_POST['champtitre'];
        $description = $_POST['description'];
        $champdescription = $_POST['champdescription'];
    
        xprofile_set_field_data($champdemo, $id,  $liendemo);
        xprofile_set_field_data($champdescription, $id,  $description);
        xprofile_set_field_data($champtitre, $id,  $titre);
    }
    if(isset($_POST['submit']))
    {
        updatedemo();
        
    }
    ?>
    

    And here is the form:

    
    <?php if ( bp_is_my_profile() ) { ?>
    	<form method="post" action="<?php bp_displayed_user_link(); ?>">
    
    	<?php if ( xprofile_get_field_data('25') == "" ) { ?>
    		<img src="http://www.cinemacontact.com/fr/images/form-demo-01.jpg"><br>
    
    	<?php }; if ( !xprofile_get_field_data('25') == "" ) { ?>
    		<img src="http://www.cinemacontact.com/fr/images/form-demo-01-1.jpg"><br>
    	<?php }; ?>
    
    		<img src="http://www.cinemacontact.com/fr/images/form-demo-02.jpg"><br><input type="text" name="titre" size="40"><br>
    		<img src="http://www.cinemacontact.com/fr/images/form-demo-03.jpg"><br><input type="text" name="liendemo" size="80"><br>
    		<img src="http://www.cinemacontact.com/fr/images/form-demo-04.jpg"><br>
    		<img src="http://www.cinemacontact.com/fr/images/form-demo-05.jpg"><br>
    		<textarea style="width:400px; height:75px;" name="description"></textarea>
    		
    		<input type="hidden" name="champdemo" value="lien-4">
    		<input type="hidden" name="champdescription" value="description-4">
    		<input type="hidden" name="champtitre" value="titre-4">
    		<input type="hidden" name="id-member" value="<?php echo bp_loggedin_user_id(); ?>"><br>
    		<input type="submit" value="Envoyer" name="submit">
    	</form>
    	<?php }; ?></h3>
    

    And this actually works.

    Maybe I didn’t ask correctly at the beginning of the thread. All I wanted to know was:
    « Oh, Martin, there is, indeed, a function to update xprofile fields: xprofile_set_field_data(), don’t need to start with edit.php file. »

    🙂

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Function to update databse fields’ is closed to new replies.
Skip to toolbar