Adding information from one database to another

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brassfid
Forum Newbie
Posts: 18
Joined: Thu Jan 13, 2011 11:09 pm

Adding information from one database to another

Post 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>
Last edited by Benjamin on Wed Jan 19, 2011 2:42 am, edited 1 time in total.
Reason: Added [syntax=html] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Adding information from one database to another

Post 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" />
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
brassfid
Forum Newbie
Posts: 18
Joined: Thu Jan 13, 2011 11:09 pm

Re: Adding information from one database to another

Post 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;
?>"
Post Reply