为什么它不更新数据库中的URL

时间:2012-03-07 09:14:44

标签: php mysql

我有一个名为ques.php的网页。如果用户的回答是正确的,他将被引导到下一个ques1.php。 check.php检查用户发布的答案,如果正确,我想将新URL(ques1.php)存储在数据库的用户帐户中。


check.php

<?php

  require_once("./include/membersite_config.php");

  if (!$fgmembersite->CheckLogin()) {
    $fgmembersite->RedirectToURL("login.php");
    exit;
  }

  mysql_connect("localhost", "root", "") or die(mysql_error()); 
  mysql_select_db("simplelogin") or die(mysql_error()); 

  $data = mysql_query("SELECT * FROM member") or die(mysql_error()); 

  while($info = mysql_fetch_array( $data )) {
    // print $info['username'];
    if ($info['username'] == $fgmembersite->UserName()) {
      $fullname = $info['name'];
      $username = $info['username'];
      $password = $info['password'];
      $email = $info['email'];
      $url = $info ['url'];
      break;
    }
  }

  $answer = $_POST['answer'];
  if ($answer == "correct") {
    "UPDATE `simplelogin`.`member`
     SET `url` = 'ques1.php'
     WHERE
       `member`.`name` = '$fullname' 
       AND `member`.`email` = '$email'
       AND `member`.`username` = '$username' 
       AND `member`.`password` = '$password' 
       AND `member`.`confirmcode` = 'y'
       AND `member`.`url` = '$url'";

    //in place of above update query i had also used
    //"UPDATE  member 
    //SET url = 'ques1.php'
    //WHERE username = '$username'"

    Header("Location:ques1.php");

  } else {

    Header("Location: ques.php");

  }

?>

function UserName() {
  return isset($_SESSION['user_name'])?$_SESSION['user_name']:'';
}

的login.php

<?php

  require_once("./include/membersite_config.php");

  if (isset($_POST['submitted'])) {

    if ($fgmembersite->Login()) {

      //$fgmembersite->RedirectToURL("login-home.php");

      mysql_connect("localhost", "root", "") or die(mysql_error()); 
      mysql_select_db("simplelogin") or die(mysql_error());  

      $data = mysql_query("SELECT * FROM member") or die(mysql_error()); 
      while ($info = mysql_fetch_array( $data )) {
        if ($info['username']==($fgmembersite->SafeDisplay('username'))) {
          $url = $info['url'];
          break;
        }
      }

      $fgmembersite->RedirectToURL("$url");

    }
  }

?>

login.php我正在从数据库中检索URL并重定向用户 - 默认情况下,URL为用户是ques.php。

为什么我的查询没有更新数据库中的网址?

3 个答案:

答案 0 :(得分:2)

"UPDATE `simplelogin`.`member` SET `url` = 'ques1.php' WHERE 
  `member`.`name`  ='$fullname' 
AND `member`.`email` = '$email' AND `member`.`username` = '$username' 
AND `member`.`password` = '$password' 
AND `member`.`confirmcode` = 'y' AND `member`.`url` = '$url'" ;

似乎没有作为查询运行,您没有将它放在mysql_query()函数中,因此它不知道您要对该语句执行什么操作。

请改为尝试:

mysql_query(
"UPDATE `simplelogin`.`member` SET `url` = 'ques1.php' WHERE 
`member`.`name`  ='$fullname' 
AND `member`.`email` = '$email' AND `member`.`username` = '$username' 
AND `member`.`password` = '$password' 
AND `member`.`confirmcode` = 'y' AND `member`.`url` = '$url'");

由于以下评论而更新:

试试这个,它已被重写和简化,应该可以工作,如果没有请你的端口收到错误信息

mysql_query("
    UPDATE
        member
    SET
        url = 'ques1.php'
    WHERE
        name = '$fullname'
    AND
        email = '$email'
    AND
        username = '$username'
    AND
        password = '$password'
    AND
        confirmcode = 'y'
    AND
        url = '$url'
") or die('Unable to update members URL: ' . mysql_error());

答案 1 :(得分:1)

执行查询dude ....使用mysql_query("$your_update query");

答案 2 :(得分:1)

因为您正在循环一组数据库结果并与您已有的值进行比较,只是为了获得您已有的值。充其量,这将验证用户是否存在于数据库中,最糟糕的是它根本不做任何事情。

您真的需要使用数据库表的主键作为UPDATE。最佳实践规定,这应该是一个自动递增的整数,除了识别行之外,它与数据无关。初始化$fgmembersite对象时,此值应存储在其中,因此可以在任何需要引用用户的数据库查询中轻松使用。最坏的情况是,表格的username列应该有唯一索引。

您可以完全删除SELECT查询 - 您已拥有用户名,因此您可以直接在UPDATE中使用此代码:


check.php:

<?php

  require_once("./include/membersite_config.php");

  // Redirect to login page if not already authenticated
  if (!$fgmembersite->CheckLogin()) {
    $fgmembersite->RedirectToURL("login.php");
    exit;
  }

  // Define DB connection info in variables for readability/maintainability
  $dbHost = 'localhost';
  $dbUser = 'root'; // NEVER use root for a live website!
  $dbPass = ''; // A blank root password? Really?
  $dbName = 'simplelogin';

  // Connect to database - NEVER show the result of mysql_error() in a live site!
  mysql_connect($dbHost, $dbUser, $dbPass) or die(mysql_error()); 
  mysql_select_db($dbName) or die(mysql_error()); 

  if ($_POST['answer'] == 'correct')

    // Update the database with the new URL
    $query = "
      UPDATE `member`
      SET `url` = 'ques1.php'
      WHERE `username` = '".mysql_real_escape_string($fgmembersite->UserName())."'
    ";
    mysql_query($query) or die(mysql_error());

    // This line should help you debug the query. REMOVE IT before putting this script on a live site!
    if (!mysql_affected_rows()) die("No rows were affected by the query.\nQuery: $query\nError: ".mysql_error());

    // Redirect to ques1.php
    // Note that a header redirect should provide a FULL url, not just a relative path.
    header("Location:ques1.php");

  } else {

    // Redirect to ques.php
    header("Location: ques.php");

  }

?>

的login.php

<?php

  require_once("./include/membersite_config.php");

  if (isset($_POST['submitted']) && $fgmembersite->Login()) {

    // Define DB connection info in variables for readability/maintainability
    $dbHost = 'localhost';
    $dbUser = 'root'; // NEVER use root for a live website!
    $dbPass = ''; // A blank root password? Really?
    $dbName = 'simplelogin';

    // Connect to database - NEVER show the result of mysql_error() in a live site!
    mysql_connect($dbHost, $dbUser, $dbPass) or die(mysql_error()); 
    mysql_select_db($dbName) or die(mysql_error()); 

    // Fetch the URL from the database
    $query = "
      SELECT `url`
      FROM `member`
      WHERE `username` = '".mysql_real_escape_string($fgmembersite->UserName())."'
    ";
    $result = mysql_query($query) or die(mysql_error());
    if (!mysql_num_rows($result)) die('Invalid user name');
    $info = mysql_fetch_assoc($result);
    $url = $info['url'];

    // Redirect to URL
    // Add some error checking to verify that $url actually contains something valid!
    $fgmembersite->RedirectToURL($url);

  } else {

    // What happens if the condition fails?

  }

?>