Jump to content

Recommended Posts

Hello my fellow PHP developers. I am writing a script that will allow for someone to create a table in their database but just filling in a few text boxes.

 

I am coming across the problem Notice: Undefined offset: 1. I know what it means and all but I just do not know how to fix it. So if someone would be so kind to help me I would appreciate it.

 

<?php 

$table_name = $_POST['table_name'];

$create_sql = "create table $table_name (";

for ($i = 0; $i < count($_POST['field_name']); $i++) {
    $create_sql .= $_POST['field_name'][$i] . " " . $_POST['field_type'][$i];

if($_POST['auto_increment'][$i] == "y" ) {
	$additional = "NOT NULL auto_increment";
} else {
	$additional = "";
}

if($_POST['primary'][$i] == "y"){
	$additional .= ", primary key(".$_POST['field_name'][$i].")";
} else {
	$additional = "";
}

    if ($_POST['field_length'][$i] != "") {
        $create_sql .= "(" . $_POST['field_length'][$i] . ") $additional,";
    }
    else {
        $create_sql .= " $additional ,";
    }
}
$create_sql = substr($create_sql, 0, -1);
$create_sql .= ")";

$create_res = mysql_query($create_sql);

if ($create_res) {

    $sql_column = "SHOW COLUMNS FROM $table_name";
    $res_column = mysql_query($sql_column);

    echo "<h3>the following database has been created <strong>". $table_name. "</strong></h3>";
        if (mysql_num_rows($res_column)) {
            echo "<table class='db-table' cellpadding='5' cellspacing='5'>";
            echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default<th>Extra</th></tr>";
            while ($row = mysql_fetch_row($res_column)) {
                echo "<tr>";
                foreach ($row as $key => $value) {
                    echo "<td>". $value ."</td>";
                }
                echo "</tr>";
            }
            echo "</table><br />";
        }

        echo "<a href='create_table.php'>Create</a> another table";
}
else {
    echo "<h3>There was an error" . mysql_error()."</h3>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/258975-why-array-why/
Share on other sites

Place this code at the top of the page.

 

echo "<pre>";
print_r($_POST);
echo "</pre>";

 

This will display the $_POST values being submitted to the page via form.

My guess is that one or more of the post values you are trying to access as arrays are in fact strings.

Link to comment
https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1327604
Share on other sites

This is the output from the code

 

Array

(

    [table_name] => mike

    [field_name] => Array

        (

            [0] => hggghghbwthw

            [1] => test

            [2] => test_field

        )

 

    [field_type] => Array

        (

            [0] => int

            [1] => char

            [2] => varchar

        )

 

    [field_length] => Array

        (

            [0] => 5

            [1] => 50

            [2] => 50

        )

 

    [primary] => Array

        (

            [0] => y

        )

 

    [auto_increment] => Array

        (

            [0] => y

        )

 

)

Link to comment
https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328306
Share on other sites

This is the output from the code

 

Array
(
    [table_name] => mike
    [field_name] => Array
        (
            [0] => hggghghbwthw
            [1] => test
            [2] => test_field
        )

    [field_type] => Array
        (
            [0] => int
            [1] => char
            [2] => varchar
        )

    [field_length] => Array
        (
            [0] => 5
            [1] => 50
            [2] => 50
        )

    [primary] => Array
        (
            [0] => y
        )

    [auto_increment] => Array
        (
            [0] => y
        )

)

Link to comment
https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328308
Share on other sites

should be pretty obvious at this point.

The internal arrays [primary] and [auto_increment] only contain 1 key/value pair, so trying to put these in a for loop and access any further key/value pairs will trip an error (the one you received).

e.g.

$_POST['auto_increment'][1] does not exist, only $_POST['auto_increment'][0] does.

Link to comment
https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328333
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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