duanjunao9348 2017-04-21 00:56
浏览 27
已采纳

将用户名转换为userID以进入评论框

<?php
   include("connection.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($conn,$_POST['username']);
      $mypassword = mysqli_real_escape_string($conn,$_POST['password']); 
     $row['userID'] = $myuserid;


      $sql = "SELECT * FROM u803621131_login.users WHERE  username = '$myusername' and password = '$mypassword'";
      $result = mysqli_query($conn,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_start("myuserid");


$_SESSION['login_user'] = $myusername;
$_SESSION['login_id'] = $myuserid;


         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>
<html>

   <head>
      <title>Login Page</title>

      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }

         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }

         .box {
            border:#666666 solid 1px;
         }
      </style>

   </head>

   <body bgcolor = "#FFFFFF">

      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

            <div style = "margin:30px">

               <form action = "" method = "post">
                  <label>UserName  :</label><input type = "text" name = "username" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>

               <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

            </div>

         </div>

      </div>

   </body>
</html>

Login.php - The login page with all the changed parts, the actual login works as it should. although it is hard to tell if there are any other issues

<?php session_start();
include'../../connection.php';?>
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link rel="stylesheet" type="text/css" href=".../../../../style.css">
    <title>Home</title>

    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
<?php include('../../main/main.php');?>
</head>
  <body>



<div class=containermain>
  <h1>I5-6600k.php</h1>
<form action="ratepost.php" method="post">

<label for="rating">rating:</label>
<select name="rating" id="rating" value="rating" >
<option>
    <option value="1">1 </option>
    <option value="2">2</option>
    <option value="3">3 </option>
    <option value="4">4</option>
    <option value="5">5</option>
</option>
</select>
<input type="submit" value="Submit">
</form>



  <h2>graphics card write up................</h2>
  <?php echo "Hello " . $_SESSION['user']; ?>
  <p>&nbsp;</p>
  <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>

<div
  class="fb-like"
  data-share="true"
  data-width="450"
  data-show-faces="true">
</div>

<!---------------------------------------COMMENT BOX---------------------------------------------------->

<div class="comments" align="center">
<form action="" method="post" >
<textarea rows="4" cols="50" name="comment">
Please type a comment if you are logged in....


</textarea>
<input type="submit" value="Submit">
</form>


<?php

if (isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])) {
 $id = $_SESSION['login_id'];
$sqlinsert = "INSERT INTO comment (userID, comment, dCpuID) VALUES ('$id', '$comment', '1')";
if(mysqli_query($conn, $sqlinsert)){


      header("Location: i5-6600k");
} else {
    echo "ERROR: Could not able to execute $sqlinsert. " . mysqli_error($conn);
}
}

    // close connection










$sql  = "SELECT `users`.`username`, `comment`.`comment`, `comment`.`timestamp`
"

    . "FROM `users`
"

    . "LEFT JOIN `comment` ON `users`.`userID` = `comment`.`userID` 
"

    . "where dCpuID = 1";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table><tr><th>Username</th><th>Comment</th><th>Timestamp</th>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr><td>" . $row["username"]. "</td><td>" . $row["comment"]."</td><td>"  . $row["timestamp"]. "</td>";
     }
     echo "</table>";
} else {
     echo "0 results";
}


?>  
</div>
<?php include('../../assets/footer.php');?>

<div class="fb-comments" data-href="http://www.computercomparison.tk/#home" data-numposts="5"></div>
  </body>
</html>

Have included entirety of 2nd page, incase there may be clashes with other parts of the code in the site that may be pointed out.

Also you will find lots of code in strange places, only testing bits at the mo.

<?php
   include('connection.php');

   session_start();

   $user_check = $_SESSION['login_user'];

   $ses_sql = mysqli_query($conn,"select username, from users where username = '$user_check' ");


   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

   $login_session = $row['username'];


   if(!isset($_SESSION['login_user'])){
      header("location:login.php");


   }
?>

Have this session.php file, didn't think it was too relevant but changing it around did affect logging in and stuff, it is in good condition here, wonder if there is anything i need to change here too? it is linked to the welcome.php

  • 写回答

1条回答 默认 最新

  • douya8978 2017-04-21 01:09
    关注

    Following the error message you connected a column for the comment authors ID to one in your account table using a foreign key.

    As shown in your picture they're both INT. But you are trying to insert a VARCHAR (the username) into this column instead.

    My approach would be to get the user's ID by a sql query or even better save the users ID to the session:

    session_start();
    $_SESSION['login_user'] = $usernameFromFormOrWhatever;
    $_SESSION['login_id'] = $usersID;
    

    So you can fill your userID column with it:

    $id = $_SESSION['login_id'];
    $sqlinsert = "INSERT INTO comment (userID, comment, dCpuID) VALUES ('$id', '$comment', '1')";
    

    Additionally the entered ID in your comments table must also appear in a row of your accounts table as ID of a user. Otherwise you will get an error message like you do now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能