Jump to content
sigmahokies

flexible in loop in array

Recommended Posts

Posted (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 by sigmahokies

Share this post


Link to post
Share on other sites
$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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Right... Did you submit the form and look in $_POST to see what you received?

Share this post


Link to post
Share on other sites

Not what I asked.

 

Did you submit the form and look in $_POST to see what you received?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
var_dump($_POST);

Share this post


Link to post
Share on other sites

... or, if you are interested purely in the content and not the sizes and types, use

 

echo '<pre>', print_r($_POST, 1), '</pre>';

Share this post


Link to post
Share on other sites

Barand, I tried that. I don't see any inside post in php. 

Share this post


Link to post
Share on other sites

Did you submit the form so that POST data gets created?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Dodgeitorelse, it is above already. it is my code in php. it is on first I post in this thread.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.