簡體   English   中英

將各種下拉列表中的值插入數據庫表

[英]insert values from various drop down list to database table

我是PHP新手。 我沒有面向對象PHP的經驗。 我只有Raw PHP的經驗。

我試圖將下拉框的值插入數據庫。 這是我在這里給出的代碼。

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php

        if (isset($_POST['submit']))
        {
            $roll=$_POST['roll'];
            $first_exam=$_POST['first_exam'];
            $second_exam=$_POST['second_exam'];
            $third_exam=$_POST['third_exam'];
            $fourth_exam=$_POST['fourth_exam'];
            $fifth_exam=$_POST['fifth_exam'];
            include 'db.php';

            $sql= "INSERT * INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
                            VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)" 
                                    or die (mysql_error());
            $result=  mysql_query($sql);
            if ($result){
                echo 'ok doen';

            }
            else {
                echo 'dont';
            }
        }

        ?>
    </body>



    <form method="post">
        <tr>
            <td>Roll:</td>
            <td><input type="text" name="roll" /></td>
         </tr>
         <br></br>
    </form>


          <form method="post">      
          <?php echo "first_exam";?>
          <select name="first_exam">        
          <option value="math">Mathematics</option>
          <option  value="phy">Physics</option>
          <option  value="chem">Chemistry</option>
          <option  value="eng">English</option>
          <option  value="bio">Biology</option>
           </select>
       </form>
         <br></br>

      <form method="post">   
          <?php echo "second_exam";?> 
          <select name="second_exam">        
          <option  value="math">Mathematics</option>
          <option  value="phy">Physics</option>
          <option  value="chem">Chemistry</option>
          <option  value="eng">English</option>
          <option  value="bio">Biology</option>
           </select>
       </form>
         <br></br>

         <form method="post">   
          <?php echo "third_exam";?> 
          <select name="third_exam">        
          <option  value="math">Mathematics</option>
          <option  value="phy">Physics</option>
          <option  value="chem">Chemistry</option>
          <option  value="eng">English</option>
          <option  value="bio">Biology</option>
           </select>
       </form>
         <br></br>

         <form method="post">   
          <?php echo "fourth_exam";?> 
          <select name="fourth_exam">        
          <option  value="math">Mathematics</option>
          <option value="phy">Physics</option>
          <option  value="chem">Chemistry</option>
          <option  value="eng">English</option>
          <option  value="bio">Biology</option>
           </select>
       </form>

         <br></br>

         <form method="post" action="">   
          <?php echo "fifth_exam";?> 
          <select name="fifth_exam">        
          <option  value="math">Mathematics</option>
          <option  value="phy">Physics</option>
          <option value="chem">Chemistry</option>
          <option value="eng">English</option>
          <option  value="bio">Biology</option>
           </select>
       </form>

         <br></br>
         <form method="post">
         <input type="submit" name="submit" value="submit">
         </form>
  <br><br>
</form>
</html>

我得到了$ _post ['roll'],$ _ post ['first_exam']這些錯誤。

那里只有一個簡單的錯誤。 取而代之的是,您制作了許多表格,只需制作一張表格並將所有選擇添加到其中...

像這樣:

<form method="post"> code here </form>

您的代碼中有很多錯誤,這是一個完整的工作示例:

HTMl和PHP:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Divix Help</title>
</head>
<body>
    <?php

    if (isset($_POST['submit']))
    {
        $roll=$_POST['roll'];
        $first_exam=$_POST['first_exam'];
        $second_exam=$_POST['second_exam'];
        $third_exam=$_POST['third_exam'];
        $fourth_exam=$_POST['fourth_exam'];
        $fifth_exam=$_POST['fifth_exam'];
        include 'db.php';

        $sql= "
            INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th)
            VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)
        ";
        //echo $sql;
        $result = mysql_query($sql);
        if ($result){
            echo 'ok doen';
        } else {
            echo 'dont';
        }
    }

    ?>

    <form method="post">
        <table>
            <tr>
                <td>Roll:</td>
                <td><input type="text" name="roll" /></td>
            </tr>
        </table>
        <br></br>

        <?php echo "first_exam";?>
        <select name="first_exam">
            <option value="math">Mathematics</option>
            <option  value="phy">Physics</option>
            <option  value="chem">Chemistry</option>
            <option  value="eng">English</option>
            <option  value="bio">Biology</option>
        </select>
         <br></br>

        <?php echo "second_exam";?>
        <select name="second_exam">
            <option  value="math">Mathematics</option>
            <option  value="phy">Physics</option>
            <option  value="chem">Chemistry</option>
            <option  value="eng">English</option>
            <option  value="bio">Biology</option>
        </select>
         <br></br>

        <?php echo "third_exam";?>
        <select name="third_exam">
            <option  value="math">Mathematics</option>
            <option  value="phy">Physics</option>
            <option  value="chem">Chemistry</option>
            <option  value="eng">English</option>
            <option  value="bio">Biology</option>
        </select>
         <br></br>

        <?php echo "fourth_exam";?>
        <select name="fourth_exam">
            <option  value="math">Mathematics</option>
            <option value="phy">Physics</option>
            <option  value="chem">Chemistry</option>
            <option  value="eng">English</option>
            <option  value="bio">Biology</option>
        </select>
         <br></br>

        <?php echo "fifth_exam";?>
        <select name="fifth_exam">
            <option  value="math">Mathematics</option>
            <option  value="phy">Physics</option>
            <option value="chem">Chemistry</option>
            <option value="eng">English</option>
            <option  value="bio">Biology</option>
        </select>

        <br></br>
        <input type="submit" name="submit" value="submit" />
    </form>

