-1

このスクリプト内の変数はまったく機能していません。誰かがそれを助けることができれば、それは私を夢中にさせます.

<?php 
$db = mysql_connect('HOST', 'USER', 'PASS') or die('Could not connect: ' . mysql_error()); 
mysql_select_db('DBNAME') or die('Could not select database');

// Strings must be escaped to prevent SQL injection attack. 
$name = mysql_real_escape_string($_GET['name'], $db); 
$score = mysql_real_escape_string($_GET['score'], $db); 
$QuestionN = mysql_real_escape_string($_GET['QuestionN'], $db);        
$hash = $_GET['hash']; 
$num = (int)$QuestionN;
$var1     = mysql_real_escape_string($_POST['var1']);   
$var2     = mysql_real_escape_string($_POST['var2']);           


$secretKey="SecretKey"; # Change this value to match the value stored in the client javascript below 

$real_hash = md5($name . $score . $secretKey); 
if($real_hash == $hash) { 
$query = mysql_query("UPDATE Quiz1 SET " . $var1 . " = (1 + ". $var1 .")". " WHERE Question = " . $var2);
//$query = mysql_query("UPDATE Quiz1 SET " . $score . " = (1 + ". $score .")". " WHERE Question = " . $QuestionN);
//$query = mysql_query("UPDATE Quiz1 SET A = (1 + A ) WHERE Question = 1 ");

    $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
} 
print($var1) ; 
?>

これを PDO でクリーンアップしました。必要な人のために、より良い PHP プラクティスを備えた同じコードをここに示します。

<?php
        // Configuration
        $hostname = 'host';
        $username = 'user';
        $password = 'pass';
        $database = 'DBNAME';
    //$score = 'A' ; 

        $name = $_GET['name']; 
        $score = $_GET['score']; 
    $QuestionN = $_GET['QuestionN'];   
        $table = $_GET['table'];
$hash = $_GET['hash']; 
    $num = (int)$QuestionN;
        $secretKey="SecretKey"; # Change this value to match the value stored in the client javascript below 

        $real_hash = md5($name . $score . $secretKey); 
       // if($real_hash == $hash) { 



        try {
            $conn = new PDO('mysql:host='. $hostname .';dbname='. $database, $username, $password);
    echo "Connected to database"; // check for connection
    //$dbh->exec("UPDATE Quiz1 SET $score = 1 WHERE Question = 1");  // THIS DOES NOT  
    //$dbh->exec("UPDATE Quiz1 SET B = 1 WHERE Question = 1"); // THIS WORKS
$conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8
//$score = 'A';
//$scoreB = 'A'; 
//14
$author = 'Imanda';
//15
//$id = 1 ;
//16
// query
//$table = 'Quiz1';
//17
$sql = "UPDATE $table 

        SET $score = ( 1 + $score)

        WHERE Question = ?  "  ;
//20
$q = $conn->prepare($sql);
//21
$q->execute(array($QuestionN));




    //AddScore($dbh,'Quiz1','A','1'); 



}
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
//  }
?>
4

2 に答える 2

1

mysql_queryを2つの場所で使用していますが、1つの場所でのみ使用する必要があります。

$query = mysql_query("UPDATE Quiz1 SET " . $var1 . " = (1 + ". $var1 .")". " WHERE Question = " . $var2);

    $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
} 

$query = "UPDATE Quiz1 SET " . $var1 . " = (1 + ". $var1 .")". " WHERE Question = " . $var2;
    $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
} 
于 2012-07-21T05:36:18.527 に答える
0

タスクを支援するために、必ずMySQLのドキュメントを調べてください。

mysqli: http: //php.net/manual/en/book.mysqli.php

PDO: http: //php.net/manual/en/book.pdo.php

于 2012-07-21T05:29:58.577 に答える