对于PHP来说,我几乎是一个完全的初学者,并且在更新数据库中的值时遇到了一些问题 - 当脚本触发它时会更新列中的每个用户值,而不仅仅是记录的个人in。
一些背景信息:
数据库名称:“user”
表“用户”,其中包含用户ID,用户名,密码,电子邮件地址,优惠等列。
我正在努力的网站允许用户完成许多优惠,然后在完成后获得奖励。商品列的默认值为“1”,登录后,用户将根据商品列中的值重定向。 (因此,首次登录用户重定向到example.com/offer1,在商品1完成后,此值会更新,以便下次登录用户重定向到example.com/offer2 - 实质上是存储用户进度)
这是在要约完成后播放的脚本(在此情况下,在要约3完成后) - 旨在连接到数据库,然后更新用户“提供价值”,以便当他们下次登录时,他们将被定向到正确的报价 - 从而存储他们的进展:
<?php
session_start();
$con = mysqli_connect("localhost","name","pass","user");
$select = mysqli_fetch_assoc(mysqli_query($con,"SELECT offer FROM users WHERE Username = '".$_SESSION['username']."'"));
$plus = $select['offer']++;
mysqli_query($con,"UPDATE users SET offer=3".$plus);
header("location: http://example.com/offer4".$plus);
?>
这对更新值有效,但对数据库中的所有用户而言只是这样做,而不仅仅是登录的用户。所以,让我们说Jim完成了此优惠,他的优惠价值将会是3的更新,但所有其他用户将只有Jim应该将他们的报价值设置为3。 - 希望这是有道理的。如何更改上面的脚本,以便只设置登录的用户值。也许这是会话的问题?或者这部分剧本不起作用
$select = mysqli_fetch_assoc(mysqli_query($con,"SELECT offer FROM users WHERE Username = '".$_SESSION['username']."'"));
以防万一它有帮助,这是登录脚本的一部分,它根据用户商品列中的值重定向,工作正常。
echo '<meta http-equiv="refresh" content="0;URL=\'http://example.com/offer'.$row['offer'].'\'" />';
因此,对于重定向到example.com/offer1等的1个用户的默认值
如果需要,还可以使用我的完整登录脚本:
<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="main">
<?php
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
echo "<h1>Success</h1>";
echo "<p>We are now redirecting you to the member area.</p>";
echo '<meta http-equiv="refresh" content="0;URL=\'http://example.com/offer'.$row['offer'].'\'" />';
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
}
}
else
{
?>
<h1>Member Login</h1>
<p>Thanks for visiting! Please either login below, or <a href="register.php">click here to register</a>.</p>
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
<?php
}
?>
</div>
</body>
</html>
最后,继承人base.php
<?php
session_start();
$dbhost = "localhost"; // this will ususally be 'localhost', but can sometimes differ
$dbname = "user"; // the name of the database that you are going to use for this project
$dbuser = "name"; // the username that you created, or were given, to access your database
$dbpass = "password"; // the password that you created, or were given, to access your database
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>
非常感谢你的时间。
答案 0 :(得分:2)
您必须指定where条件
更改以下代码
mysqli_query($con,"UPDATE users SET offer=3".$plus);
到
mysqli_query($con,"UPDATE users SET offer=3".$plus."where Username = $_SESSION['username']" );
除非给出where条件,否则更新将应用于表中的所有记录
答案 1 :(得分:0)
您的问题似乎不是PHP,而是SQL ......
Repalce:
mysqli_query($con,"UPDATE users SET offer=3".$plus);
使用:
mysqli_query($con,"UPDATE users SET offer=".$plus." WHERE Username = '".$_SESSION['username']."'");