Jump to content

Flexible in array for table in the Database


Sigmahokies

Recommended Posts

Hi everyone,

I am trying to set up the flexible with array, like set up the array by count the columns in tables in the Database, so I am still figure how to create variable word through loop to allow equalized from $_post as if $_post appear in two or three or four...I figured out to set up the $_post in loop, but I haven't find a way to have create new variable to equalized with $_post in loop in same time.

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 NewDSDJ";
	    $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 = '';
	            foreach ($array as $row5) {
	                $input = "<tr><td>{$row5}:</td><td colspan='2'><input type='text' name='label[]'></td></tr>";
	            }
	            echo $input;
	        }
	        if (isset($_POST['insert'])) {
	            foreach ($array as $row6) {
	                $ins = "{$row6},";
	            }
	            for ($i = 0; $i < count($array); $i++) {
	                $label = $_POST['label'];
	            }
	            echo $label;
	            $insert = "insert into " . $selected . " (" . $ins . ") values (" . $label . ")";
	            mysqli_query($GaryDB, $insert) or die("Could not insert");
	        }
	    }echo "<tr><td><input type='submit' name='insert'></td></tr>";
	    mysqli_close($GaryDB);
	    ?>
	    </table>
	</form>
	</html>
	

Look at above of $insert, that is one I haven't figure how to create variable word to get data from name in form in HTML. Can you help?

Thanks,

Gary Taylor

Link to comment
Share on other sites

It's hard to understand what you're asking for, but there are a few problems with that code.

                foreach ($array as $row5) {
                    $input = "<tr><td>{$row5}:</td><td colspan='2'><input type='text' name='label[]'></td></tr>";
                }

That's going to overwrite $input every time through the loop, it's only going to contain the text for the last item in the array.

                foreach ($array as $row6) {
                    $ins = "{$row6},";
                }

That's the same problem, you overwrite $ins every time through the loop.  It's only going to have the last value.  And, since you didn't set $array to anything else, it's going to be the last row in the SQL result set.

                for ($i = 0; $i < count($array); $i++) {
                    $label = $_POST['label'];
                }

That loop doesn't do anything, you're not using $i inside the loop.  You just keep setting $label to the same value from $_POST.

Link to comment
Share on other sites

hi just someguy,

please forgive me for my grammar, I'm Deaf, so ASL is my prime language, English is my second language. Anyway, I'm trying to create new variable by loop which must be follow number of array. In the many database, there are various number of columns, so I need to set up the flexible array that can insert in value in the database. For example, if one table has three columns, so have to set up with three array, if table has four columns, so have to set up four array. feel me? So, I need to create variable to get post from the name in HTML, which need to same number from array. For example, if post get four value from HTML, have to have four variable. if post get three value from HTML ,have to have three variable. 

I'm doing my best to improve my grammar in next time.

Link to comment
Share on other sites

Did you understand my previous comments?  You need to fix the issues with the loops, if you want to print one input per column then you need to do that, right now you're defining a variable called $input in the loop but you only print it outside the loop, so it's only going to print the last value.  If you name the inputs with the square brackets then that will make an array in $_POST, you can get that array and loop through it to get the submitted values.

Link to comment
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
×
×
  • Create New...