2014-02-20 36 views
0

爲什麼我無法插入到數據庫中?我的代碼有什麼問題?無法將行插入到數據庫中

<form action = "" method ="POST">           
    <center>            
     <b>Name</b><br><br>Quantity: <input type = "text" name = "name" style = "width: 155px"><br><br>           
     <b>Contact Number</b><br><br>Quantity: <input type = "text" name = "contact" style = "width: 155px" ><br><br>           
     <b>Address</b><br><br>Quantity: <input type = "text" name = "address" style = "width: 155px"><br><br> 
     <b>Spoon N1(₱25000.00)</b><br><br>Quantity: <input type = "text" name = "Squantity" style = "width: 155px" value = "0"><br><br> 
     <b>Tanabe Hypermedallion(₱15000.00)</b><br><br>Quantity: <input type = "text" name = "Tquantity" style = "width: 155px" value = "0"><br><br> 
     <b>Fujitsubo Legalis R(₱15000.00)</b><br><br>Quantity: <input type = "text" name = "Fquantity" style = "width: 155px" value = "0"><br><br> 
     <b>GCash Transaction No.</b><br>:  
     <input type = "text" name = "quantity" style = "width: 155px"><br><br> 
     <input type = "submit" value = "submit"> 
    </center> 
</form> 

<?php 
if(isset($_POST['submit'])) 
{ 
    $name = empty($_POST['name']) ? die ("Input a name"): mysql_escape_string($_POST['name']); 
    $contact = empty($_POST['contact']) ? die ("Input a contact number"): mysql_escape_string($_POST['contact']); 
    $address = empty($_POST['address']) ? die ("Input a address"): mysql_escape_string($_POST['address']); 
    $spoon = empty($_POST['Squantity']) ? die ("Input a value"): mysql_escape_string($_POST['Squantity']); 
    $tanabe = empty($_POST['Tquantity']) ? die ("Input a value"): mysql_escape_string($_POST['Tquantity']); 
    $fujitsubo =empty($_POST['Fquantity']) ? die ("Input a value"): mysql_escape_string($_POST['Fquantity']); 
    $total = ($spoon * 25000) + ($tanabe * 15000) + ($fujitsubo * 15000); 
    $host = "localhost"; 
    $user = "root"; 
    $pass = "password"; 
    $db = "eurocare"; 
    $con = mysql_connect($host,$user,$pass,$db) or die ("Unable to connect"); 
    $conn = mysql_select_db($db,$con); 
    $query = "INSERT INTO orders(name, contact, address, spoon, tanabe, fujitsubo) VALUES ('$name','$contact','$address','$spoon','$tanabe','$fujitsubo','$total')"; 
    $result = mysql_query($query,$con) or die("Error in Query : $query ." .mysql_error()); 
    exit; 
    mysql_close($con); 
} 
+1

'mysql_error()'給了什麼? – TN888

+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**粉紅色框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – h2ooooooo

+1

'mysql_escape_string()'已過時。實際上,所有的'mysql_ *'函數都是。 –

回答

0

你的提交按鈕即。 HTML輸入元素<input type="submit" ... ...>必須具有"name"屬性才能包含在$_POST陣列中。

<input type = "submit" value = "submit" name="submit"> 

沒有它if(isset($_POST['submit']))永遠不會解決爲true。

2

使用mysql_connect已被棄用,使用mysqli代替。

我看到你基本上要插入7元,但只有六宣佈...

INSERT INTO orders(name, contact, address, spoon, tanabe, fujitsubo) <-- @@[email protected]@ VALUES ('$name','$contact','$address','$spoon','$tanabe','$fujitsubo','$total') <-- @@[email protected]@ 
相關問題