0

I have a form on a page that populates the database table:

<table border="1">
  <tr>
    <td align="center">Form Input Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
        <form method="post" action="input.php">

        <tr>
          <td>Product Name</td>
          <td><input type="text" name="name" size="20">
          </td>
        </tr>
        <tr>
          <td>Brand</td>
          <td><input type="text" name="brand" size="40">
          </td>
        </tr>
        <tr>
          <td></td>
          <td align="right"><input type="submit" name="submit" value="Sent"></td>
        </tr>
        </table>
      </td>
    </tr>
</table>

And here is input.php:

<?
//the example of inserting data with variable from HTML form
//input.php
mysql_connect("localhost","xxx","xxx");//database connection
mysql_select_db("xxxxxxx");




$current_user= wp_get_current_user();
$id= $current_user->user_login; 


//inserting data order
$order = "INSERT INTO wp_userdata
            (id, product_name, product_brand)
            VALUES
            ('$id',
                        '$_POST[name]',
            '$_POST[brand]')";

//declare in the order variable
$result = mysql_query($order);  //order executes
if($result){
    echo("<br>Input data is succeed");
} else{
    echo("<br>Input data is fail");
}
echo $id;
?>

I am trying to pass username to a database as "id". I am getting next error:

Fatal error: Call to undefined function wp_get_current_user() in /home/xxx/input.php on line 10

I tried to change things around but still not working..

  • 1
    Does input.php load any of the WordPress functionality? wp_get_current_user() is a WordPress function, so you need to load up WordPress before you use it. Also, don't use PHP short tags -- use <?php instead of <? in your code. And last, check out the wpdb class, which simplifies interacting with the WordPress DB. – Pat J Aug 8 '14 at 17:46
  • Ok I moved the wp_get_current_user() bit to the top of first file and now it works, however it doesn't pass the username value – Acidon Aug 8 '14 at 18:06
1

Wordpress doesn't work this way. If you want to work with a framework like Wordpress, you should obey its rules!

Of course you can add a database record like this, but it is a really bad approach. Try some plugins for creating and saving forms in Wordpress like Ninja Forms or Quform. You can even do it yourself, but its not recommended for Wordpress newbies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.