Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Letting users add content 1

Status
Not open for further replies.

SLMHC

MIS
Jul 23, 2004
274
CA
I would like to give my users the ability to add content to my site. the amount of stats that they want on our baseball site is too much for me to keep up with, hence wanting to give controll of the content to them.

I have setup a database and one test table. I am able to add data and to display it. i am havin some trouble with extra data being entered when i do not want it. i have an admin area where ive setup two links to the two areas i have tables to input data to. when i click on the links, not the submit buttons on the forms, it enters null data into the DB. im not sure why it is doing this.

here is the code im using:
Code:
<?php

$username="user";
$password="pass";
$database="db";

$Name=$_POST['Name'];
$URL=$_POST['URL'];
$Team=$_POST['Team'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO hof_player VALUES ('','$Name','$URL','$Team')";
mysql_query($query);

mysql_close();
?>

<h3>Add HOF Player to Database</h3>

<form action="<?php echo $PHP_SELF ?>" method="post">
Player Name: <input type="text" name="Name"><br>
Player Page: <input type="text" name="URL"><br>
Inducted Team: 
<SELECT name="Team">
<option SELECTED>Choose a Team</option>
<OPTION value="ana">Anaheim</option>
<OPTION value="ari">Arizona</option>
<OPTION value="atl">Atlanta</option>
<OPTION value="bal">Baltimore</option>
<OPTION value="bos">Boston</option>
<OPTION value="chn">Chicago N</option>
<OPTION value="chw">Chicago A</option>
<OPTION value="cinn">Cinncinati</option>
<OPTION value="clev">Cleveland</option>
<OPTION value="col">Colorado</option>
<OPTION value="det">Detroit</option>
<OPTION value="fla">Florida</option>
<OPTION value="hou">Houtson</option>
<OPTION value="kc">Kansas City</option>
<OPTION value="la">Los Angeles</option>
<OPTION value="mil">Milwaukee</option>
<OPTION value="min">Minnisota</option>
<OPTION value="mon">Montreal</option>
<OPTION value="nym">New York N</option>
<OPTION value="nyy">New York A</option>
<OPTION value="oak">Oakland</option>
<OPTION value="phi">Philiadelphia</option>
<OPTION value="pit">Pittsburgh</option>
<OPTION value="sd">San Diego</option>
<OPTION value="sf">San Francisco</option>
<OPTION value="sea">Seattle</option>
<OPTION value="stl">St. Louis</option>
<OPTION value="tb">Tampa Bay</option>
<OPTION value="tex">Texas</option>
<OPTION value="tor">Toronto</option>
<OPTION value="was">Washington</option>
</SELECT>
<br/>
<input type="Submit" Value="Enter Player">
</form>

This adds a player to the hall of fame page. the other bit of code adds a GM to th hall of fame. Once i get this working correctly i will use this to have users add stats and edit errors as well.

-Dave
 
You need to first check that the form has been posted before you run db queries. Try wrapping your php code at the top of the page in an 'if' statement like this:
Code:
if (count($_POST) != 0) {
  $Name=$_POST['Name'];
  $URL=$_POST['URL'];
  $Team=$_POST['Team'];

  mysql_connect(localhost,$username,$password);
  @mysql_select_db($database) or die( "Unable to select database");

  $query = "INSERT INTO hof_player VALUES ('','$Name','$URL','$Team')";
  mysql_query($query);

  mysql_close();
}

Hope this helps...
Itshim
 
Just to be clear, am i replacing my code with yours, or adding to it?

-Dave
 
I changed the bit of code you gave me and so far its working great!

thanks alot!

-Dave
 
The other part I was having trouble with was letting users edit records that are in the DB already, ie change or delete records.

The tutorial i read yesterday wasnt too clear on that part of things.

-Dave
 
To delete or update records in the db, you just need to create update and delete statements from the information you recieve by way of the submitted form. Some how you will need to determine what action the user is performing.

For a page like yours, I usually have a select menu which starts with an empty option, then lists all the records which can be updated or deleted, then the form to fill out below.

When the page first loads the empty option is selected, with a blank form, if they choose a record from the select menu then the form fills in with the current values, and a delete checkbox appears.

After checking that the form has been submitted, I check the select menu for a value, if the value is empty I know we need to insert, if it has a value, I then check if the delete box is checked, if so, then we are deleting, if not we are updating.

Hope this helps,
Itshim
 
the tutorial i read did not go this indepth. do you know of any good places for tutorials, examples of code?

-Dave
 
I found this tutorial: Content Management System (CMS) Using PHP And MySQL I believe it should give you all the tools you are looking for. You may not want to use everything in the tutorial, but it does show you how to allow users to insert, update, and delete content.

The tutorial is on There are other tutorials you my find useful also.

Hope this helps,
Itshim
 
I am having one hell of a time wrapping my head around updating table entries. The tutorial on creating a CMS is nice but i cannot adapt it to suit my needs. my skill is not quite there yet.

it would be nice, like in Itshim's example is the add and edit/delete records page was the same. It would be even better if the page displayed all the records as well.

I think the biggest hurdle is that in every article i read each programmer uses a different ways to do the same thing.

-Dave
 
ok hasty last comment...it seems if you actually read the article and look at the complete source code it makes more sense...thank you again Itshim...i may call on your talents again.

-Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top