将结果从一个php程序发送到另一个php程序

时间:2018-05-17 04:26:00

标签: php mysql database

我有一个php程序,它读取excel文件并完美地返回变量。变量如下所示

$str  = $jdate . ",";
$str .= $block . ",";
$str .= $a_harv . ",";
$str .= $b_harv . ",";
$str .= $loss . ",";
$str .= $sweet . ",";
$str .= $species . ",";
$str .= $comment . "\n";

现在我将这些变量发送到另一个php程序(程序将变量发送到数据库,它也没有问题)来接收/获取变量:

list($sdate,$block,$a_harv,$b_harv,$loss,$sweet,$species,$comment) = 
explode(",",$_GET['$str']);

我想知道如何连接这两个程序?

第一个是readExcel_SendResult.php

<?php

date_default_timezone_set('Asia/Tokyo');


global $link, $db;

include('db.inc');

include_once( dirname( __FILE__ ) . '/PHPExcel-1.8/Classes/PHPExcel.php' );
include_once( dirname( __FILE__ ) . '/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php' );


function endsWith( $str, $suffix ){
  $len = strlen( $suffix );
  return $len == 0 || substr( $str, strlen( $str ) - $len, $len ) === 
$suffix;
}

$excelfilename = 'harv.xlsx';
$excelfilepath = dirname( __FILE__ ) . '/' . $excelfilename;

$reader = null;
if( endsWith( $excelfilename, 'xls' ) ){
  $reader = PHPExcel_IOFactory::createReader( 'Excel5' );
}else if( endsWith( $excelfilename, 'xlsx' ) ){
  $reader = PHPExcel_IOFactory::createReader( 'Excel2007' );
}

if( $reader ){
  $excel = $reader->load( $excelfilepath );

  $obj1 = $excel->getActiveSheet()->toArray( null, true, true, true );
  $num_row = count($obj1);

  print "Variable Count:" . $num_row . "\n";
  for($i=0; $i<$num_row; $i++){
    $sdate = trim($obj1[$i]['A']);
    if($sdate == "")    continue;
    $block = str_replace(" ","",$obj1[$i]['B']);
    $block = mb_convert_kana($block,"r","utf-8");
    $a_harv = $obj1[$i]['C'];
    $b_harv = $obj1[$i]['D'];
    $loss = $obj1[$i]['E'];
    $sweet = trim($obj1[$i]['F']);
    $species = str_replace(" ","",$obj1[$i]['G']);
    $comment = trim($obj1[$i]['H']);

    list($mm, $dd, $yy) = explode("/",$sdate);
    if($yy == "")   continue;
    $jdate = $yy . "-" . $mm . "-" . $dd;

    $str  = $jdate . ",";
    $str .= $block . ",";
    $str .= $a_harv . ",";
    $str .= $b_harv . ",";
    $str .= $loss . ",";
    $str .= $sweet . ",";
    $str .= $species . ",";
    $str .= $comment . "\n";


    $shell .= "?ARG=" . $str;

  }
}else{
  echo "No reader.";
}
?>

我拥有的另一个程序就是这个; send_datatoDB.php

global $link, $db;

include'excel_db.php'; 

include('db.inc');

list($sdate,$block,$a_harv,$b_harv,$loss,$sweet,$species,$comment) = 
explode(",",$_GET['$str']);

$block = mb_convert_kana($block,"r","utf-8");

if($species == "type1"){
    $vid = "v2";
} else if ($species == "type2"){
    $vid = "v3";
} else if ($species == "type3"){
    $vid = "v1";
} else {
    $vid = "v4";
}

if(is_numeric($sweet)){
    $sweetness = $sweet;
    $s_and_c = $comment;
} else {
    $sweetness = 0.0;
    $s_and_c = $sweet . $comment;
}


connect_mysql($link, $db);

    $sql = "Select * from tableName where sdate = '{$sdate}' and block = '{$block}' and vid = '{$vid}'";


    $result_id = mysqli_query($link,$sql);
    if($result_id == false){
        exit();
    }

    $row_cnt = mysqli_num_rows($result_id);
    echo "Result Number:" . $row_cnt . "<br>";

    while ($row = mysqli_fetch_assoc($result_id)) {
                $hid = $row['hid'];
                //echo "hid = " . $hid . "<br>";
    }

    mysqli_close($link);


connect_mysql($link, $db);

    if($row_cnt < 1){       //INSERT
        $values = "'{$sdate}' , '{$block}' , '{$vid}', '{$a_harv}', 
'{$b_harv}', '{$loss}', '{$sweetness}', '{$s_and_c}'";
        $sql = "INSERT into tableNamw ";
        $sql .= "(`sdate`, `block`, `vid`, `A_harv`, `B_harv`, `loss`, 
`sweetness`, `comments`) ";
        $sql .= " VALUES ({$values})";

    //echo $sql;

        $result_id = mysqli_query($link,$sql);
        if($result_id == false){
            exit();
        }
    } else {                        //UPDATE
        $sql = "UPDATE tableName SET ";
        $sql .= "a_harv = '" . $a_harv . "', ";
        $sql .= "b_harv = '" . $b_harv . "', ";
        $sql .= "loss = '" . $loss . "', ";
        $sql .= "sweetness = '" . $sweetness . "', ";
        $sql .= "comments = '" . $s_and_c . "'";
        $sql .= " where hid = " . $hid;

       //echo $sql . "<br>";

        $result_id = mysql_query($sql,$link);
        if($result_id == false){
            //echo "<BR>Uooops<BR>";

            exit();
        }
    }

