使用php将数据存储到mysql数据库

时间:2015-03-25 19:03:00

标签: php mysql

我的代码不会插入学生表中我不知道你们可能有什么帮助错了吗?我尝试在每一行打印和错误消息,找出错误的位置,但我没有错误,所以我很困惑

<?PHP

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



$link = mysql_connect('localhost','root','');

if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}


$sql    = "INSERT INTO  student (FirstName, LastName, UserName,Password,  confirmP ,phoneNum,Email) VALUES ('$_POST[FN]','$_POST[LN]','$_POST[userName]','$_POST[password]','$_POST[confirmPass]','$_POST[number]','$_POST[email]') ";

mysql_query($sql,$link);


mysql_close($link);

}


?>

这里是html表单:

     <form  method="post" action="Register.php">
    <div class="contact-to">

     <P> <input name="FN" type="text" class="text" value="First name"  > 

      <input name="LN" type="text" class="text" value="Last name"  style="margin-left: 10px">
      <input name="userName" type="text" class="text" value="username"  style="margin-left: 10px">
    </div>
      <div class="contact-to">

      <input name="password" type="text" class="text" value="Password"  style="margin-left: 10px">
      <input name="confirmPass" type="text" class="text" value="Confirm password" style="margin-left: 10px">
       <input name="number" type="text" class="text" value="Phone Number" >
        <input name="email" type="text" class="text" value="email" >
    </div>

    <div> <input  value="submit" type="submit" class="submit"> </div>
    </form>

2 个答案:

答案 0 :(得分:3)

如果您在用户点击按钮后尝试验证$_POST['submit'],则应在输入提交标记中包含属性name,如下所示:

<input value="submit" type="submit" name="submit" class="submit">

然后你可以检查$ _POST:

if(isset($_POST['submit'])) {
    // your code
}

PHP将始终在没有属性名称的情况下返回false ...

答案 1 :(得分:0)

将sql insert查询代码更改为:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

function debug($data){
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}
function getConnected($host,$user,$pass,$db) {

    $mysqli = new mysqli($host, $user, $pass, $db);

    if($mysqli->connect_error)
        die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

    return $mysqli;
}

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

    debug('Connecting database...');

    $link = getConnected('localhost','root','', 'stackoverflow');

    debug('Database connected...');
    $sql    = "
        INSERT INTO  `student` (`FirstName`, `LastName`, `UserName`, `Password`, `confirmP`, `phoneNum`, `Email`) 
        VALUES (
            '{$_POST['FN']}',
            '{$_POST['LN']}',
            '{$_POST['userName']}',
            '{$_POST['password']}',
            '{$_POST['confirmPass']}',
            '{$_POST['number']}',
            '{$_POST['email']}'
        )";

    if (!mysqli_query($link, $sql)) {
        debug("Errormessage: ". mysqli_error($link)."\n");
    }else{
        debug('Query executed successfully...');
    }

    mysqli_close($link);
}
?>

同时将表单提交按钮代码更改为:

<div> <input  name="submit" value="submit" type="submit" class="submit"> </div>