PHP:没有存储全局变量?

时间:2014-12-28 00:34:53

标签: php global-variables

事实证明这与全局变量无关(很抱歉在另一篇文章中讨论过)。

我试图将所有代码保存在一个页面上(自我处理),但我很确定这是不可能的。在同一页面上排列以下答案中列出的以下代码不起作用。数据。

*编辑以添加整个脚本减去CSS(不必要)。

<!DOCTYPE html>
    <head>
        <title>Inventory Tables</title>
    </head>
<style></style>
<body>
<?php //IRCinventoryhome.php
    require("IRCpage.inc");
    require_once 'IRCinventoryconfig.php';

    $homepage = new IRCtemplate();

    $homepage->Display();

    session_start();
?>

<!-- Dropdown Menu for Table Selection -->

    <div id="contentHeader">
        <?php //Menu for table selection
        ini_set('display_errors',1);  error_reporting(E_ALL);

        $connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
            if ($connection->connect_error) die($connection->connect_error);

        $result = $connection->query("SHOW TABLES");

        $table = array();
            while ($row = $result->fetch_row()){
            $table[] = $row[0];
            }
        $count = count($table);
        ?>

    <div id="select">
        <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
            <select name="value">
                <?php for ($pointer = 0 ; $pointer < $count ; ++$pointer) {
                    echo <<<_END
                        <pre>
                            <option value="$table[$pointer]">$table[$pointer]</option>
                        </pre>
_END;
                }
?>
            </select>
        <input type="submit" value="go">
        </form>
    </div> <!-- End .select -->
    </div> <!-- End #contentHeader -->
<div id="content">


<!-- Code for Database Tables and Actions -->

<?php //inventory mysql tables
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['value'])) {

$thisTable = $_POST['value'];

global $thisTable;

ini_set('display_errors',1);  error_reporting(E_ALL);

$queryColumns  = "SHOW COLUMNS FROM $thisTable";
$resultColumns = $connection->query($queryColumns);
    if (!$resultColumns) die ("Database access failed: " . $connection->error);

$columns = array();
    while ($column = $resultColumns->fetch_row()){
        $columns[] = $column[0];
     }

echo "<div id=\"table\"><table class=\"CSSTableGenerator\" >\n";
$count = count($columns);

$insertColumns = array();
for ($pointer = 1 ; $pointer < $count ; ++$pointer) {
    $insertColumns[] = $columns[$pointer];
    }


for ($pointer = 0 ; $pointer < $count ; ++$pointer) {
    echo "<th scope=\"col\" bgcolor=\"#efefef\">";
    echo $columns[$pointer];
    echo "</th>";
    }
    echo "<th>ACTIONS</td>";
$queryRows  = "SELECT * FROM $thisTable";
    $resultRows = $connection->query($queryRows);
    if (!$resultRows) die ("Database access failed: " . $connection->error);

$rows = $resultRows->num_rows;


for ($j = 0 ; $j < $rows ; ++$j)
    {
        $resultRows->data_seek($j);
        $row = $resultRows->fetch_array(MYSQLI_NUM);

        $count = count($row);

        echo "<tr>";
        for ($pointer = 0 ; $pointer < $count ; ++$pointer) {
            echo "<td>";
            echo $row[$pointer];
            echo "</td>";
        }
?>
        <td>
            <input action="<?php echo $_SERVER['PHP_SELF']?>" type="submit" value="edit" name="edit">
            <input action="<?php echo $_SERVER['PHP_SELF']?>" type="submit" value="delete" name="delete">
        </td>
        </tr>
<?php
    }
echo "</table></div>"; //end table, end content

$connection->close();
}
?>

<!-- FORM FOR ADDING ROWS TO CURRENT TABLE -->

<div id="table">
    <table id="formTable">
        <form name="addRow" action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
                <input type="hidden" name="control">
            <td>
                Add row?
            </td>
        <?php for ($pointer = 1 ; $pointer < $count ; ++$pointer) { ?>
            <td>
                <input type="text" name="<?php echo $columns[$pointer];?>">
            </td>
        <?php } ?>
            <td>
                <input type="submit" value="go">
            </td>
        </form>
    </table>
</div> <!-- end form, end table, end content -->
<?php