mysqli_close($link);
?>

3 个答案:

答案 0 :(得分:0)

您可以使用会话 Cookie

将数据从一个页面传递到另一个页面

<强>会话

//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];

<强>曲奇:

//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];

答案 1 :(得分:0)

您的代码缺少很多东西。而不是使两个文件将代码放在一个文件中,并根据以下代码进行一些更改。

通过

将所有数据从excel扔到数组中
$rec  = array();

$rec['jdate']  = $jdate;
$rec['block'] = $block;
$rec['a_harv'] = $a_harv;
$rec['b_harv'] = $b_harv;
$rec['loss'] = $loss;
$rec['sweet'] = $sweet;
$rec['species'] = $species;
$rec['comment'] = $comment;

$arr[] = $rec;

然后

if(count($arr) > 0){
    foreach ($arr as $key => $row) {
        $row['block'] = mb_convert_kana($row['block'],"r","utf-8");

        if($row['species'] == "type1"){
            $vid = "v2";
        } else if ($row['species'] == "type2"){
            $vid = "v3";
        } else if ($row['species'] == "type3"){
            $vid = "v1";
        } else {
            $vid = "v4";
        }

        if(is_numeric($sweet)){
            $sweetness = $sweet;
            $s_and_c = $comment;
        } else {
            $sweetness = 0.0;
            $s_and_c = $sweet . $comment;
        }
        $sql = "Select * from tableName where sdate = '{$row['sdate']}' and block = '{$row['block']}' and vid = '{$vid}'";


        $result_id = mysqli_query($link,$sql);
        if($result_id == false){
            exit();
        }

        $row_cnt = mysqli_num_rows($result_id);
        echo "Result Number:" . $row_cnt . "<br>";

        while ($r = mysqli_fetch_assoc($result_id)) {
            $hid = $r['hid'];
            //echo "hid = " . $hid . "<br>";
        }
        if($row_cnt < 1){       //INSERT
            $values = "'{$row['sdate']}' , '{$row['block']}' , '{$row['vid']}', '{$row['a_harv']}', '{$row['b_harv']}', '{$row['loss']}', '{$row['sweetness']}', '{$row['s_and_c']}'";
            $sql = "INSERT into tableNamw ";
            $sql .= "(`sdate`, `block`, `vid`, `A_harv`, `B_harv`, `loss`, `sweetness`, `comments`) ";
            $sql .= " VALUES ({$values})";

            //echo $sql;

            $result_id = mysqli_query($link,$sql);
            if($result_id == false){
                exit();
            }
        } 
        else {                        //UPDATE
            $sql = "UPDATE tableName SET ";
            $sql .= "a_harv = '" . $row['a_harv'] . "', ";
            $sql .= "b_harv = '" . $row['b_harv'] . "', ";
            $sql .= "loss = '" . $row['loss'] . "', ";
            $sql .= "sweetness = '" . $row['sweetness'] . "', ";
            $sql .= "comments = '" . $row['s_and_c'] . "'";
            $sql .= " where hid = " . $row['hid'];

           //echo $sql . "<br>";

            $result_id = mysql_query($sql,$link);
            if($result_id == false){
                //echo "<BR>Uooops<BR>";

                exit();
            }
        }
    }

}

答案 2 :(得分:0)

编辑后的代码如下。我还包括一个测试mysql数据库用于测试和数据。     

date_default_timezone_set('Asia/Tokyo');
global $link, $db;

include('db2.inc');

include_once( dirname( __FILE__ ) . '/PHPExcel-1.8/Classes/PHPExcel.php' );
include_once( dirname( __FILE__ ) . '/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php' );


function endsWith( $str, $suffix ){
  $len = strlen( $suffix );
  return $len == 0 || substr( $str, strlen( $str ) - $len, $len ) === $suffix;
}

$excelfilename = 'harv2.xlsx';
$excelfilepath = dirname( __FILE__ ) . '/' . $excelfilename;

$reader = null;
if( endsWith( $excelfilename, 'xls' ) ){
  $reader = PHPExcel_IOFactory::createReader( 'Excel5' );
}else if( endsWith( $excelfilename, 'xlsx' ) ){
  $reader = PHPExcel_IOFactory::createReader( 'Excel2007' );
}

