2012-01-24 62 views
0

我正在構建一個用於社會科學實驗的網站。完成表格後,用戶將被分配兩次處理。因此,所有的治療方法都會得到同樣的迴應,我希望爲用戶分配最少的治療方案。PHP/MySQL基於類別的最小響應分配類別

選擇處理後,腳本應該對響應字段+1,並相應地更新數據庫,以及使用已分配的處理更新用戶記錄。

在此之後,用戶應該被髮送到一個頁面,取決於他們已被分配的「treat1」。

我寫了下面的代碼,但它似乎沒有執行任何功能,不是將用戶重定向到其他treatment0.html

<?php 

session_start(); 

//Connect to Database 
$con = mysql_connect("db.host.com","user","password"); 
if (!$con) 
    { 
    die('Could Not Connect: ' .mysql_error()); 
    } 


//Select Database 
mysql_select_db("myDB", $con); 


//Get treatment id's and store in SESSION variables 
$_SESSION['treat1'] = mysql_query("SELECT TOP 1 treat1_id FROM treatment1 ORDER BY response ASC"); 
$_SESSION['treat2'] = mysql_query("SELECT TOP 1 treat2_id FROM treatment2 ORDER BY response ASC"); 


//Add 1 to response field in treatment databases 
mysql_query("UPDATE treatment1 SET RESPONSE='RESPONSE + 1' WHERE treat1_id = ".$_SESSION['treat1'].""); 
mysql_query("UPDATE treatment2 SET RESPONSE='RESPONSE + 1' WHERE treat2_id = ".$_SESSION['treat2'].""); 


//Update user record with assigned treatments 
mysql_query("UPDATE user SET TREAT1=".$_SESSION['treat1']." , TREAT2=".$_SESSION['treat2']." WHERE email = '[email protected]'"); 

//Send user to page based on treat1_id 
if ($_SESSION['treat1'] == 0) 
     $url = 'treatment0.html'; 
elseif ($_SESSION['treat1'] == 1) 
    $url = 'treatment1.html'; 
else 
    $url = 'treatment2.html';   

    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 


//Close connection 
mysql_close($con); 


?> 

Dreamweaver中並沒有給予任何語法錯誤,當我運行腳本,它不會拋出任何錯誤,但不會執行我期待的任何功能。

任何幫助將不勝感激。

+0

你爲什麼要在會話中存儲治療ID? – bowlerae

+0

我是PHP新手,稍後需要它們,因此我認爲將它們存儲在會話中而不是再次查詢數據庫會更好。 –

+0

好吧,然後繼續使用會話,我想除非有人提出一個更好的方法。嘗試在查詢的末尾添加$ con,例如:'mysql_query(「QUERY CONTENT」,$ con);' – bowlerae

回答

0

的mysql_query返回的資源或FALSE在首位?

PHP.net description

+0

感謝您的幫助。我現在通過將查詢結果傳遞給mysql_fetch_array來工作。 –

0
$treatment1 = mysql_query("SELECT id FROM treatment1 ORDER BY response ASC LIMIT 1"); 
$treatment2 = mysql_query("SELECT id FROM treatment2 ORDER BY response ASC LIMIT 1"); 


//Add 1 to response field in treatment databases 
mysql_query("UPDATE treatment1 SET RESPONSE='RESPONSE + 1' WHERE treat1_id = $treatment1"); 
mysql_query("UPDATE treatment2 SET RESPONSE='RESPONSE + 1' WHERE treat2_id = $treatment2"); 


//Update user record with assigned treatments 
mysql_query("UPDATE user SET TREAT1 = '$treatment1' , TREAT2 = '$treatment2' WHERE email = '[email protected]'"); 

//Send user to page based on treat1_id 
if ($treament1 == 0) 
     $url = 'treatment0.html'; 
elseif ($treatment == 1) 
    $url = 'treatment1.html'; 
else 
    $url = 'treatment2.html';   

    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';