Jump to content

update multiple tables


adzie

Recommended Posts

Hi Guys,

 

working on a membership database system, the question is if someone amends a record and then commits those changes to the database is it possible to also uipdate another database to record the change and date of change.

 

for example

 

members database

name - text

address - text

level - drop down options

loudness -text

 

levelchange database

prev level

new level

date

 

what I'd like to do is when the when they choose a new option from level it updates the members database  but also puts a entry into the levelchange database showing previous level the new option selected and the date actioned

 

What i'm concerned about is that everytime they submit changes to their profile it might put an entry into that levelchange table even if the level field does not change.

 

is this possible to be done?

 

thanks for any advice :)

Link to comment
Share on other sites

I wouldn't go at it with a new database so much as a new table,

 

in your 2nd table you'd want 2 more columns than you have,

 

1: ID

2: user_ID

3: prev_lvl

4: new_lvl

5: timestamp

 

might be able to do it in a single query though that's beyond my mySQL knowledge,

doing it in 2 queries would be simple enough

Link to comment
Share on other sites

amites is correct. you need additional columns in your levelchange table. an id (primary key) and the member id (foreign key).

 

This is off the top of my head - the code is not complete but should get you going in the right direction

 

<?php
// get the member id from the form
$id = $_POST['member_id'];

// get the values from the db. Select the columns you need
$sql = mysql_query("SELECT prev_level FROM members WHERE id = {$id}");
$row_members = mysql_fetch_array($sql);

// check to make sure the level change was made
if($_POST['level'] != $row_members['level']) {
    // a change was made
    // update the members table
    $sql = mysql_query("UPDATE members SET level = $_POST['level'] WHERE member_id = {$id}");

    // update the levelchange table
    $sql = mysql_query("UPDATE levelchange SET new_level = $_POST['level'], prev_level = SELECT level FROM members WHERE member_id = {$id}");
}
?>

Link to comment
Share on other sites

I found a couple of mistakes in my earlier post. I wasn't able to edit them though

 

amites is correct. you need additional columns in your levelchange table. an id (primary key) and the member id (foreign key).

 

This is off the top of my head - the code is not complete but should get you going in the right direction

 

<?php
// get the member id from the form
$id = $_POST['member_id'];

// get the values from the db. Select the columns you need
$sql = mysql_query("SELECT level FROM members WHERE id = {$id}");
$row_members = mysql_fetch_array($sql);

// check to make sure the level change was made
if($_POST['level'] != $row_members['level']) {
    // a change was made
    // update the members table
    $sql = mysql_query("UPDATE members SET level = $_POST['level'] WHERE member_id = {$id}");

    // update the levelchange table
    $sql = mysql_query("UPDATE levelchange SET new_level = $_POST['level'], prev_level = $row_members['level'] WHERE member_id = {$id}");
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.