开发者

How to insert the results of permutation into table in mysql

I want to store the results of permutation into a temporary table in mysql so i can use it in the search script.

<?php
function开发者_开发百科 permute($str,$i,$n) {
    if ($i == $n) {
        echo "$str\n";
        echo "<br/>";

        $query = mysql_query("CREATE TABLE temp(
            id int NOT NULL AUTO_INCREMENT,
            number varchar(64) NOT NULL,
            PRIMARY KEY(id)
            )");

        $str = addslashes(trim($_POST['str']));

        $query = mysql_query("INSERT INTO temp (number) VALUES ('$str')")
            or die(mysql_error());
    }
    else {
        for ($j = $i; $j < $n; $j++) {
            swap($str,$i,$j);
            permute($str, $i+1, $n);
            swap($str,$i,$j); // backtrack.
        }
    }
}

// function to swap the char at pos $i and $j of $str.
function swap(&$str,$i,$j) {
    $temp = $str[$i];
    $str[$i] = $str[$j];
    $str[$j] = $temp;
}   

$str = @$_GET['number'] ;
permute($str,0,strlen($str)); // call the function.
  // Get the search variable from URL

?>

Does anyone know how to insert the result of permutation into a temporary table?

I have try to insert into permanent table but the 24sets result of 1234 is not inserted but only the id 1-24 is inserted. I think the problem is because the $str contain 24sets of result but is not separated since it is print out together??

Any solution?


Your real problem is $str = addslashes(trim($_POST['str']));, which is attempting to retrieve the value from the web request in a post form, immediately before putting it into the temp table.
Basically, you're over-writing your variable with something that is (probably not set, thus) blank.
Oh, and I hope in reality you're safely escaping your input parms, or your site is going to be taken over in short order. If number is only ever supposed to contain an actual number (1234), and not some string (ABC), cast it to numeric data (and back to string) to completely sanitize it (exception handling should be included, too).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