在php中使用url传递数组(未定大小)

时间:2016-03-12 14:31:06

标签: php arrays get

我有php文件,用于生成用户输入的结果。结果不等于。我的意思是有时它产生3个结果,有时它产生5或4或2个结果。 首先,我定义一个数组变量来存储这些结果:

$resultcount = 0;
$result=array();
while(!feof($fpout3)) 
{
    $row = fgets($fpout3);
    if (!$row) {continue;}
    $element = preg_split("/\t/", $row);        
    $page = $element[0];
    $queryName = $element[1];
    $target_name = $element[7];
    $var_target = $page."\t".$element[1]."\t".$element[2];
    if(count($element) != 1) 
    {
        if($i < 10) 
        {
            echo "<tr align='center'><td align=left><input type='checkbox' id='checkedSeq' name='target[]' value='$var_target'>$target_name</td></tr>";
            fwrite($fp, "$var_target\n");

            // fill $result array
            $resultcount++;
            $result[$resultcount]=$target_name;
            //////////////////////
        }                   
    }
}

我想发送带有url链接的$ result数组以将其保存在数据库中: 我已经使用url发送了其他变量,现在想要用它们传递$ result数组:

echo "<form action='saveResult.php?searchType=$searchType&querySeq=$querySeq&patientIDarray=$patientIDarray&expect=$expect&wordSize=$wordSize' method='post' target='_blank' onsubmit=\"return checkform(this);\">";
echo "<input type='submit' value='Save Result'>";
echo "</form>";

saveResult.php:

<?php
$searchType = (empty($_GET['searchType'])) ? '' : $_GET['searchType'];
$querySeq = (empty($_GET['querySeq'])) ? '' : $_GET['querySeq'];
$patientIDarray = (empty($_GET['patientIDarray'])) ? '' : $_GET['patientIDarray'];
$QueryDate = date("Y-m-d");
$expect=(empty($_GET['expect'])) ? 10 : $_GET['expect'];
$wordSize = (empty($_GET['wordSize'])) ? '' : $_GET['wordSize'];

$serverName = "Alaa";
$connectionInfo = array( "Database"=>"i2b2blast", "UID"=>"i2b2blast", "PWD"=>"demouser");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Result Saved.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
$sql = "INSERT INTO BlastQueryDim (QuerySeq, DatabaseName, WordSize, ExpectThreshold, QueryDate) VALUES ('$querySeq', '$patientIDarray', '$wordSize', '$expect', '$QueryDate')";

$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}
echo "<script>
alert('Result Saved');
</script>";
?>

我的问题:如何使用url传递$ result数组?以及如何在其他php中获取每个索引&#34; saveResult&#34;?

谢谢,

EDIT1:

我输入:

    echo "<form action='saveResult.php?searchType=$searchType&querySeq=$querySeq&patientIDarray=$patientIDarray&expect=$expect&wordSize=$wordSize' method='post' target='_blank' onsubmit=\"return checkform(this);\">";
foreach ($result as $resultcount => $target_name)
echo "<input type='hidden' name='result[$resultcount]' value='$target_name'/>";
echo "<input type='submit' value='Save Result'>";
echo "</form>";

在saveResult.php中,我输入:

$result = empty($_POST['result']) ? array() : $_POST['result'];

并尝试回应它并显示:&#39; Array&#39;这意味着它收到它是空的!

编辑2: 我通过使用:

来解决问题
$result = json_encode($_POST['result'], true); 
echo $result;

现在,echo的输出是:

{"1":"gi|1786181|gb|AE000111|ECAE000111","2":"gi|1786250|gb|AE000117|ECAE000117"}

如何在数据库中插入每行的第二个单元格? 我想插入&#39; gi | 1786181 | gb | AE000111 | ECAE000111&#39;连续 并插入&#39; gi | 1786250 | gb | AE000117 | ECAE000117&#39;在另一行

3 个答案:

答案 0 :(得分:1)

要在提交表单时传递setInterval(function () { $.ajax({ url : "specific url", method : "GET"/"POST", data : "", success : function (data) { $('#divID').html(data); } }); }, 500); 数组,只需创建隐藏的输入,如下所示:

$result

echo "<form action='saveResult.php?searchType=$searchType&querySeq=$querySeq&patientIDarray=$patientIDarray&expect=$expect&wordSize=$wordSize' method='post' target='_blank' onsubmit=\"return checkform(this);\">"; foreach ($result as $key => $value) { echo "<input type='hidden' name='result[$key]' value='$value'/>"; } echo "<input type='submit' value='Save Result'>"; echo "</form>"; 中得到如下结果:

saveResult.php

答案 1 :(得分:0)

您可以将_post作为数组发送更有用:

echo "<form action='saveResult.php?searchType=$searchType&querySeq=$querySeq&patientIDarray=$patientIDarray&expect=$expect&wordSize=$wordSize' method='post' target='_blank' onsubmit=\"return checkform(this);\">";
// here you is the "solution"
foreach($result as $id => $name){
    echo "<input type='hidden' name='result[".$id."]' value='".$name."'>";
}
echo "<input type='submit' value='Save Result'>";
echo "</form>";

然后在saveResult.php中,你现在有$ _POST [&#39;结果&#39;]作为数组。

提示:请注意您的sql语句以避免注入。

修改

您现在正在接收数组,现在如果您可以检索该信息,您只需使用json_encode或在saveResult.php中再次添加foreach:

foreach($result as $resultcount => $target_name){
    // Here, can ho your statement or sql
}

答案 2 :(得分:0)

注意,如果你去回复$ _POST [&#39;结果&#39;]&#34;,你将始终返回&#34;数组&#34;。您需要做的是&#34; print_r&#34;,以转储该数组......

<?php

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

    $sql = null;

    foreach ($_POST['result'] as $resultcount => $target_name) {
        echo $resultcount . " -> " .$target_name;
        //$sql .= "INSERT INTO table (resultcount, target) VALUES (...);";
    }

    if($sql) {
        sqlsrv_query(....);
    }

}

我会再次给你小费:小心那些sql,也许可以实现PDO绑定值。