Jump to content

[SOLVED] avoiding double rows with same results


uisneach

Recommended Posts

hello :)

I have a form with 6 drop down menu - it's about the chance to enter cancellation of trains

 

I want people entering e.g a day when the trains has been cancelled

 

Every values is taken from the drop down menu like this

the people from the web page mask

will choose from the list in the ddm the day, month, year, scheduled hour of train, direction, if the cancellation has been announced or not (yes/no, the choice in the drop down menu)

So...My purpose is to enter in the mysql table only one time if is the same train.It's possibile, indeed, that e.g. three people enter for the same train, their values. I would like that php recognize the train has already been entered (eg. 1-10-2008, North(direction), 12.00 (hour scheduled)) while the only thing will change will be if has been yes or not (announced).

So, I want only a row in the web page for the same train.

php must be able to recognize taht the train problem hase been already reported, and does not add a new row for the same train.

I create a table with different field in mysql

 

$ day= $_POST[' day']; // it's

$mese= $_POST['']; //month

$year = $_POST['year']; //

$scheduled_time = $_POST['scheduled_time'];  //

$direction  = $_POST['direction ']; //

$announced = $_POST ['announced'];  //cancellation

 

 

$sql = "INSERT INTO cancellation_train (day, month, year, scheduled_time,direction, announced) VALUES ('$day','$month','$year','$scheduled_time','$direction','$announced')";

$result = mysql_query($sql);

 

 

The table "cancellation_train" will have same fields plus id (primary key)

 

I want that php knows  that first 5 fields identify a train and don't print again another row, but count only the number of reports  :)

thanks

uisneach

 

 

Link to comment
Share on other sites

You will need to add a column to the table called something like "report_amt" to keep track of the number of reports.  Then, when a user submits you will query the database for the primary key of the row using all of the posted data except you will leave the column "announced" out since that can differ.  If you get a row back, which if done right, there should only be one, you add 1 to the "report_amt" column.  I forget(I know you can in postgres) if you can create a combined column unique constraint but you may want to do that for all of the columns except "report_amt", "announced" and your primary key.

Link to comment
Share on other sites

$id is ID field form DB in table cancellation_train

 

As you wrote:

The table "cancellation_train" will have same fields plus id (primary key)

 

So, if you want info about some train you do:

 

$id = 4; //this is just an example
$sql = "SELECT * FROM  train_cancellation WHERE id = '4' ORDER BY id, day, month, year, hour, direction";

 

Hope it's more clear now...

 

 

Link to comment
Share on other sites

Just a sudocode tip.

 

Since you are ordering by the keys, as you loop thru the result, you can save the keys concatenated into another var "saveKeys"

The before you print it check if the key has changed.

 

--- in the result loop ---

If saveKkeys != key1.key2...keyx

{  print records

  saveKeys=key1.key2...keyx

--- in the result loop ---

}

 

hug

Link to comment
Share on other sites

@budimir  ??? sorry but I am really poor in programming

The problem for me is to print a combination only if the sequence is not present yet

1|10|2008|12.00|North|yes

could be the entry of an user. (corresp to day, month, year, scheduled_time, direction, announced ARE ALL fields of drop down menu and columns in the table "train_cancelled" with same name.

I don't want to print id cause i don't care about it. but probabily you said sometihing right I missed, sorry, i really don't get it.

anyway, if php found a record with all the 5 entries already in the table(same combination) , I don't want it to be printed on the screen

the ideal would be not to add in the table another record, not only not printing on the screen as if the combination is already present, i am interested only in the number of yes/not

@livetolove: thanks you too.

i tried to create a variable savekeys with concatenation like that, but

it doesn't insert any record on table, even if the combination is not present

My brain is grilling, I knock off just 10 minutes

see ya late, and thanks lads ;)

sorry!

 

Link to comment
Share on other sites

Hello eb

:)

This is the solution, (after long time spent, eventually!!)

it was necessary to use the SELECT . In this way, php recognize the rows inserted where the considered values (in a row) are inserted (already) as it means a train has been already reported

 

thx for eb for effort to help me anyway I appreciated :)...Here it is

 

 

$sql11="SELECT * FROM train_delays WHERE day = '$day' AND month = '$month' AND year = '$year' AND hour = '$hour' AND direction = '$direction'";

$result = mysql_query ($sql11);

$num_rows = mysql_num_rows($result);

:)

 

the prob is solved

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.