0

guys i want to submit data from text boxes ect to my database .. i have kept a pop up box to say submitted ..into database .. and when i execute it says entered database but the database is empty..

<?php

include('airlineDB2.php');
$nameb1   = @$_POST['nameb1'];
$ageb1    = @$_POST['ageb1'];
$genderb1 = @$_POST['genderb1'];
$prefb1   = @$_POST['prefb1'];
$planeno  = @$_POST['planeno'];
$pdate    = @$_POST['pdate'];

if (isset($_POST['book'])) {
    //The data is entered into the database here between this

    $insert = mysql_query("Insert into ticketbook (planeno,nameb,ageb,genderb,preferenceb,date) VALUES('$planeno','$nameb1','$ageb1','$genderb1','$prefb1','$pdate')");
    echo '<script type="text/javascript">alert("Your ticket is booked check your email for further details!")</script>';
    //The data is entered into the database here between this
}

?>
Kavvson
  • 825
  • 3
  • 9
  • 23
lee
  • 9
  • 3
  • 1
    You never test the return value of `mysql_query` so you don't know if it was successful or not. You never look at `mysql_error()` so you never find out what is wrong. – Quentin Nov 09 '14 at 21:32
  • 2
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Nov 09 '14 at 21:33
  • 1
    I don't think the use of @ is a good sign (except maybe when logging, which you are not doing here). – Harry Pehkonen Nov 09 '14 at 21:38
  • sidenote: don't bandaid errors, correct way to handle that situation is handle `$_POST` values when you submitted the form – Kevin Nov 10 '14 at 00:57

1 Answers1

0

There is some error with your query, you can change the logic to this:

$insert = mysql_query("Insert into ticketbook (planeno,nameb,ageb,genderb,preferenceb,date) VALUES('$planeno','$nameb1','$ageb1','$genderb1','$prefb1','$pdate')");
if ( !$insert ) {
    die('Invalid query: ' . mysql_error());
} else if ( mysql_affected_rows()!=1 ) {
    die('Error inserting row');
} else {
    echo '<script type="text/javascript">alert("Your ticket is booked check your email for further details!")</script>';
};
skobaljic
  • 9,379
  • 1
  • 25
  • 51