0

Why I can't insert into the database? What's wrong with my code?

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

2 Answers 2

2

Using mysql_connect is deprecated, use mysqli instead.

I see you basically want to insert 7 elements, but declared only six...

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

Your submit button ie. HTML input element <input type="submit" ... ...> has to have the "name" attribute to be included in the $_POST array.

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

Without it if(isset($_POST['submit'])) would never resolve to true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.