如何将数据从html表单插入到mysql数据库2中

时间:2014-06-21 23:56:32

标签: php mysql

**我想插入学生成绩,他计算出一些方程并将其发送到用户数据库中的单元格

我的问题是我无法将某个学生的结果发送到用户表中他自己的单元格 当我尝试这样做时,它总是(结果)转到我登录的第一个用户,我想我的会话的问题 可以任何1帮助请??? 我的index.php代码是**

<html>
<head>
<title>Home Work Page</title>
</head>

<body>
<form action="login.php?login=yes"  method="POST">
<table border=1>  
<tr> 
<td> username : <input type="text"  name="username"/></td>
<br />
<td>  password  :<input type="password"  name="password"/> </td>  
</tr>  
</table>

<input type="submit"  name="resultbtn"  value='login'/>
</form>
</body>
</html>

,我的login.php代码

<?php
error_reporting(0);
$username = $_POST['username'];
$password= $_POST['password'];
$login = $_GET['login'];
setcookie("username","$username",time()+15);
if($login=='yes') {
$con = mysql_connect("localhost","root","");
mysql_select_db("members");

$get = mysql_query("SELECT count(id) FROM users WHERE username='$username' and password='$password' ");
$resultt = mysql_result($get,0);

if($resultt !=1){
  echo "error with login";
  }
else {
  $_SESSION['username'] = $username;
  $sql = "SELECT * FROM users WHERE username='$username' ";
  $myval = mysql_query($sql,$con);
echo "welcome back  " . $_COOKIE['username'];
echo"
<br/>
<br/>
<br/>
<br/>
<html dir='rtl'>
<meta charset ='Windows-1256'>
</html>
<table>  
<tr> 
<th> </th>
</tr>";

if ($val = mysql_fetch_array($myval)) {
echo "<tr>";
echo " <td>" . "the question : calculate the triangle area if H = " . "</td>";
echo " <td> <input type='text'  value= $val[val1] disabled='disabled' size=5/> ". "</td>";
echo " <td>and L = <input type='text'  value= $val[val2]  disabled='disabled' size=5/> ". "</td>";

echo "</tr>";
     }
echo "</table>";
}
}
echo"<form action='result.php' method='POST'>
<br/>
<br/>
<br/>
<br/>
<table>
<tr>
<td> Hello student the result will be : <input type='text' name='add' ></td>
<td><input type='submit' name='submit' value='send results'></td>
</tr>
</table>
</form>
";
?>

和result.php(有问题的页面)代码是

<?php
error_reporting(0);
$val3 = $_POST["add"];

//connect
mysql_connect("localhost", "root", "");
mysql_select_db("members");

//insert

$insert_query = mysql_query("UPDATE users SET results='$val3' WHERE username ='$_SESSION[username]' ");

$query = mysqli_query($insert_query) or die(mysqli_error());


//check whether the data insertion was successful

if(!$insert_query)

echo "<p>Sorry! Something went wrong.</p>";

else

echo "<p>Thanks! Your Results has been Sent.</p>";

&GT;

我的logout.php代码是

<?php
session_start();
unset($_SESSION['username']);
session_destroy();
header ("location: index.php");
?>

请帮助

1 个答案:

答案 0 :(得分:0)

代码编写得很糟糕并且使用了mysql弃用函数,无论如何它都不起作用:

$insert_query = mysql_query("UPDATE users SET results='$val3' WHERE username ='$_SESSION[username]' ");

您无法将数组值放入字符串中。请尝试改为:

$username = $_SESSION['username'];
$insert_query = mysql_query("UPDATE users SET results='$val3' WHERE username ='$username' ");

或者:

$insert_query = mysql_query("UPDATE users SET results='$val3' WHERE username ='".$_SESSION['username']."'");

此外,这行没有任何意义,因为$ insert_query是mysql_query的结果:

$query = mysqli_query($insert_query) or die(mysqli_error());