【问题标题】:How can I get the input value, from another PHP file?如何从另一个 PHP 文件中获取输入值?
【发布时间】:2016-11-27 04:47:04
【问题描述】:

我想用输入文本字段更新我的 SQL 语句并且需要传递值。

我有一个index.php 文件和一个getDataC.php 文件。我需要在索引的文本字段中插入一个值,以更新getDataC 中的 SQL 语句,以使用索引中的函数。如何将索引字段的值传递给getDataC 以再次在索引中使用它?

这是我的代码,来自index.php

<body>
<h1 align="center">BigData - Gruppe 2</h1>
</br>
<table width="100%" align="center">
    <tr>
        <td width="25%" align="center">
            <h2>Bitte w&auml;hlen Sie eine Abfrage aus!</h2>
        </td>
        <td width="75%" align="center">
            <h2>Grafische Darstellung</h2>
        </td>
    </tr>
    <tr>
        <td width="40%">
            <hr>
            <h3 align="center">Abfragen:</h3> </br>
            <div>
                Einfluss von Bildung auf Fertilitätsrate (Japan)
                <button type="submit" id="actionA">Anzeigen</button>
            </div> </br>
            <div>
                Einfluss von Internet auf Fertilitätsraten (Japan)
                <button type="submit" id="actionB">Anzeigen</button>
            </div> </br>
            <div>
                Einfluss von Internet und Bildung auf die Anzahl der Frauen in
                Wirtschaft/Politik </br>
                <form > <label for="LandC">Wähle ein Land: <input type=text id="LandC" name="LandC"> </label></form>

                <button type="submit" id="actionC">Anzeigen</button>
            </div> </br>

还有,这里是来自getDataC.php的代码:

<?php
$con = mysqli_connect('localhost','root','','bigdata');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

if( isset($_GET['submit']) )
{
//be sure to validate and clean your variables
$landC = htmlentities($_GET['LandC']);

}
if( isset($landC) ) {
 $k = $landC;
}
else $k='Germany';


$qry = "SELECT  * FROM facts WHERE facts.Country = '".  $k ."' AND Frauen_Fuehrungspositionen_Prozent IS NOT NULL";

$result = mysqli_query($con,$qry);

mysqli_close($con);

$table = array();
//Es´rstelt die Spaöten der Tabelle
$table['cols'] = array(
//Labels for the chart, these represent the column titles
array('id' => '', 'label' => 'Land', 'type' => 'string'),
    array('id' => '', 'label' => 'Jahr', 'type' => 'number'),

array('id' => '', 'label' => 'Staatsausgaben GDP per capita in Dollar', 'type' => 'number'),
array('id' => '', 'label' => 'Internetzugang in Prozent', 'type' => 'number'),

array('id' => '', 'label' => 'Frauen Fuehrungspositionen in Prozent', 'type' => 'number')

); 

$rows = array();
foreach($result as $row){
$temp = array();

//Values / Füllt die Spalten in der hier angegebenen Reihenfolge mit Zahlen
$temp[] = array('v' => (string) $row['Country']);
    $temp[] = array('v' => (int) $row['Year']); 

$temp[] = array('v' => (double) $row['Staatsausgaben_GDP_per_capita_in_Dollar']); 
    $temp[] = array('v' => (double) $row['Internetzugang_in_Prozent']); 

$temp[] = array('v' => (double) $row['Frauen_Fuehrungspositionen_Prozent']);
$rows[] = array('c' => $temp);

}   
$result->free();

$table['rows'] = $rows;

$jsonTable = json_encode($table, true);
echo $jsonTable;
?>

现在,getDataC 的 else 语句将 $k 设置为 'Germany',因为输入字段 LandC 的文本似乎没有传递给 getDataC

【问题讨论】:

  • 顺便说一句,您的代码可能容易受到 SQL 注入攻击:stackoverflow.com/questions/60174/…
  • @YeuSeChia 这些不起作用,因为我在这里使用谷歌图表并且需要留在 index.php 这些解决方案都将我发送到 getDataC

