Jump to content


Photo

flexible in loop in array

php array

  • Please log in to reply
15 replies to this topic

#1 sigmahokies

sigmahokies
  • Members
  • PipPipPip
  • Advanced Member
  • 125 posts
  • LocationRichmond, VA

Posted 12 April 2018 - 09:51 PM

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, 12 April 2018 - 09:53 PM.


#2 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,817 posts
  • LocationWA

Posted 13 April 2018 - 03:08 AM

$input = "<tr><td>{$row5}:</td><td colspan='2'><input type='text' name='label$j'></td></tr>";
Simple: don't do that. Name the input label[] and forget $j. Then $_POST[label] will be an array you can foreach over.
The Reimann Zeta Function Trolley Problem | If you want to escape with me, come take my hand...

#3 sigmahokies

sigmahokies
  • Members
  • PipPipPip
  • Advanced Member
  • 125 posts
  • LocationRichmond, VA

Posted 13 April 2018 - 12:37 PM

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



#4 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,817 posts
  • LocationWA

Posted 13 April 2018 - 12:58 PM

Right... Did you submit the form and look in $_POST to see what you received?
The Reimann Zeta Function Trolley Problem | If you want to escape with me, come take my hand...

#5 sigmahokies

sigmahokies
  • Members
  • PipPipPip
  • Advanced Member
  • 125 posts
  • LocationRichmond, VA

Posted 13 April 2018 - 01:51 PM

It doesn't work, the data did not get in the Database.



#6 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,817 posts
  • LocationWA

Posted 13 April 2018 - 02:19 PM

Not what I asked.

Did you submit the form and look in $_POST to see what you received?
The Reimann Zeta Function Trolley Problem | If you want to escape with me, come take my hand...

#7 sigmahokies

sigmahokies
  • Members
  • PipPipPip
  • Advanced Member
  • 125 posts
  • LocationRichmond, VA

Posted 13 April 2018 - 03:15 PM

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



#8 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,817 posts
  • LocationWA

Posted 13 April 2018 - 03:47 PM

var_dump($_POST);

The Reimann Zeta Function Trolley Problem | If you want to escape with me, come take my hand...

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,110 posts

Posted 13 April 2018 - 04:12 PM

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

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 sigmahokies

sigmahokies
  • Members
  • PipPipPip
  • Advanced Member
  • 125 posts
  • LocationRichmond, VA

Posted 15 April 2018 - 05:11 PM

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



#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,110 posts

Posted 15 April 2018 - 06:53 PM

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


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,110 posts

Posted 15 April 2018 - 08:49 PM

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.


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 dodgeitorelse3

dodgeitorelse3
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationMassena, NY

Posted 15 April 2018 - 10:30 PM

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?



#14 sigmahokies

sigmahokies
  • Members
  • PipPipPip
  • Advanced Member
  • 125 posts
  • LocationRichmond, VA

Posted 16 April 2018 - 03:35 PM

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.



#15 dodgeitorelse3

dodgeitorelse3
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationMassena, NY

Posted 16 April 2018 - 07:04 PM

Please show code you have in addname.php

#16 sigmahokies

sigmahokies
  • Members
  • PipPipPip
  • Advanced Member
  • 125 posts
  • LocationRichmond, VA

Posted 17 April 2018 - 12:35 AM

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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users