无法将行插入数据库

时间:2014-02-20 16:35:26

标签: php sql

为什么我无法插入数据库?我的代码出了什么问题?

<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);
}

2 个答案:

答案 0 :(得分:2)

弃用mysql_connect,请改用mysqli

我看到你基本上想要插入7个元素,但只声明了6个......

INSERT INTO orders(name, contact, address, spoon, tanabe, fujitsubo) <-- @@!!SIX!!@@ VALUES ('$name','$contact','$address','$spoon','$tanabe','$fujitsubo','$total') <-- @@!!SEVEN!!@@

答案 1 :(得分:0)

您的提交按钮即。 HTML输入元素<input type="submit" ... ...>必须将"name"属性包含在$_POST数组中。

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

没有它if(isset($_POST['submit']))永远不会解决为真。

相关问题