2017-11-24 4 views
-1

データベースに次のように書く必要があります:最初の整数、2番目の整数、結果ページの最後に、結果をデータベースに格納するコードを書きましたが、実際の結果ではなく最初の整数と2番目の整数だけを書き込みます。PHPからデータベースへの結果を書く方法

私は初心者です。

ご覧のとおり、コードの最初の部分は、最大公約数の実際の計算です。私が書いた第二の部分はデータベースに結果を書き込むはずですが、実際の結果ではなく1と2の整数しか書いていません。

<!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");** 
?> 

これらは私が書いたものですが、結果は1番目と2番目の整数だけデータベースに書き込まれます。私はデータベースに書き込む必要があります:結果の最初の整数と2番目の整数。私は2つの整数をデータベースに正常に書きましたが、結果はそこに終わりません。

+1

「mysql_ *」関数の使用をやめてください。彼らは3年以上前倒しされており、PHP 7以降では動作しなくなります。あなたのコードは[SQLインジェクション](https://en.wikipedia.org/wiki/SQL_injection)に対して広く公開されています。これは基本的に、あなたのウェブサイトの訪問者があなたのデータベースで必要なことを何時でもやり遂げるのに1分もかからないことを意味します。それはそれを破壊することを含む。 [MySQLi](http://php.net/manual/en/book.mysqli.php)または[PDO](http://php.net/manual/en/book.pdo.php)を組み合わせて使用​​する[Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement)を参照してください。 – icecub

+0

それはちょうど私がそれを完了する必要がある学校プロジェクトのためのいくつかの機密データを持つ実際のウェブサイトではありません... – Potatoe989898959

+1

は関係ありません。あなたがまだ学んでいるなら、最初から正しい方法で学んでください。あなたが学びたくないので、弱い言い訳を使わないでください。 PHP 7では、mysql_ *関数をもう使用することもできません。彼らは削除されました。 – icecub

答えて

0

$_POST['result']は、NULL値であるためデータベースに表示されません。 データが格納されていません$c = $_POST['result']; $_POST['result']を使用する代わりに、$c = $resultを使用すると、gcd機能の返品を$resultに保存するだけです。

関連する問題