</body>
</html>
  • 正如其他人提到的,您有太多<form>標記,請記住,您只需要1個form標記即可通過POSTGET方法發送多個字段。
  • 您在SQL中有錯誤(請勿將INSERT * INTO放進去, 號僅用於SELECT語句)
  • 損壞的HTML結構,例如缺少</body></html>標簽
  • 最后但並非最不重要的是,它or die (mysql_error()); 你把它針對MySQL的executeconnect功能,而不是針對字符串。

嘗試這個。 填寫1張表格。

也改變這個。

$sql= "INSERT INTO roll (Roll,1st,2nd,3rd,4th,5th) VALUES ($roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";

$result=  mysql_query($sql) or die (mysql_error());

這也是一個問題。 使用or die (mysql_error()); 在mysql函數中,而不是在聲明中。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
<form method="post">
<table>
        <tr>
            <th>Roll:</th>
            <th>First Exam</th>
            <th>Second Exam</th>
            <th>Third Exam</th>
            <th>Fourth Exam</th>
            <th>Fifth Exam</th>
            <th>Action</th>
        </tr>
    <tr>
      <td><input type="text" name="roll" /></td>
            <td>
                <select name="first_exam">        
                    <option value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
            <td>
                <select name="second_exam">        
                    <option  value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
            <td>
                <select name="third_exam">        
                    <option  value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
            <td>
                <select name="fourth_exam">        
                    <option  value="math">Mathematics</option>
                    <option value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
            <td>
                <select name="fifth_exam">        
                    <option  value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option value="chem">Chemistry</option>
                    <option value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
            <td><input type='submit' name='submit' value='submit'></td>
    </tr>
</table>
</form>
<?php
if (isset($_POST['submit']))
{
    $roll=$_POST['roll'];
    $first_exam=$_POST['first_exam'];
    $second_exam=$_POST['second_exam'];
    $third_exam=$_POST['third_exam'];
    $fourth_exam=$_POST['fourth_exam'];
    $fifth_exam=$_POST['fifth_exam'];
    include 'db.php';

    $sql= "INSERT INTO roll (id,Roll,1st,2nd,3rd,4th,5th) VALUES (NULL,$roll,$first_exam,$second_exam,$third_exam,$fourth_exam,$fifth_exam)";
    $result=  mysql_query($sql) or die (mysql_error());
    if ($result){
        echo 'ok doen'; 
    }
    else {
        echo 'dont';
    }
}
?>
</body>
</html>

屏幕截圖 在此處輸入圖片說明

我想指出的幾件事。

  1. 您的include語句應始終位於php文檔的頂部,這是一種良好的編程習慣。
  2. 使用require_once而不是include,因為它可以防止文件被多次包含在php文檔中。
  3. 創建並使用Database類執行所有數據庫操作。
  4. 在插入之前,請始終使用real_escape_string函數清理數據,以防止SQL注入。
  5. 總是在最后關閉數據庫連接,這是一種好的編程習慣。
  6. 最后但並非最不重要的一點是,正確學習HTML和PHP。

這是完整的代碼,

<?php 

// instead of include, use require_once
require_once('db.php'); 

// create and use Database class for all your database operations
class Database{
    public static function open_connection($host, $username, $password, $dbName){
        return new mysqli($host, $username, $password, $dbName);
    }

    public static function check_connection(){
        if(!mysqli_connect_error()){
            return true;
        }else{
            return false;
        }
    }

    public static function execute_query($connection,$query){
        return $connection->query($query);
    }

    public static function close_connection($connection){
        $connection->close();
    }
}

?>

<?php

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

    $connection = Database::open_connection(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
    if(Database::check_connection()){
        // sanitize your input data
        $roll = $connection->real_escape_string($_POST['roll']);
        $fifth_exam = $connection->real_escape_string($_POST['first_exam']);
        $second_exam = $connection->real_escape_string($_POST['second_exam']);
        $third_exam = $connection->real_escape_string($_POST['third_exam']);
        $fourth_exam = $connection->real_escape_string($_POST['fourth_exam']);
        $fifth_exam = $connection->real_escape_string($_POST['fifth_exam']);

        // now insert into database
        // instead of taking id as NULL, take id as AUTO_INCREMENT 
        $query = "INSERT INTO table(roll_no, first_exam, second_exam, third_exam, fourth_exam, fifth_exam) VALUES ($roll, '$first_exam', '$second_exam', '$third_exam', '$fourth_exam', '$fifth_exam')";
        if(Database::execute_query($connection,$query)){
            echo "record is successfully inserted";
        }else{
            echo "error: record could not be inserted";
        }
    }else{
        echo "database connection failed";
    }
}

?>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Some Title</title>
</head>
<body>

<form action="test.php" method="post">
    <table>
        <tr>
            <td>Roll No.</td>
            <td><input type="text" name="roll" /></td>
        </tr>

        <tr>
            <td>First exam</td>
            <td>
                <select name="first_exam">        
                    <option value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>Second exam</td>
            <td>
                <select name="second_exam">        
                    <option value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>Third exam</td>
            <td>
                <select name="third_exam">        
                    <option value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>Fourth exam</td>
            <td>
                <select name="fourth_exam">        
                    <option value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
        </tr>

        <tr>
            <td>Fifth exam</td>
            <td>
                <select name="fifth_exam">        
                    <option value="math">Mathematics</option>
                    <option  value="phy">Physics</option>
                    <option  value="chem">Chemistry</option>
                    <option  value="eng">English</option>
                    <option  value="bio">Biology</option>
                </select>
            </td>
        </tr>

        <tr>
            <td></td>
            <td><input type="submit" name="submit" value="Submit" /></td>
        </tr>

    </table>
</form>
</body>
</html>
<?php
// it's a good programming practice to always close database connection at the end
if(isset($connection)){
    Database::close_connection($connection);
}

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM