sigmahokies Posted April 12, 2018 Share Posted April 12, 2018 (edited) Hello everyone, I'm getting there, but not perfect. Still learning to do write the script in PHP. Anyway, I am trying to get set up the flexible array and values in html and SQL. I am trying to make different name, cannot same name in loop. For example, in PHP, to get data value from $_POST from name from input in form area in html, so I can't figure how to get like array. In loop, look like: using for or foreach loop, I need to have <input type="text" name="label$j"> in different number, like to have loop name = label1, label2, label3... But I found php print the loop same - label1, label1, label1...I don't want that, because it will conflict to insert in the data in the database. here my code: <!doctype html> <html> <head> <title>Add name and number</title> <link href="defaultdatabase.css" rel="stylesheet" type="text/css"> </head> <h2>Add any DSDJ information to database</h2> <?php require ("require2.php"); $sql = "show tables from XXXX"; $list = mysqli_query($GaryDB, $sql); while ($row = mysqli_fetch_array($list)) { $table[] = $row[0]; } $option = ''; foreach ($table as $rows) { $option .= "<option value='{$rows}'>{$rows}</option>"; } ?> <form action="addname.php" method="post"> <table> <tr><th>Select the table</td><td> <select name="subject"> <?php echo $option; ?> </select></td><td><input type="submit" name="selected" value="select"></td></tr> </table> </form> <form> <table> <?php if (isset($_POST['selected'])) { $selected = $_POST['subject']; $column = "select column_name from information_schema.columns where table_name = '" . $selected . "'"; $list5 = mysqli_query($GaryDB, $column); while ($array = mysqli_fetch_array($list5)) { $input = ''; $j = 0; foreach ($array as $row5) { $input = "<tr><td>{$row5}:</td><td colspan='2'><input type='text' name='label$j'></td></tr>"; $j++; } echo $input; } if (isset($_POST['insert'])) { foreach ($array as $row6) { $ins = "{$row6},"; } foreach ($_POST['label'] as $row7) { $ins5 = "'{$row7}',"; } $insert = "insert into " . $selected . " (" . $ins . ") values (" . $ins5 . ")"; $added = mysqli_query($GaryDB,$insert); if($added) { echo "<tr><td>Data are insert into Database</td></tr>"; } else { echo "<tr><td>Data did not get in the Database</td></tr>"; } } }echo "<tr><td><input type='submit' name='insert' value='Add data in database'></td></tr>"; ?> </table> </form> </html> So, in result: <tr><td>Articles_ID:</td><td colspan='2'><input type='text' name='label1'></td></tr><tr><td>Subject_ID:</td><td colspan='2'><input type='text' name='label1'></td></tr><tr><td>ID:</td><td colspan='2'><input type='text' name='label1'></td></tr><tr><td>LastName:</td><td colspan='2'><input type='text' name='label1'></td></tr><tr><td><input type='submit' name='insert' value='Add data in database'></td></tr> Look at result name in label1, label1, label1, ...that is no no no. I want to have label1, label2, label3...can you help? Thank you so much! Gary Taylor Edited April 12, 2018 by sigmahokies Quote Link to comment Share on other sites More sharing options...
requinix Posted April 13, 2018 Share Posted April 13, 2018 $input = " {$row5}: ";Simple: don't do that. Name the input label[] and forget $j. Then $_POST[label] will be an array you can foreach over. Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted April 13, 2018 Author Share Posted April 13, 2018 I just tried that as soon as you posted, seem it doesn't work. No error message, but result showed same - label[], label[], label[]. Not label0, label1, label2... Quote Link to comment Share on other sites More sharing options...
requinix Posted April 13, 2018 Share Posted April 13, 2018 Right... Did you submit the form and look in $_POST to see what you received? Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted April 13, 2018 Author Share Posted April 13, 2018 It doesn't work, the data did not get in the Database. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 13, 2018 Share Posted April 13, 2018 Not what I asked. Did you submit the form and look in $_POST to see what you received? Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted April 13, 2018 Author Share Posted April 13, 2018 I cannot see what is inside $_POST in form during php is interpreting. if there is possible to see what is inside $_POST, Please show me how... Gary Quote Link to comment Share on other sites More sharing options...
requinix Posted April 13, 2018 Share Posted April 13, 2018 var_dump($_POST); Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2018 Share Posted April 13, 2018 ... or, if you are interested purely in the content and not the sizes and types, use echo '<pre>', print_r($_POST, 1), '</pre>'; Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted April 15, 2018 Author Share Posted April 15, 2018 Barand, I tried that. I don't see any inside post in php. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 15, 2018 Share Posted April 15, 2018 Did you submit the form so that POST data gets created? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 15, 2018 Share Posted April 15, 2018 You need to think about what your code is doing, or rather, what your code should be doing. The code has three distinct steps create form listing table names for selection using selected tablename, list fields for data input update the table with the data from step 2 Your code assumes all these three things happen at the same time - they don't. You might find it easier to split these actions into three separate pages. Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted April 15, 2018 Share Posted April 15, 2018 your form is posting to addname.php but it looks like you are trying to add the data to the database on the same page as form. What is your code that is on addname.php page? Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted April 16, 2018 Author Share Posted April 16, 2018 Hi dodgeitorelse3, Yes. I am trying to add the data into the database. I need to make a flexible in array and column that can insert or update into database with any number of columns. For example, If table has three columns, then program can set the value to store data tp update or insert three data from input in HTML. Please forgive me, I'm ASL user, so English is my second language, I am doing my best to make this post is understandable. Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted April 16, 2018 Share Posted April 16, 2018 Please show code you have in addname.php Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted April 17, 2018 Author Share Posted April 17, 2018 Dodgeitorelse, it is above already. it is my code in php. it is on first I post in this thread. Quote Link to comment 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.