Jump to content

flexible in loop in array


sigmahokies

Recommended Posts

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

Link to comment
Share on other sites

You need to think about what your code is doing, or rather, what your code should be doing.

 

The code has three distinct steps

  1. create form listing table names for selection
  2. using selected tablename, list fields for data input
  3. 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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