TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

May 18, 2024, 11:39:37 PM

Login with username, password and session length
Members
  • Total Members: 3,886
  • Latest: Grendor
Stats
  • Total Posts: 195,189
  • Total Topics: 21,220
  • Online today: 112
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 1
  • Guests: 85
  • Total: 86
  • @rjen

MySQL and php help, please

Started by JPDeni, August 16, 2006, 10:32:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

JPDeni

I'm using a php article to create a form to add, update and delete records in a database table that I created. Well, actually, there's two tables.

One is an "events" sort of table that lists past performances of the object of my fandom.  :) It is connected to a locations table because those performances are often in the same cities and I don't want to type the same ones in over and over. (Less chance for error.)

Instead of making a separate form to add a new city, I have three input fields for location in the form where I add and edit the events. The first is a drop-down select field that lists all of the cities in the locations table. Below that are two text fields -- one for city and one for state, province or country -- to use if the city is not already on the list.

With me so far? :)

When I add or edit the event record, the script looks to see if there is anything in the two text fields. If there is, it first adds those to the location table and then gets the value of the key field to add to the events table with the rest of the event record. It's adding things just fine. That's not the problem. But I'm getting to it.  ;)

After a record is added, I want to be able to add another one without clicking a link. I want it to build the form and be ready to go. If I don't add a new location when I add a record, the select field with the locations comes back just fine as it should. If I do add a new location, though, when the form builds again, I get an empty select field. What do I need to do?

Here's the code I have to add a new city to the location table:
  if ($location <> '' && $location2 <> '')
  {
   db_query(
   "INSERT
    INTO mts_timeline_location
    SET location_id = '', location='$location', location2='$location2'", __FILE__, __LINE__);
    $query = db_query(
    "SELECT location_id
     FROM mts_timeline_location
     WHERE location='$location'
     AND location2='$location2'", __FILE__, __LINE__);
     $row = mysql_fetch_assoc($query);
     $location_id = $row['location_id'];
     mysql_free_result($query);
  }


And this is the code that builds the select field, which is after the code above:

$query = db_query(
   "SELECT *
    FROM mts_timeline_location
    ORDER BY location", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($query))
  $location[$row['location_id']] = $row['location'] . ', ' . $row['location2'];

and
<SELECT NAME="location_id">';
  echo '<OPTION VALUE="">---</OPTION>';
foreach ($location as $key => $value)
{
  if ($key == $location_id)
    echo '<OPTION SELECTED VALUE="' , $key , '">' , $value , '</OPTION>';
  else
    echo '<OPTION VALUE="' , $key , '">' , $value , '</OPTION>';
}
        echo '</SELECT><BR>
<INPUT TYPE="text" NAME="location"> City<br>
        <INPUT TYPE="text" NAME="location2"> State/Prov/Country


It seems that it doesn't "want" to do another query on the location table after it's added and then looked one up. But I want it to, darn it!  ;) How can I whip it into shape?