当我们从mysql数据库获取数据时获取错误是多么正确

时间:2016-10-03 14:51:48

标签: php html mysql database search

我将这个submit.php用于将学生数据提交到数据库

这是代码

<html>
    <head>
        <title>Submit Student DATA </title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>

        <div id="main">
            <h1>Submit Student Data for Verify </h1>
            <div id="login">
                <h2>Student's Form</h2>
                <hr/>
                <form action="" method="post">
                    <label>Student Name  :</label>
                    <input type="text" name="stu_name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
                    <label>Father Name  :</label>
                    <input type="text" name="stu_fathername" id="name" required="required" placeholder="Please Father Name"/><br /><br />
                    <label>Phone Number  :</label>
                    <input type="text" name="stu_phonenumber" id="name" required="required" placeholder="+92000000"/><br /><br />
                    <label>Address  :</label>
                    <input type="text" name="stu_address" id="name" required="required" placeholder="Pakistan, Punjab 0000"/><br /><br />
                    <label>Course Name  :</label>
                    <input type="text" name="stu_course" id="name" required="required" placeholder="Nebosh saftey"/><br /><br />
                    <label>Certificate Number  :</label>
                    <input type="text" name="stu_certificatenumber" id="name" required="required" placeholder="Enter Number"/><br /><br />
                    <label>Registration Number  :</label>
                    <input type="text" name="stu_registrationnumber" id="name" required="required" placeholder="Enter Number"/><br /><br />
                    <label>Student Email  :</label>
                    <input type="email" name="stu_email" id="email" required="required"  placeholder="john123@gmail.com"/><br/><br />
                    <label>Student City  :</label>
                    <input type="text" name="stu_city" id="city" required="required"  placeholder="Lahore"/><br/><br />
                    <input type="submit" value=" Submit " name="submit"/><br />
                </form>
            </div>


        </div>
        <?php
        if (isset($_POST["submit"])) {
            $servername = "localhost";
            $username = "root";
            $password = "root";
            $dbname = "college";

// Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }

            $sql = "INSERT INTO students (student_name, father_name, phone_number, student_address, student_course, student_certificatenumber, student_reg, student_email, student_city)
VALUES ('" . $_POST["stu_name"] . "','" . $_POST["stu_fathername"] . "','" . $_POST["stu_phonenumber"] . "','" . $_POST["stu_address"] . "','" . $_POST["stu_course"] . "','" . $_POST["stu_certificatenumber"] . "','" . $_POST["stu_registrationnumber"] . "','" . $_POST["stu_email"] . "','" . $_POST["stu_city"] . "')";

            if ($conn->query($sql) === TRUE) {
                echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
            } else {
                echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error . "');</script>";
            }

            $conn->close();
        }
        ?>
    </body>
</html>

我想在1个文本框中拨打学生注册号码的搜索框。当我把学生注册号码点击搜索然后我从mysql获取学生数据

这里的搜索代码可能是正确的,也可能是因为我没有

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
$hostname_php_result_conn = "localhost";
$database_php_result_conn = "college";
$username_php_result_conn = "root";
$password_php_result_conn = "root";   
$php_result_conn = mysql_connect($hostname_php_result_conn, $username_php_result_conn, $password_php_result_conn);
mysql_select_db($database_php_result_conn,$php_result_conn);

if(isset($_POST['submit'])) {
   $student_reg = $_POST['stu_registrationnumber'];

   $sql1 = mysql_query("SELECT students FROM student_reg WHERE = $stu_registrationnumber");
   $row1 = mysql_num_rows($sql1);

   if($row1 == 0) {
      echo 'Error, ID does not exist';
   } else {
      header('Location: show_result.php?student_reg='.$stu_registrationnumber);
   }   
}
?>

<html>
<head>
<title>Search</title>
</head>
<body>

<form action="search.php" method="post">
<input name="student_reg" type="text" id="stu_registrationnumber" />
<input type="submit" name="submit" value="search" />
</form>
</body>
</html>

这是show_result.php页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
$hostname_php_result_conn = "localhost";
$database_php_result_conn = "college";
$username_php_result_conn = "root";
$password_php_result_conn = "root";   
$php_result_conn = mysql_connect($hostname_php_result_conn, $username_php_result_conn, $password_php_result_conn);
mysql_select_db($database_php_result_conn,$php_result_conn);

$stu_reg = $_GET['stu_registrationnumber'];

$sql1 = mysql_query("SELECT * FROM students WHERE stu_registrationnumber = $stu_registrationnumber");
$row1 = mysql_fetch_array($sql1);
$student_name = $row1['student_name'];
$phone_number = $row1['phone_number'];
$student_address = $row1['student_address'];
$student_course = $row1['student_course'];
$student_certificatenumber = $row1['student_certificatenumber'];
$stu_registrationnumber = $row1['stu_registrationnumber'];
$student_email = $row1['student_email'];
$student_city = $row1['student_city'];
?>

<html>
<head>
<title>Show Results</title>
</head>
<body>

<?php
echo 'Student Name = '.$student_name.'<br />
Father Name = '.$father_name;
Phone Number = '.$phone_number;
Address = '.$student_address;
Course = '.$student_course;
Certificate Number = '.$student_certificatenumber;
Registration Number = '.$stu_registrationnumber;
E-Mail = '.$student_email;
City = '.$student_city;
?>

</body>
</html>

但我收到错误,ID不存在,但在数据库中我们已经注册号码但是收到错误仍然是错误

2 个答案:

答案 0 :(得分:0)

您的帖子值错误。

变化:

<input type="text" name="stu_registrationnumber" />

<input type="text" name="student_reg" />

答案 1 :(得分:0)

如果您的学生注册输入是&#34; stu_registrationnumber&#34;但是你正在寻找&#34; student_reg&#34;在display.php。

更改

$student_reg = $_POST['student_reg'];

$student_reg = $_POST['stu_registrationnumber']