SalientAnimal Posted October 24, 2012 Share Posted October 24, 2012 Hi All, I have a PHP form that submits data to the database table. Everything is working however, for the purpose of having a better structure in the table I would like to make a change. On one part of the form the user would select a Visit Reason. If the user selects Sale they are given the "Primary" sales reasons from a dynamic drop-down box. As a sale can contain more than one type of sale e.g handset/accessory I have also added additional tick boxes to add any additional items that may for part of that sale. Currently the "Primary" sale reason is writing to one column in my table, and then each of the tick boxes will also write to their own individual boxes. What this means is Accessory can be written as the primary reason, and then again in its own field in the table. How can I get each of the fields from the drop-down to write to their individual table fields rather than have an additional field? Also, is it able to activate the radio buttons only if sale is selected from the drop-down? Here is submit code below: <?php$con = mysql_connect("localhost","username","password"); if (!$con) {die('Could not connect: ' . mysql_error()); } mysql_select_db("store", $con); $sql="INSERT INTO store_traffic (username , gender , ethnic_group , age_group , requirement , visit_detail , accessory , airtime , handset , postpaid , prepaid , handset_make) VALUES ('$_POST[username]' ,'$_POST[gender]' ,'$_POST[ethnic_group]' ,'$_POST[age_group]' ,'$_POST[requirement]' ,'$_POST[visit_detail]' ,'$_POST[accessory]' ,'$_POST[airtime]' ,'$_POST[handset]' ,'$_POST[postpaid]' ,'$_POST[prepaid]' ,'$_POST[handset_make]')"; if (!mysql_query($sql,$con)) {die('Error: ' . mysql_error()); } echo "<b><font color='white' face='segoe' size='2'>1 record added</b></font>"; include "redirect_store.html";mysql_close($con)?> Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/ Share on other sites More sharing options...
White_Lily Posted October 24, 2012 Share Posted October 24, 2012 (edited) If I'm reading it correctly, try using update rather than insert. Update, updates (obviously) existing entries, whereas insert creates a new row for each entry. Also, since functions such as select, insert, update, delete, etc are very common functions to use in php - can I suggest that you write custom functions for them? Selecting: //Select function select($row = "*", $table, $where = NULL, $order = NULL, $limit = NULL) { $query = "SELECT ".$row." FROM ".$table; if($where != NULL){ $query .= " WHERE ".$where; }if($order != NULL){ $query .= " ORDER BY ".$order; }if($limit != NULL){ $query .= " LIMIT ".$limit; } $result = mysql_query($query); return $result; } Calling the function: $welcome = select("*", "welcome"); $rowWelcome = mysql_fetch_assoc($welcome); echo $rowWelcome["text"]; Edited October 24, 2012 by White_Lily Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387397 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) I'm not sure how to explain it, but its not an update function it a new entry being written to the database each time the user clicks submit. Maybe this table will explain it better? Ok the table didn't work, so I had to remove the comment. Let me send you an attachment Edited October 24, 2012 by SalientAnimal Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387401 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) Here is a document with a description. Hope it explains better. visit.doc Edited October 24, 2012 by SalientAnimal Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387402 Share on other sites More sharing options...
White_Lily Posted October 24, 2012 Share Posted October 24, 2012 are you not able to just delete the column? If not try deleting the table and re-creating it without that column? Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387403 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 I can easily delete it, I'm just not sure how to have the data written to those other columns from the submit script, rather writing to the visit_detail column. The drop-down needs to be there, as it defines a whole lot of dynamic drop-downs, but the check boxes are also required, as there might be additional reasons for the visit. Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387413 Share on other sites More sharing options...
MDCode Posted October 24, 2012 Share Posted October 24, 2012 I'm not really following your descriptions well, but here's a guess $sql = "INSERT INTO `table` (column_name) VALUES ('Value')"; This will insert into only one column, the rest will be filled with null Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387417 Share on other sites More sharing options...
White_Lily Posted October 24, 2012 Share Posted October 24, 2012 ^ they will only fill with NULL if his database columns have that box ticked - if not they will just be blank. Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387422 Share on other sites More sharing options...
MDCode Posted October 24, 2012 Share Posted October 24, 2012 Eh, was worth a try I suppose *shutting up* Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387426 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 Mmmm... I don't think I'm explaining it properly, but also don't know how to. And then again I also don't want to go and type a long message that no one will read trying to explain it. I can maybe try do a flow diagram in word/excel if that will help explain it? Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387430 Share on other sites More sharing options...
SalientAnimal Posted October 24, 2012 Author Share Posted October 24, 2012 Hi All, I have included an image that will hopefully show what I am trying to achieve: Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387434 Share on other sites More sharing options...
kicken Posted October 24, 2012 Share Posted October 24, 2012 Why do you have visit_detail at all if it is just a duplicate of the other columns? Why not just remove the column all together and if that field is required, require them to check at least one of the other boxes? Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387485 Share on other sites More sharing options...
SalientAnimal Posted October 25, 2012 Author Share Posted October 25, 2012 That field forms part of a dynamic drop-down list. As depicted in the image. It is the first part of the dynamic drop-down, with two other dynamic drop-downs that are dependant on it. Quote Link to comment https://forums.phpfreaks.com/topic/269852-add-data-to-specific-database-table-column/#findComment-1387632 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.