从html文本框将数据插入mysql。使用php/mysql

从html文本框将数据插入mysql。使用php/mysql,php,Php,我看不出哪里出了问题,它只是不允许我连接到mysql数据库,我只在尝试保存详细信息时收到错误消息。??????我认为在将值插入表中时,可能会出现显示$sql的问题。第一部分newstudent.php起作用,但sql.php不起作用 //new student.php <html> <head> </head> <body> <h2>Your details</h2> <form nam

我看不出哪里出了问题,它只是不允许我连接到mysql数据库,我只在尝试保存详细信息时收到错误消息。??????我认为在将值插入表中时,可能会出现显示$sql的问题。第一部分newstudent.php起作用,但sql.php不起作用

//new student.php
<html>
  <head>
  </head>
  <body>
     <h2>Your details</h2>
     <form name="frmdetails" action="sql.php" method="post">
        ID Number :
     <input name="txtid" type="text" />
        <br/>
    Password :
     <input name="txtpassword" type="text" />
        <br/>
    Date of Birth :
     <input name="txtdob" type="text" />
        <br/>
    First Name :
    <input name="txtfirstname" type="text" />
        <br/>
        Surname :
        <input name="txtlastname" type="text" />
    <br/>
        Number and Street :
    <input name="txthouse" type="text"   />
        <br/>
        Town :
        <input name="txttown" type="text"  />
    <br/>
        County :
    <input name="txtcounty" type="text"   />
        <br/>
         Country :
    <input name="txtcountry" type="text"   />
        <br/>
        Postcode :
        <input name="txtpostcode" type="text"   />
        <br/>
        <input type="submit" value="Save" name="submit"/>
      </form>
   </body>
   </html>

//sql.php
$conn=mysql_connect("localhost", "20915184", "mysqluser"); 
 mysql_select_db("db5_20915184", $conn);

// If the form has been submitted

$id=$_POST['txtstudentid'];
$password=$_POST['txtpassword'];
$dob=$_POST['txtdob'];
$firstname=$_POST['txtfirstname'];
$lastname=$_POST['txtlastname'];
$house=$_POST['txthouse'];
$town=$_POST['txttown'];
$county=$_POST['txtcounty'];
$country=$_POST['txtcountry'];
$postcode=$_POST['txtpostcode'];



    // Build an sql statment to add the student details
    $sql="INSERT INTO student

(studentid,password,dob,firstname,lastname,house,town,county,country,postcode) VALUES

('$id','$password','$dob','$firstname','$lastname','$house','$town','$county','$country','$postcode')";
    $result = mysql_query($sql,$conn);
        if($result){
echo"<br/>Your details have been updated";
echo "<BR>";
echo "<a href='Home.html'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection 
mysql_close($conn);
?>
//new student.php
你的细节
身份证号码:

密码:
出生日期:
名字:
姓:
编号及街道:
城镇:
县:
国家:
邮政编码:
//sql.php $conn=mysql_connect(“localhost”、“20915184”、“mysqluser”); mysql_select_db(“db5_20915184”,$conn); //如果表格已经提交 $id=$_POST['txtstudentid']; $password=$_POST['txtpassword']; $dob=$_POST['txtdob']; $firstname=$_POST['txtfirstname']; $lastname=$\u POST['txtlastname']; $house=$_POST['txthouse']; $town=$_POST['txttown']; $county=$_POST['txtcounty']; $country=$_POST['txtcountry']; $postcode=$_POST['txtpostcode']; //构建sql语句以添加学生详细信息 $sql=“插入到学生中” (学生ID、密码、出生日期、姓氏、姓氏、房屋、城镇、县、国家、邮政编码)值 (“$id”、“$password”、“$dob”、“$firstname”、“$lastname”、“$house”、“$town”、“$country”、“$postcode”); $result=mysql\u查询($sql,$conn); 如果($结果){ echo“
您的详细信息已更新”; 回声“
”; 回声“; } 否则{ 回声“错误”; } //密切联系 mysql_close($conn); ?>
在mysql_connect()中,用户名位于密码之前;
尝试在phpmyadmin中运行sql语句,看看它是否在那里工作

在if-else语句中,在回显“ERROR”的地方,尝试打印mysql\u ERROR()。如果用户名/密码组合错误,则表明mysql\u connect()是错误的

为了稍微整理一下,下面是if/else应该是什么样子

if($result){
  echo"<br/>Your details have been updated";
  echo "<BR>";
  echo "<a href='Home.html'>Back to main page</a>";
} else {
  echo "There has been an error <br/>";
  print mysql_error();
}
if($result){
echo“
您的详细信息已更新”; 回声“
”; 回声“; }否则{ echo“出现错误
”; 打印mysql_错误(); }
编辑:


另外,防止在所有发布的值上使用mysql\u real\u escape\u string()进行sql注入

