darbok 1 Posted May 12, 2017 Report Share Posted May 12, 2017 I was wondering when one has a php form with like 30 entries how does one organize or manage such a large list of entries to pass to mysql? Quote Link to post Share on other sites
niche 143 Posted May 12, 2017 Report Share Posted May 12, 2017 The answer to your specific question is you'd use a $_GET or $_POST method. Quote Link to post Share on other sites
niche 143 Posted May 12, 2017 Report Share Posted May 12, 2017 This will get you started: https://www.w3schools.com/php/php_forms.asp Quote Link to post Share on other sites
darbok 1 Posted May 15, 2017 Author Report Share Posted May 15, 2017 On 5/11/2017 at 9:15 PM, niche said: The answer to your specific question is you'd use a $_GET or $_POST method. I think i explained myself wrong possibly. I understand about submitting to mysql from php... but from what iv'e seen you need to list all the columns in the sql tables that the info has to go in from the php form. I was wondering if there was some way to do this without having to list all 30 fields that the php/sql connect statement has to post to individual. Quote Link to post Share on other sites
justsomeguy 1,135 Posted May 15, 2017 Report Share Posted May 15, 2017 It's best to list the fields. You can leave out the field list and just have the values in the insert query, but if you ever modify the table it's going to break the code. It's always best to list out what you're doing to avoid issues like that. Quote Link to post Share on other sites
darbok 1 Posted May 19, 2017 Author Report Share Posted May 19, 2017 On 5/15/2017 at 3:32 PM, justsomeguy said: It's best to list the fields. You can leave out the field list and just have the values in the insert query, but if you ever modify the table it's going to break the code. It's always best to list out what you're doing to avoid issues like that. So it's best to just it up and list all the fields? Quote Link to post Share on other sites
dsonesuk 929 Posted May 19, 2017 Report Share Posted May 19, 2017 I suppose you can use sql 'SHOW COLUMNS' to do the listing for you http://php.net/manual/en/function.mysql-list-fields.php Quote Link to post Share on other sites
dsonesuk 929 Posted May 19, 2017 Report Share Posted May 19, 2017 (edited) Rough example $sql = "SHOW COLUMNS FROM TableName"; $stmt = $pdo->prepare($sql); $stmt->execute(); $arr = []; while ($row = $stmt->fetchObject()) { //check if column is non auto_increment if ($row->Extra !== 'auto_increment') { $arr[] = $row->Field; } } $convert_arr = implode(', ', $arr); $ins_sql = "INSERT INTO TableName ($convert_arr)VALUES (11, 4)"; $pdo->exec($ins_sql); You could even use column names retrieved to retrieve $_POST/$_GET name values of the same named index. Edited May 19, 2017 by dsonesuk Quote Link to post Share on other sites
justsomeguy 1,135 Posted May 19, 2017 Report Share Posted May 19, 2017 So it's best to just it up and list all the fields? Yep. The alternative is to not list the columns in the table and only give the values: INSERT INTO table VALUES ('val1', 'val2', 'val3') But, like I said, if you ever change the structure of that table then that query is going to break if you've changed the number of columns. It's best to just list everything out explicitly. You don't have to pay per character in your source code, make the code as obvious as possible so that it's easy to maintain. Ease of maintenance should always be a higher priority than something like how long the code is. 1 Quote Link to post Share on other sites
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.