Jump to content

Calling Information From Table With A Form


jkanclerz

Recommended Posts

Complete noobie with PHP.

 

Overview:

 

My company is running a travel incentive program. Each month our customers receive a point statement showing how much credit their account has earned toward the trip. I've set up a Wordpress site to keep them informed about the trip.

 

What I want to do:

 

In sidebar.php of WP I would like to have a simple one line form, where the customer enters their account number and it retrieves the points/credit they've earned.

 

I can create a new table in MySQL, calling it "Points" with 2 columns, the primary key being their account number, and the second column, "Points."

 

My problem is I'm not sure how to call this data from the table with PHP. I'm thinking it should be fairly straightforward, especially since I'm not concerned with securing this information.

 

Your help with this is appreciated.

 

Cheers,

 

Jesse

what you need to do is have a text box so they can enter it and submit it to a page. I would use a post method.

 

then on the page you select them to go to you would assign the post value.

<?php
// set your database variables
$dbhost = 'localhost';	 // database server 
$dbuser = 'username';  // Username
$dbpass = 'password';  // Password
$dbname = 'test';        // db name
// connect to the database
$mysql_conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to MySql, check host, username, password <br />".mysql_error());
@mysql_select_db($dbname, $mysql_conn) or die("Could not connect to database <br />".mysql_error());

$account = $_POST['account_num'];
$sql = "SELECT * FROM `Points` WHERE `account_num` = '$account'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);
echo "account number = {$row['account_num']} Points = {$row['Points']}";
?>

 

Obviously it would be much more fancy. I would suggest though having a primary key as an auto number then having the 2 fields. Cause if you want to get fancy with it later, it would be much easier to link tables together through auto numbers rather than account numbers.

 

Ray

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.