标签: php html mysql input google-visualization


【解决方案1】:

创建一个您想要发送数据的 php 文件。并使用相同的名称。

【讨论】:

    【解决方案2】:

    您的 HTML 带有更改(如果您的文件 getDataC.php 位于其他位置,请更改您的表单操作):

    <body>
    <h1 align="center">BigData - Gruppe 2</h1>
    </br>
    <table width="100%" align="center">
        <tr>
            <td width="25%" align="center">
                <h2>Bitte w&auml;hlen Sie eine Abfrage aus!</h2>
            </td>
            <td width="75%" align="center">
                <h2>Grafische Darstellung</h2>
            </td>
        </tr>
        <tr>
            <td width="40%">
                <hr>
                <h3 align="center">Abfragen:</h3> </br>
                <div>
                    Einfluss von Bildung auf Fertilitätsrate (Japan)
                    <button type="submit" id="actionA">Anzeigen</button>
                </div> </br>
                <div>
                    Einfluss von Internet auf Fertilitätsraten (Japan)
                    <button type="submit" id="actionB">Anzeigen</button>
                </div> </br>
                <div>
                    Einfluss von Internet und Bildung auf die Anzahl der Frauen in
                    Wirtschaft/Politik </br>
                    <form action="getDataC.php">
                    <label for="LandC">Wähle ein Land: <input type=text id="LandC" name="LandC"> </label>
                    <button type="submit" id="actionC">Anzeigen</button>
                    </form>
                </div> </br>
    

    你的 getDataC.php 文件有变化:

    <?php
    if(isset($_GET['LandC']) ){
    //be sure to validate and clean your variables
    $landC = htmlentities($_GET['LandC']);
    
    }
    if(isset($landC) ) {
     $k = $landC;
    }
    else $k='Germany';
    $qry = "SELECT  * FROM facts WHERE facts.Country = '".  $k ."' AND Frauen_Fuehrungspositionen_Prozent IS NOT NULL";
    
    $result = mysqli_query($con,$qry);
    
    mysqli_close($con);
    
    $table = array();
    //Es´rstelt die Spaöten der Tabelle
    $table['cols'] = array(
    //Labels for the chart, these represent the column titles
    array('id' => '', 'label' => 'Land', 'type' => 'string'),
        array('id' => '', 'label' => 'Jahr', 'type' => 'number'),
    
    array('id' => '', 'label' => 'Staatsausgaben GDP per capita in Dollar', 'type' => 'number'),
    array('id' => '', 'label' => 'Internetzugang in Prozent', 'type' => 'number'),
    
    array('id' => '', 'label' => 'Frauen Fuehrungspositionen in Prozent', 'type' => 'number')
    
    ); 
    
    $rows = array();
    foreach($result as $row){
    $temp = array();
    
    //Values / Füllt die Spalten in der hier angegebenen Reihenfolge mit Zahlen
    $temp[] = array('v' => (string) $row['Country']);
        $temp[] = array('v' => (int) $row['Year']); 
    
    $temp[] = array('v' => (double) $row['Staatsausgaben_GDP_per_capita_in_Dollar']); 
        $temp[] = array('v' => (double) $row['Internetzugang_in_Prozent']); 
    
    $temp[] = array('v' => (double) $row['Frauen_Fuehrungspositionen_Prozent']);
    $rows[] = array('c' => $temp);
    
    }   
    $result->free();
    
    $table['rows'] = $rows;
    
    $jsonTable = json_encode($table, true);
    echo $jsonTable;
    ?>
    

    【讨论】:

      【解决方案3】:
      <form>
      

      应该是:

      <form action="getDataC.php" method="GET">
      

      将操作保持在文件的正确路径。

      【讨论】:

        【解决方案4】:

        用这个标签&lt;form action="HERE PUT THE PLACE OF TH FILE getDATAC.php" method="GET"&gt;改变你的表单标签

        【讨论】:

          猜你喜欢
          • 2016-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-26
          相关资源
          最近更新 更多