if( $reader ){
$excel = $reader->load( $excelfilepath );

  $obj1 = $excel->getActiveSheet()->toArray( null, true, true, true );
  $num_row = count($obj1);

  print "Data Number:" . $num_row . "\n";
  for($i=0; $i<$num_row; $i++){
    $sdate = trim($obj1[$i]['A']);
    if($sdate == "")    continue;
    $block = str_replace(" ","",$obj1[$i]['B']);
    $block = mb_convert_kana($block,"r","utf-8");
    $a_harv = $obj1[$i]['C'];
    $b_harv = $obj1[$i]['D'];
    $loss = $obj1[$i]['E'];
    $sweet = trim($obj1[$i]['F']);
    $species = str_replace(" ","",$obj1[$i]['G']);
    $comment = trim($obj1[$i]['H']);

    list($mm, $dd, $yy) = explode("/",$sdate);
    if($yy == "")   continue;
    $jdate = $yy . "-" . $mm . "-" . $dd;

    $rec  = array();

    $rec['jdate']  = $jdate;
    $rec['block'] = $block;
    $rec['a_harv'] = $a_harv;
    $rec['b_harv'] = $b_harv;
    $rec['loss'] = $loss;
    $rec['sweet'] = $sweet;
    $rec['species'] = $species;
    $rec['comment'] = $comment;

    $arr[] = $rec;

  }
}else{
  echo "No reader.";
}

if(count($arr) > 0){
    foreach ($arr as $key => $row) {
        $row['block'] = mb_convert_kana($row['block'],"r","utf-8");

        if($row['species'] == "アイコ"){
            $vid = "v2";
        } else if ($row['species'] == "チャペル"){
            $vid = "v3";
        } else if ($row['species'] == "朱雀"){
            $vid = "v1";
        } else {
            $vid = "v4";
        }
        if($row['sweet']){
            $sweetness = preg_replace("/[^0-9,.]/", '', $row['sweet']) . "\n";
        $s_and_c = $row['comment'];
    } else {
        $sweetness = 0.0;
        $s_and_c = $row['sweetness'] . $row['comment'];
    }
    connect_mysql($link, $db);

        $sql = "Select * from toguchi_harv where sdate = '{$row['sdate']}' and block = '{$row['block']}' and vid = '{$vid}'";

       # echo $sql . "<br>";
        $result_id = mysqli_query($link,$sql);
        if($result_id == false){
            exit();
        }

        $row_cnt = mysqli_num_rows($result_id);
        echo "Result Number:" . $row_cnt . "<br>";

        while ($r = mysqli_fetch_assoc($result_id)) {
            $hid = $r['hid'];
            echo "hid = " . $hid . "<br>";
        }

        mysqli_close($link);

    connect_mysql($link, $db);

        if($row_cnt < 1){//INSERT
            $values = "'{$row['jdate']}' , '{$row['block']}' , '{$vid}', '{$row['a_harv']}', '{$row['b_harv']}', '{$row['loss']}', '{$sweetness}', '{$s_and_c}'";

            var_dump($values);

            $sql = "INSERT into toguchi_harv ";
            $sql .= "(`sdate`, `block`, `vid`, `A_harv`, `B_harv`, `loss`, `sweetness`, `comments`) ";
            $sql .= " VALUES ({$values})";

            //echo $sql;

            $result_id = mysqli_query($link,$sql);
            if($result_id == false){
                exit();
            }
        } else {
            $sql = "UPDATE toguchi_harv SET sdate=?, a_harv=?, b_harv=?,
            loss=?,sweetness=?,comments=? WHERE hid=?";
            if($stmt == mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
                mysqli_stmt_bind_param($stmt, "isssiiiis", $param_hid,$param_sdate,$param_block, $param_vid,$param_a_harv,$param_b_harv,$param_loss,
                    $param_sweetness,$param_s_and_c);
        //set parameters
                $param_hid = $hid;
                $param_sdate = $row['jdate'];
                $param_block = $row['block'];
                $param_vid = $vid;
                $param_a_harv = $row['a_harv'];
                $param_b_harv = $row['b_harv'];
                $param_loss = $row['loss'];
                $param_sweetness = $sweetness;
                $param_s_and_c = $s_and_c;
        // Attempt to execute the prepared statement
                if(mysqli_stmt_execute($stmt)){
                    echo "Updated";
                    exit();
                } else{
                    echo "Something went wrong.";
                }
            }
            mysqli_close($link);
        }
    }
}

?>

免费的mysql数据库 服务器:sql12.freemysqlhosting.net 名称:sql12238577 用户名:sql12238577 密码:L8aEhbgPNP 端口号:3306

这是包含db2.inc     

function connect_mysql(&$link, &$db){

global $link;

    $link = mysqli_connect('sql12.freemysqlhosting.net', 'sql12238577', 
'L8aEhbgPNP');
    if(!$link){
        print "MySQL connection failed!";
        exit();
    }

    $db = mysqli_select_db($link,'test_DB');
    if (!$db){
        die('DB connection failed!'.mysql_error());
        exit();
    }

    mysqli_set_charset($link,'utf8');

    return true;
}

?>

示例数据位于此链接https://tokyonodai-my.sharepoint.com/:x:/g/personal/45618006_nodai_ac_jp/EaB74JirQUdJn7OAZ86IQPYBlADKBoHWax-x4_-1pf5rkQ?e=X6dIQ7