我正在建立一个用于社会科学实验的网站。完成表格后,将为用户分配两种治疗方法。为了使所有治疗方法得到同等响应,我希望为用户分配响应次数最少的治疗方法。
选择治疗后,脚本应该在响应字段中加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@address.com'");
//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 :(得分: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@address.com'");
//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.'">';
答案 1 :(得分:0)
mysql_query 是否首先返回资源或 FALSE ?