Page 1 of 1

Adding information from one database to another

Posted: Wed Jan 19, 2011 1:55 am
by brassfid
Alright, I want to snag the current users name when they entering a new beer into my system. The user has to log on to even see this page. At the top I would like their username to appear, and when the code is submitted for their name to then be pushed back to my "Beer" database. The username I assume is in another database. I am using wordpress.

PHP

Code: Select all

<?php

$con = mysql_connect("localhost","UN","PASS");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DB", $con);

$beername = mysql_real_escape_string($_POST['beername']);
$brewery = mysql_real_escape_string($_POST['brewery']);
$style = mysql_real_escape_string($_POST['style']);
$aroma = mysql_real_escape_string($_POST['aroma']);
$appearance = mysql_real_escape_string($_POST['appearance']);
$flavor = mysql_real_escape_string($_POST['flavor']);
$mouthfeel = mysql_real_escape_string($_POST['mouthfeel']);
$overallimpression = mysql_real_escape_string($_POST['overallimpression']);

$sql="INSERT INTO Beers (beername, brewery, style, aroma, appearance, flavor, mouthfeel, overallimpression)
VALUES
('$beername','$brewery','$style', '$aroma', '$appearance', '$flavor', '$mouthfeel', '$overallimpression')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>
HTML

Code: Select all

<form action="/wp-content/insert.php" method="post">

//I WOULD LIKE THEIR NAME TO BE PUT IN HERE
<label for="username">User Name: </label>
<input id="username" name="username" type="text" />

<label for="beername">Beer Name: </label>
<input id="beername" name="beername" type="text" />

<label for="brewery">Brewery: </label>
<input id="brewery" name="brewery" type="text" />

<label for="style">Style: </label>
<input id="style" name="style" type="text" />

Aroma

<textarea cols="20" rows="4" name="aroma"></textarea>

Appearance

<textarea cols="20" rows="4" name="appearance"></textarea>

Flavor

<textarea cols="20" rows="4" name="flavor"></textarea>

Mouth Feel

<textarea cols="20" rows="4" name="mouthfeel"></textarea>

Overall Impression

<textarea cols="20" rows="4" name="overallimpression"></textarea>
<input name="submitbeer" type="SUBMIT" value="SUBMIT" />
</form>

Re: Adding information from one database to another

Posted: Wed Jan 19, 2011 11:39 am
by social_experiment
I don't know how username values are stored in wordpress but you have to put that value into the value attribute of the username input tag

Code: Select all

<label for="username">User Name: </label>
<input id="username" name="username" type="text" value="UserNameHere" />

Re: Adding information from one database to another

Posted: Fri Jan 21, 2011 9:00 pm
by brassfid
Thank you very much. This was the code I ended up with if someone ends up here with a Similiar issue. This is pulling the current user name from word press and putting it into a form. Let me know if this is proper coding, it could be just all spliced poorly together.

Code: Select all

<label for="username">Username: </label>
<input id="username" name="username" type="text" readonly="readonly" value="<?php global $current_user;
      get_currentuserinfo();

      echo $current_user->user_login;
?>"