global $thisTable;
echo $thisTable;

if (isset($_POST['control'])) {

global $thisTable;
echo $thisTable;

$valuesArray = array();
if (isset($_POST)) {
    $valuesArray = $_POST;
    }

$columnsArray = array_keys($valuesArray);
//array_splice($columnsArray, 0, 1);

$columnsString = implode(", ", $columnsArray);

print_r($columnsString);

$insertValues = array();
foreach($valuesArray as $values) {
     $insertValues[] = $values;
    }

$valuesString = implode(" ", $insertValues);
$valuesString = "'".$valuesString."'";
$valuesString = str_replace(" ", "', '", $valuesString);
$valuesString = substr($valuesString, 3);

print_r($valuesString);
/*
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
            if ($connection->connect_error) die($connection->connect_error);


    $queryInsert = "INSERT INTO $thisTable ($columnsString) VALUES ($valuesString)";                           
    $result      = $connection->query($queryInsert);

    if (!$result) echo "INSERT failed: $query<br>" .
      $connection->error . "<br><br>";


$connection->close();
*/
}
?>
</div> <!-- End #content -->
</body>
</html>

1 个答案:

答案 0 :(得分:0)

大约20个小时后,我仍然不确定是否可以在同一页面上完成所有操作。但是这个解决方案有效......

第一页:

<!DOCTYPE html>
    <head>
        <title>Interactive Resource Center Inventory</title>
        <meta charset="UTF-8">
    </head>
<script src="../_js/jquery.min.js"></script>
<script src="../_js/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
    $('form').submit(function() {
        $('input[type=submit]')
        prop('disabled',true);
    });//end submit
    var max_fields      = 4; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div>Column: <input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });
    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});//end ready
</script>
<style>

#table {
    margin-top:2px;
    }

#formTable {
    width:100%;
    margin:0px;padding:0px;
    border:1px solid #000000;
    border-bottom:none;
    border-left:none;
    border-right:none;
    }

#formTable td{
    vertical-align:middle;
    border:1px solid #000000;
    border-width:0px 1px 1px 0px;
    text-align:left;
    padding:7px;
    width:100px;
    font-size:14px;
    font-family:arial;
    font-weight:normal;
    color:#000000;
    }

#formTable input[type="text"]{
        width:95%;
        }

.CSSTableGenerator {
margin:0px;padding:0px;
width:100%; 
border:1px solid #000000;
border-top:none;
border-right:none;
border-left:none;
}

.CSSTableGenerator table{
width:100%;
height:100%;
margin:0px;padding:0px;
}

