2016-07-23 45 views
0

我想用輸入文本字段更新我的SQL語句,並且需要傳遞值。如何從另一個PHP文件獲取輸入值?

我有一個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$k'Germany' else語句,因爲它似乎文本的輸入字段LandC未傳遞給getDataC

+1

順便說一句,你的代碼可能容易受到SQL注入:http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – chrki

+0

@YeuSeChia這些不工作,因爲我工作在這裏谷歌圖表,需要留在index.php 這些解決方案都送我到getDataC –

回答

0

這個標籤<form action="HERE PUT THE PLACE OF TH FILE getDATAC.php" method="GET">

0
<form> 

改變你的表單標籤應該是:

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

保持操作的文件的正確路徑。

0

你有變動(變更形式的行動,如果你的文件getDataC.php是在另一位置)HTML:

<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; 
?> 
0

創建一個PHP文件,你想發送你的數據。並把相同的名稱在行動。

相關問題