将数据插入MySQL无效

时间:2015-02-14 22:02:08

标签: php mysql sql database

我有一个简单的学生注册到某个大学取决于他们的STUDENT_IDPASSWORD

登录后,向他们展示一些必须填写的信息,如“名字,姓氏,出生日期,学位等”。当我尝试插入这些信息时,它不起作用。

数据库名称:users,表名:members_info。这是我使用的代码:

的index.php

<?php session_start(); ?>
<form action="login.php?login=yes" method="post" name="login">
<input type='text' name='username' id='userid' placeholder='Your ST_ID' >
<input type='password' name='password' required='required'>
</form>
 <input type='submit' name='resultbtn'  id='submit' value='Log In' >

的login.php

<?php
error_reporting(0);
$username = $_POST['username'];
$password= $_POST['password'];
$login = $_GET['login'];

setcookie("username","$username",time()+86400);

if($login=='yes') {
    $con = mysql_connect("localhost","root","");
    mysql_select_db("users");
    $get = mysql_query("SELECT count(id) FROM members_info WHERE username='$username' and password='$password' ");
    $result = mysql_result($get,0);

if($result !=1){
    echo "error with login";
}
else {
$_SESSION['username'] = $username;

$sqll = "SELECT * FROM members_info WHERE username='$username' ";
$myqu = mysql_query($sqll,$con);
    include 'info.php';
  }
}
?>

info.php的

...
<form method='post' action='mmbrs_inf.php'>
<input type='text' name='stname' placeholder='Your Name'>
<input type='text' name='stfname' placeholder='Father's Name'>
<input type='date' name='birthday'>
<input type='text' name='city'  placeholder='Your City'>
<select name='Select1'>
                <option>50</option>
                <option>60</option>
                <option>70</option>
                <option>80</option>
                <option>90</option>
                <option>100</option>
</select>

<input type='Reset' value='Reset' name='resetall'/>
    <input type='submit' value='Send'  name='submit'/>

</form>
...

mmbrs_inf.php

$stname = $_POST['stname'];
$stfname = $_POST['stfname'];
$birthday = $_POST['birthday'];
$city = $_POST['city'];
$english_dgree =$_POST['eng_dgree'];
//connect
mysql_connect("localhost", "root", "");
mysql_select_db("users");


//check if submit is pressed 
if(isset($_POST['submit'])){
    //inserting
    $query = mysql_query("INSERT INTO members_info (student_name,student_father,birthdate,mycity,english) VALUES($stname,$stfname,$birthday,$city,$english_dgree) 
WHERE username='$_COOKIE[username]' ");
    echo"Your Informations Has Been Sent Successfully,....";        
}
else{
echo"An Error Has Accoure ";
}

它向我显示消息“您的信息已成功发送,......”但我的数据库中没有插入任何内容。

1 个答案:

答案 0 :(得分:3)

INSERT语句具有WHERE子句。您要么插入新行还是更新新行。你不能同时做两件事。

此外,您的字符串值缺少引号。

所以:

INSERT INTO members_info 
(student_name,student_father,birthdate,mycity,english_dgree) 
VALUES('$stname','$stfname','$birthday','$city','$english_dgree');

或:

 UPDATE members_info 
 SET student_name = '$stname', 
     student_father = '$stfname', 
     birthdate = '$birthday', 
     mycity = '$city', 
     english_dgree = '$english_dgree'
 WHERE username='$_COOKIE[username]';