.CSSTableGenerator tr:nth-child(odd){ background-color:#e5e5e5; }
.CSSTableGenerator tr:nth-child(even) { background-color:#ffffff; }

.CSSTableGenerator th{
border:1px solid #000000;
border-width:0px 1px 1px 0px;
width:100px;
}

.CSSTableGenerator td{
vertical-align:middle;
border:1px solid #000000;
border-width:0px 1px 1px 0px;
text-align:left;
padding:7px;
width:100px;
font-size:14px;
font-family:arial;
font-weight:normal;
color:#000000;
}

.CSSTableGenerator tr:last-child td{
border-width:0px 1px 0px 0px;
}

.CSSTableGenerator tr td:last-child{
border-width:0px 0px 1px 0px;
}

.CSSTableGenerator tr:last-child td:last-child{
border-width:0px 0px 0px 0px;
}

.CSSTableGenerator tr:first-child td{
background:-o-linear-gradient(bottom, #cccccc 5%, #b2b2b2 100%);    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #cccccc), color-stop(1, #b2b2b2) );    background:-moz-linear-gradient( center top, #cccccc 5%, #b2b2b2 100% );    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#cccccc", endColorstr="#b2b2b2");  background: -o-linear-gradient(top,#cccccc,b2b2b2);
background-color:#cccccc;
border:0px solid #000000;
text-align:center;
border-width:0px 0px 1px 1px;
font-size:14px;
font-family:arial;
font-weight:bold;
color:#000000;
margin-top:-2px;
</style>
<body>
<?php //IRCinventoryhome.php
require("IRCpage.inc");
require("IRCinventoryfunctions.php");
require_once 'IRCinventoryconfig.php';

$homepage = new IRCtemplate();

$homepage->Display();

session_start();
    $_SESSION['value'] = $_POST['value'];
    $thisTable = $_SESSION['value'];
    var_dump($_SESSION);

$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
    if ($connection->connect_error) die($connection->connect_error);
    global $connection;

$result = $connection->query("SHOW TABLES");

$table = array();
    while ($row = $result->fetch_row()){
    $table[] = $row[0];
    }
$count = count($table);

?>

<!-- Dropdown Menu for Table Selection -->

<div id="contentHeader">
    <div id="select">
        <form action="http://localhost:8888/IRC/IRCinventoryhometest.php" method="POST">
            <select name="value">
                <?php for ($pointer = 0 ; $pointer < $count ; ++$pointer) {
                    echo <<<_END
                        <pre>
                            <option value="$table[$pointer]">$table[$pointer]</option>
                        </pre>
_END;
                }
                ?>
            </select>
            <input type="submit" name="go">
        </form>
    </div> <!-- End .select -->
</div> <!-- End #contentHeader -->

<div id="content">

<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$thisTable = "OUTGOING";

$queryColumns  = "SHOW COLUMNS FROM $thisTable";
$resultColumns = $connection->query($queryColumns);
    if (!$resultColumns) die ("Database access failed: " . $connection->error);
global $resultColumns;

$queryRows  = "SELECT * FROM $thisTable";
    $resultRows = $connection->query($queryRows);
    if (!$resultRows) die ("Database access failed: " . $connection->error);
global $resultRows;

$rows = $resultRows->num_rows;
global $rows;

global $connection;

drawTable();

$connection->close();
} else {

if(isset($_POST['value'])) {
    $thisTable = $_POST['value'];
    }

$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
    if ($connection->connect_error) die($connection->connect_error);
    global $connection;


$queryColumns  = "SHOW COLUMNS FROM $thisTable";
$resultColumns = $connection->query($queryColumns);
    if (!$resultColumns) die ("Database access failed: " . $connection->error);
global $resultColumns;

$queryRows  = "SELECT * FROM $thisTable";
    $resultRows = $connection->query($queryRows);
    if (!$resultRows) die ("Database access failed: " . $connection->error);
global $resultRows;

$rows = $resultRows->num_rows;
global $rows;

global $connection;

drawTable();

?>
<!-- FORM FOR ADDING ROWS TO CURRENT TABLE -->

<div id="table">
    <table id="formTable">
        <form name="addRow" action="http://localhost:8888/IRC/IRCprocessinventory.php" method="POST">
                <input type="hidden" name="control">
            <td>
                Add row?
            </td>
            <td></td>
        <?php for ($pointer = 2 ; $pointer < $countColumnsGlobal ; ++$pointer) { ?>
            <td>
                <input type="text" name="<?php echo $columns[$pointer];?>">
            </td>
        <?php } ?>
            <td>
                <input type="submit" value="submit">
            </td>
        </form>
    </table>
</div> <!-- end table -->
<?php 
}

?>
</div> <!-- End #content -->

</body>
</html>

第2页:

<?php
require_once 'IRCinventoryconfig.php';

ini_set('display_errors',1);  error_reporting(E_ALL);
session_start();

if(isset($_SESSION['value'])) { 
    $thisTable = $_SESSION['value'];

    $valuesArray = array();
    if (isset($_POST)) {
        $valuesArray = $_POST;
        }

    $columnsArray = array_keys($valuesArray);
    array_splice($columnsArray, 0, 1);

    $columnsString = implode(", ", $columnsArray);


    $insertValues = array();
    foreach($valuesArray as $values) {
        $insertValues[] = $values;
        }

    $valuesString = implode("','", $insertValues);
    $valuesString = "'".$valuesString."'";
    $valuesString = substr($valuesString, 3);

    $connection = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
        if ($connection->connect_error) die($connection->connect_error);

    $queryInsert = "INSERT INTO $thisTable ($columnsString) VALUES ($valuesString)";                           
    $result      = $connection->query($queryInsert);

    if (!$result) echo "INSERT failed: $query<br>" .
        $connection->error . "<br><br>";
    else echo "Successful entry";

    $connection->close();

}
?>