2012-07-31 19 views
-1

我設計了我的接口來提交數據到我的數據庫表,但我不確定如何去做這個這裏是我的一些代碼大部分是一樣的,所以一旦我知道如何要做到這一點,我可以複製它。WordPress提交數據到我的sql

<form method="post" action="input.php"> 
<?php wp_nonce_field('update-options'); ?> 

<table width="600"> 
<tr valign="top>"> 
    <th width="92" scope="row">Enter New Track</th> 
    <th width="92" scope="row">Enter New Position</th> 
    <th width="92" scope="row">Enter New Driver</th> 
    <th width="92" scope="row">Enter New Class</th> 
</tr> 
<tr valign="top>"> 
    <td><input type="text" name="track" /></td> 
    <td><input type="text" name="position" /></td> 
    <td><input type="text" name="driver" /></td> 
    <td><input type="text" name="class" /></td> 
</tr> 
<tr valign="top"> 
    <td><input type="submit" value="Submit" /></td> 
    <td><input type="submit" value="Submit" /></td> 
    <td><input type="submit" value="Submit" /></td> 
    <td><input type="submit" value="Submit" /></td> 
</tr> 
</table> 

我想要做的就是輸入每個上有自己的這些值有表我都瞪大眼睛的問題,並沒有發現任何東西,這是任何幫助任何想法是值得讚賞的。

+0

爲什麼你有多個'submit'' '標籤? – Matt 2012-07-31 17:24:30

+0

由於每個輸入都不需要填寫,但我只想一次發送一個,並且他們都會轉到不同的表 – bobthemac 2012-07-31 17:28:38

+1

好的,這不是必需的,因爲任何單個'submit'輸入都會提交表單,發送*全部*從表單輸入數據到處理腳本。 – Matt 2012-07-31 17:32:59

回答

1

這裏有一些問題需要解決。

首先,您只需要一個<input>submit類型。這可以放在桌子外面。其次,您的valign屬性包含額外的>字符。

<table width="600"> 
    <tr valign="top"> 
     <th width="92" scope="row">Enter New Track</th> 
     <th width="92" scope="row">Enter New Position</th> 
     <thwidth="92" scope="row">Enter New Driver</th> 
     <th width="92" scope="row">Enter New Class</th> 
    </tr> 
    <tr valign="top"> 
     <td><input type="text" name="track" /></td> 
     <td><input type="text" name="position" /></td> 
     <td><input type="text" name="driver" /></td> 
     <td><input type="text" name="class" /></td> 
    </tr> 
</table> 
<br /> 
<input type="submit" value="Submit" /> 

上面的代碼應該在<form>標籤內。

一旦提交表單,數據將POST版到input.php,意圖這是處理數據和插入任何新的數據到表中(你爲什麼做這樣仍然是個謎,但我會先回答這個問題)。

input.php應該是這個樣子:

<?php 
    // I will ignore error handling here, but if you want a good tutorial on PHP PDOs, try http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html 
    $pdoObj = new PDO("mysql:host=[yourhost];dbname=[yourdbname]", [db_username], [db_password]); 

    // keep in mind that this code is WAY OPEN to SQL injection attacks, but this will at least give you an idea of how to get your code to function, if not securely. 
    if(isset($_POST['track'])) { 
     $pdoObj->exec("INSERT INTO tracks (name) VALUE ('" . $_POST['track'] . "')"); 
    } 
    if(isset($_POST['position'])) { 
     $pdoObj->exec("INSERT INTO positions (name) VALUE ('" . $_POST['position'] . "')"); 
    } 
    if(isset($_POST['driver'])) { 
     $pdoObj->exec("INSERT INTO drivers (name) VALUE ('" . $_POST['driver'] . "')"); 
    } 
    if(isset($_POST['class'])) { 
     $pdoObj->exec("INSERT INTO classes (name) VALUE ('" . $_POST['class'] . "')"); 
    } 

    // some code here that will either display some copy or redirect the user to another page 

注:

看起來好像你還是新的網絡開發,所以我會離開你這個:SQL Injection不是玩笑。你* 必須*爲它辯護,否則你可能會失去你的數據庫中的一切。

拿起一些關於PHP,Wordpress和Web開發的書籍或教程。與社區中的人討論最佳實踐,並嘗試獲得更多關於如何執行您感覺自己滿意的基本任務的指導。然後轉向更復雜的場景來測試您的能力。有了足夠的經驗,你會驚訝於你可以讓你的web應用程序做什麼。

+0

我還應該注意,使用'

'來顯示錶單輸入並不是一般公認的「最佳實踐」。使用'