2013-11-26 35 views
-1

我不會聲稱完全知道我在做什麼,因爲我不這樣做,那可能很明顯。我試圖保持窗體和函數更新數據庫在同一個文件中。我究竟做錯了什麼?HTML表單和PHP函數將表單數據保存到同一文件/頁面中的mysql

這裏是重要的位:

HTML:

<form id="billing" action="?updatebilling" method="post"> 

PHP:

<!-- Function to update billing --> 
<?php 
function updatebilling() { 
    // Connecting to the MySQL server 
    $host="localhost"; 
    $user_name="root"; 
    $pwd="bluebox"; 
    $database_name="rewired"; 
    $db=mysql_connect($host, $user_name, $pwd) or die(mysql_error()); 
    if (mysql_error() > "") print mysql_error() . "<br>"; 
    mysql_select_db($database_name, $db); 
    if (mysql_error() > "") print mysql_error() . "<br>"; 

    // Static info - account number 
    $account_id=users::getAttr('Account', 'account_id'); 

    // Storing form values into PHP variables 
    $zip = mysql_real_escape_string($_POST['billingzip']); 
    $name = mysql_real_escape_string($_POST['name']); 


    // Inserting variables into database 
    $sql = "INSERT INTO `web_signup` 
     SET `account_id` = '{$account_id}', 
    `zip` = '{$billingzip}', 
    `cardholder_name` = '{$name}', 
    `updated_at` = NOW()"; 

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

    mysql_close($db); 

} ?> 
+0

您收到了哪些錯誤? –

+0

應該保持您的數據庫連接在一個單獨的安全頁面上。此外,您不能將其用作表單操作。應該是'

'method ='post'>'。如果你是新手,使用'new mysqli()'和Object Oriented PHP。這將節省很多擊鍵! – PHPglue

+0

那麼你怎麼實際調用這個函數呢?你有什麼具體問題? –

回答

0

<form>標籤是奇數。

<form id="billing" action="?updatebilling" method="post"> 

這將重新加載相同的頁面,但?updatebilling操作沒有意義。您需要檢查頁面頂部提交的內容並採取相應措施。許多人檢查submit按鈕,如果他們找到它,則轉到處理代碼。

if (!isset($_POST['submit'])) { 
    // echo form here 
} else { 
    // process submitted data here. 
} 

你那麼action可以留空或設置爲指向頁面的文件名與action='<?php echo $_SERVER['PHP_SELF']; ?>'

+0

@PHPglue糟糕!固定。 – 2013-11-26 23:57:27

0

我想答案可能是一個有點明顯這裏。提交後你是否在調用函數?從我在代碼中可以看到的你已經定義了一個HTML表單,並且你的代碼定義了一個將數據保存到數據庫的函數,但是你是否正在執行該函數?例如:

<?php 
// function to save billing info 
function updatebilling() { ... } 

if (isset($_POST)) { // only call function when a post request is being processed 
    updatebilling(); // execute function to save the billing info 
} 
?> 
<form action="?updatebilling" method="post"> 
    <!-- your inputs here --> 
    <input type="submit"/> 
</form> 
相關問題