将数据表单插入mysql表

时间:2014-03-23 14:39:40

标签: php mysql forms

有人告诉我哪里有错误

html代码是:

<form action="insert.php" method="POST">
<label>Firstname:</label> <input type="text" name="firstname" value="first name" /><br />
<label>Lastname:</label> <input type="text" name="lastname" value="last name" /><br />
<label>email:</label> <input type="text" name ="email" value="email" /><br />

php代码是:

<?php
 if(isset($_POST['submit'])!='')
    {
    $fname=$_POST ['firstname'];
    $lname=$_POST ['lastname'];
    $email=$_POST ['email'];
    mysql_connect ("localhost","root","mydatabase") or die (mysql_error ());
    mysql_select_db ("my_db") or die (mysql_error ());
    mysql_query ("ISERT INTO userinfo (`firstname`,`lastname`,`email`)
    VALUES ('$fname','$lname','$email')");
    echo "successfully updated" ;
    }

&GT;

2 个答案:

答案 0 :(得分:0)

INSERT而不是ISERT ..你有错字

mysql_query ("ISERT INTO userinfo (`firstname`,`lastname`,`email`)
              ^^^^^ 

修改代码..

<?php
if(isset($_POST['submit'],$_POST['firstname'],$_POST ['lastname'],$_POST ['email']))
{
    $fname=$_POST['firstname'];
    $lname=$_POST['lastname'];
    $email=$_POST['email'];
    mysql_connect ("localhost","root","mydatabase") or die (mysql_error ());
    mysql_select_db ("my_db") or die (mysql_error ());
    $result=mysql_query ("INSERT INTO userinfo (`firstname`,`lastname`,`email`)
    VALUES ('$fname','$lname','$email')");
    if($result){     echo "Successfully Updated" ; } else {die(mysql_error());}
}

mysql_*)扩展程序自PHP 5.5.0起已弃用,将来会被删除。相反,应使用MySQLiPDO_MySQL扩展名。切换到PreparedStatements甚至可以更好地抵御SQL注入攻击!

答案 1 :(得分:0)

试试这个

if ($_POST) {
    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
    $email = $_POST['email'];
    mysql_connect ("localhost","root","mydatabase") or die (mysql_error ());
    mysql_select_db ("my_db") or die (mysql_error ());
    mysql_query ("INSERT INTO userinfo (firstname,lastname,email)
    VALUES ('$fname','$lname','$email')");
    echo "successfully updated" ;
}

在您的示例中,我没有看到$_POST['submit']并且您错误地使用了INSERT