从内部删除参数并放入空字符串。i、 e

 VALUES('','$password','$dob', 

etc etc

如果您的代码不完整,则必须在单击按钮时插入。在将字段保存到数据库中之前,检查字段是否已设置也很重要。在提交之前,过滤和清理用户输入也很重要。学习使用准备好的语句,使用mysqli准备好的语句或PDO任何适合您的语句,也不要将密码存储在纯文本/md5使用密码_hash()和密码_verify()中

准备好mysqli后的代码应该如下所示:

<html>
   <head>
   </head>
   <body>
      <h2>Your details</h2>
      <form name="frmdetails" action="sql.php" method="post">
         ID Number :
         <input name="txtid" type="text" />
         <br/>
         Password :
         <input name="txtpassword" type="text" />
         <br/>
         Date of Birth :
         <input name="txtdob" type="text" />
         <br/>
         First Name :
         <input name="txtfirstname" type="text" />
         <br/>
         Surname :
         <input name="txtlastname" type="text" />
         <br/>
         Number and Street :
         <input name="txthouse" type="text"   />
         <br/>
         Town :
         <input name="txttown" type="text"  />
         <br/>
         County :
         <input name="txtcounty" type="text"   />
         <br/>
         Country :
         <input name="txtcountry" type="text"   />
         <br/>
         Postcode :
         <input name="txtpostcode" type="text"   />
         <br/>
         <input type="submit" value="Save" name="submit"/>
      </form>
   </body>
</html>

你的细节
身份证号码:

密码:
出生日期:
名字:
姓:
编号及街道:
城镇:
县:
国家:
邮政编码:
sql.php

<?php



$servername = "localhost";
$username   = "20915184";
$password   = "mysqluser";
$dbname     = "db5_20915184";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection
if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
}


$errors = "";

if (isset($_POST['submit'])) { // submit button clicked


        // validate fields



        if (empty($_POST['txtstudentid'])) {

                echo "enter id";
                $errors++;
        } else {

                $id = userData($_POST['txtstudentid']);

        }

        if (empty($_POST['txtpassword'])) {

                echo "enter password";
                $errors++;
        } else {

                $password = userData($_POST['txtpassword']);

                $hash = password_hash($password, PASSWORD_DEFAULT); //hashing password
        }

        if (empty($_POST['txtdob'])) {
                echo "enter date of birth";
                $errors++;
        } else {

                $dob = userData($_POST['txtdob']);
        }
        if (empty($_POST['txtfirstname'])) {
                echo "enter first name";
                $errors++;
        } else {

                $firstname = userData($_POST['txtfirstname']);
        }

        if (empty($_POST['txtlastname'])) {

                echo "enter last name";
                $errors++;
        } else {

                $lastname = userData($_POST['txtlastname']);
        }

        if (empty($_POST['txthouse'])) {

                echo "enter house";
                $errors++;
        } else {

                $house = userData($_POST['txthouse']);
        }

        if (empty($_POST['txttown'])) {

                echo "enter town";
                $errors++;
        } else {

                $town = userData($_POST['txttown']);
        }

        if (empty($_POST['txtcounty'])) {

                echo "enter country";
                $errors++;
        } else {

                $country = userData($_POST['txtcounty']);
        }
        if (empty($_POST['txtpostcode'])) {

                echo "enter post code";
                $errors++;
        } else {

                $postcode = userData($_POST['txtpostcode']);
        }

        if ($errors <= 0) { //all fields are set no errors

                //start query


                //check if user id does not exist


                $statement = $conn->prepare("SELECT studentid FROM students WHERE studentid = ?");
                $statement->bind_param('s', $id);
                $statment->execute();

                $statement->bind_result($studentID);

                if ($statement->num_rows == 1) {

                        echo "the student Id " . $studentID . " already registered please login";

                } else {


                        // no results then lets insert


                        $stmt = $conn->prepare("INSERT INTO students (studentid,password,dob,firstname,lastname,house,town,country,postcode) VALUES(?,?,?,?,?,?,?,?,?)");
                        $stmt->bind_param("sssssssss", $id, $hash, $dob, $firstname, $lastname, $house, $town, $country, $postcode);
                        $stmt->execute();
                        echo "<p>Your Details have been updated<br> <a href=\"Home.html\">Back to main page";
                        $stmt->close();
                        $conn->close();



                }



        }






}

//filter userinput
function userData($data)
{

        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
}
?>

网上有很多关于这方面的好教程,希望能有所帮助,我也愿意接受建议和更正,以防我遗漏了什么

**>问号(?)(占位符)用于指定值。在

我们在bind参数函数中赋值的语句 我们的查询以安全的方式处理,并防止SQL注入**

在准备好的语句中,我们通过Bind参数函数将值传递或附加到数据库查询

您必须在查询中附加所有需要其值的变量及其相应的数据类型,就像我们传递的“s”表示变量包含字符串数据类型一样


要在准备好的语句中执行查询,必须对查询对象使用execute()函数。

20915184是您的db用户还是db密码?请注意,您的脚本容易通过SQL注入被篡改。使用
mysql\u real\u Escape\u string()
$password=mysql\u real\u Escape\u string($\u POST['password'])一样对所有
$\u POST
输入值进行转义
如果您收到错误消息,请
echo mysql\u ERROR()
查看失败的原因。关于样式和安全性的另一个注意事项是:在数据库中以纯文本形式存储密码是不安全的。相反,在数据库中存储一个类似于
sha1($password)
的散列,在后续登录时,检查
sha1($password)
是否等于数据库中的值。通过javascript传递值,如下->var x=escape($('#xyz'.val()); 并在PHP中接收如下值->$x=mysql\u real\u escape\u string($\u REQUEST['xyz']);然后将$x传递到sql查询$sql=“插入公司测试(名称、货币)值('ABC小部件','$x')”;这对我有用..用户名是20915184,密码是mysqluserAgreed。这将告诉您SQL拒绝插入的原因。如果这对您没有帮助,请将结果发布到此处。我现在得到键1的“存在错误复制项”这听起来像是您试图复制表中的某些数据。其中已经存在设置为主键的内容。例如,如果您