2014-07-22 31 views
0

我幾乎是一個完整的初學者,當涉及到PHP,並且在更新數據庫中的值時遇到了一些問題 - 腳本觸發時它會更新每個用戶的值在PHP腳本更新所有用戶的價值而不是個人用戶的價值

列,而不僅僅是個人誰是記錄的一些背景資料:

數據庫名稱:「用戶」

表「用戶」與列用戶ID,用戶名,密碼,EMAILADDRESS,提供。

我正在處理的網站允許用戶完成多個優惠,並在完成後獲得獎勵。商品列的默認值爲「1」,在登錄時,用戶將根據商品列中的值重定向。 (因此,首次登錄用戶被重定向到example.com/offer1,在優惠1完成後,此值更新,以便下次登錄用戶重定向到example.com/offer2-實質上存儲用戶進度)

This是在報價完成後播放的腳本(在這種情況下,在報價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); 
?> 

這樣確實可以在更新的值,但這樣做對所有用戶在數據庫中,而不僅僅是一個用戶誰已登錄。那麼讓我們說吉姆已經完成了此優惠,他的優惠價值將會更新爲3,但所有其他用戶在只有吉姆的時候將其優惠價值設爲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'].'\'" />'; 

所以對於1個用戶的默認值重定向到example.com/offer1等

繼承人也是我全登錄腳本的情況下,它需要:

<?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> 

最後,繼承人基地。 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

'更新FOO集合欄= 3 WHERE'。使用WHERE!確定您想要更新的用戶。 [示例](http://www.tutorialspoint.com/mysql/mysql-update-query.htm) – ex3v

回答

2

必須指定一個where條件

更改下面的代碼

mysqli_query($con,"UPDATE users SET offer=3".$plus); 

mysqli_query($con,"UPDATE users SET offer=3".$plus."where Username = $_SESSION['username']"); 

除非那裏的條件給定的更新會在表中可以應用到所有記錄

+0

感謝@Vijayaragavendran,出於某種原因,雖然我在替換代碼時遇到了語法錯誤。似乎無法糾正它。 – raymond567

+0

你可以發佈你得到的錯誤 – Vijayaragavendran

+0

只是在第6行語法錯誤繼承人完整的代碼(感謝您的幫助,但!)<?php session_start(); $ con = mysqli_connect(「localhost」,「username」,「pass」,「user」); $ select = mysqli_fetch_assoc(mysqli_query($ con,「SELECT offer FROM user WHERE Username ='」。$ _ SESSION ['username']。「'」)); $ plus = $ select ['offer'] ++; mysqli_query($ con,「UPDATE users SET offer = 3」。$ plus。「where user_id」= $ id); header(「location:http://example.com/offer4".$plus); ?> – raymond567

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']."'"); 
+0

謝謝@Sérgio Bernard;嘗試交換這個,現在它根本不更新數據庫中的值 – raymond567

+0

可能有些SQL錯誤...將這段代碼放在UPD UPDATE查詢之後,看到mySQL服務器中的錯誤: print「Mysql error :「.mysqli_error(); –