dongqing4774 2017-11-24 22:18
浏览 54

如何将结果从php写入数据库

I need to write the following to a database: first integer, second integer and the result. On the end of page I wrote some code to try to put the results to a database, but it only writes first integer and second integer but not the actual result.

I am beginner at this.

First part of code as you can see, is the actual calculation of greatest common divisor. Second part which I wrote is supposed to write results to my database, but it only writes 1 and 2 integer not the actual results.

<!DOCTYPE HTML>
<?php
#Created Dec 18 02:09:31 2016
#author: Justin, owner of linuxwebdevelopment.com
$first_integer = $_POST['integer1'];
$second_integer = $_POST['integer2'];
**$result = $_POST ['result'];**   //These I wrote myself i think it is wrong

function gcd($x, $y)
{
    /*This function finds the gcd (greatest common divisor)
    of the two integers $x and $y and returns the gcd
    It uses the Euclidean Algorithm.
    The general form for the Euclidean Algorithm is
    r_k = q_(k+2)*r_(k+1) + r_(k+2)
    Here _ denotes subscripts. 
    Above Parentheses indicate that the subscript is more than 1 character long
    for the code I just remove the _ and ()
    initially $r_k = $x and $r_(k+1) = $y
    */
    if ($x == $y) {
        return 1;
    } elseif ($x == 0 or $y == 0) {
        return 1;
    } else {
        #we assume $x is greater than y. If $y is greater than $x, 
        #just switch them
        if ($y > $x) {
            $temp = $x;
            $x = $y;
            $y = $temp;
        }
        $rk = $x;
        $rkp1 = $y;
        $rkp2 = $rk % $rkp1;
        if ($rkp2 == 0)
            return $rkp1;
        else {
            while ($rkp2 != 0) {
                $rk = $rkp1;
                $rkp1 = $rkp2;
                $rkp2 = $rk % $rkp1;
            }
            return $rkp1;
        }
    }
}

$result = gcd($first_integer, $second_integer);
echo "Najväčší spoločný deliteľ $first_integer a $second_integer je $result";

**$link = mysql_connect("localhost", "root")
or die("Nelze se připojit: " . mysql_error());
print "Připojeno úspěšně";
mysql_select_db("delitel") or die("Nelze vybrat databázi");
$a = $_POST["integer1"];
$b = $_POST["integer2"];
$c = $_POST["result"];
mysql_query("INSERT INTO delitel(integer1,integer2,vysledok) values ('$a','$b','$c')");
mysql_close($link);
include("vypis.php");**
?>

And these I wrote myself but it only writes to a database first and second integer not the result. I need to write to a database: Result first integer and second integer. I wrote the two integers succesfully to my database, but the result doesn't end up there.

  • 写回答

1条回答 默认 最新

  • douhan4812 2017-11-25 13:32
    关注

    $_POST['result'] is not showing up in the database because it is a null value. No data is getting stored in your $c = $_POST['result']; So instead of using $_POST['result'] simply use $c = $result as you are storing the return of your gcd funtion in $result.

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示