2016-02-14 39 views
0

我被嘗試插入數據在SQL DB使用PHP,但它不工作。告訴我這裏有什麼問題SQL插入不工作在PHP

SQL代碼在這裏

CREATE TABLE `billing` (
    `user_id` int(10) UNSIGNED NOT NULL, 
    `student_name` varchar(9) NOT NULL, 
    `student_roll` varchar(9) NOT NULL, 
    `student_batch_ID` varchar(9) NOT NULL, 
    `students_course` varchar(9) NOT NULL, 
    `students_payble_ammount` varchar(9) NOT NULL, 
    `payment_recived_date` varchar(9) NOT NULL, 
    `ammount_recived_by` varchar(9) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

HTML和PHP代碼在這裏所有的

<form method="post" action="<?php $_PHP_SELF ?>" name="new-Billing-Info-entry-form" id="new_record_form" class="border-style-1" enctype="multipart/form-data"> 
    <label class="form-label-FOR-student-name border-style-1">Name:</label> 
    <input type="text" name="input_s_name" class="form-input-FOR-student-name border-style-1 form-control" placeholder="Enter Student Name"/> 
     <br/><br/> 
    <label class="form-label-FOR-student-name border-style-1">Roll:</label> 
    <input class="form-input-FOR-student-name border-style-1 form-control" type="text" name="input_s_roll" placeholder="Enter Roll Number"/> 
     <br/><br/> 
    <label class="form-label-FOR-student-name border-style-1">Batch:</label> 
    <input class="form-input-FOR-student-name border-style-1 form-control" type="text" name="input_s_batch" placeholder="Enter Batch Number"/> 
     <br/><br/> 
    <label class="form-label-FOR-student-name border-style-1">Course:</label> 
    <input name="input_s_course" type="text" class="form-input-FOR-student-name border-style-1 form-control" placeholder="Enter Course Name"/> 
     <br/><br/> 
    <label class="form-label-FOR-student-name border-style-1">Ammount:</label> 
    <input name="input_s_payble_ammount" type="text" class="form-input-FOR-student-name border-style-1 form-control" placeholder="Enter Payble Ammount"/> 
     <br/><br/> 
    <label class="form-label-FOR-student-name border-style-1">Date:</label> 
    <input id="input-date" class="form-input-FOR-student-name border-style-1 form-control" type="text" name="input_s_paid_date" placeholder="Select Billing Date"/> 
     <br/><br/> 
    <button type="submit" class="btn btn-primary save-btn-1" name="s_payment_info_save_btn">LOOKS GOOD, Save</button> 
</form> 

<?php if(isset($_POST['s_payment_info_save_btn'])!="") 
    { 
    $mysql_hostname = "localhost"; 
    $billing_phpmyadmin_uid = "root"; 
    $billing_phpmyadmin_pass = ""; 
    $billing_db_Name = "student_billing"; 

    $billing_db_connection = mysql_connect($mysql_hostname, $billing_phpmyadmin_uid, $billing_phpmyadmin_pass, $billing_db_Name); 

    $input_student_name = mysql_real_escape_string($_POST['input_s_name']); 
    $input_student_roll = mysql_real_escape_string($_POST['input_s_roll']); 
    $input_student_batch = mysql_real_escape_string($_POST['input_s_batch']); 
    $input_student_course = mysql_real_escape_string($_POST['input_s_course']); 
    $input_student_payble_ammount = mysql_real_escape_string($_POST['input_s_payble_ammount']); 
    $input_student_paid_date = mysql_real_escape_string($_POST['input_s_paid_date']); 



    if(!$billing_db_connection){ 
     echo "<script type='text/javascript'> $(window).load(function(){ $('#modal_billing_DB_connection_error').modal('show'); }); </script>"; 
     exit(); 
    }else{ 
     /* echo "<script type='text/javascript'> $(window).load(function(){ $('#modal_billing_DB_connection_success').modal('show'); }); </script>"; */ 
    } 

$billing_data_insert = "INSERT INTO `billing` (`student_name`, `student_roll`, `student_batch_id`, `students_course`, `students_payble_ammount`, `payment_recived_date`) VALUES ($input_student_Name, $input_student_roll, $input_student_batch, $input_student_course, $input_student_payble_ammount, $input_student_paid_date)"; 


    if(!$billing_data_insert){ 
     echo "<script type='text/javascript'> $(window).load(function(){ $('#modal_bill_successfully_generate_notification').modal('show'); }); </script>"; 
    }else{ 
     echo "<script type='text/javascript'> $(window).load(function(){ $('#modal_bill_error_generate_notification').modal('show'); }); </script>"; 
    } 


    echo "<br/>", $input_student_name, "<br/>", $input_student_roll, "<br/>", $input_student_batch, "<br/>", $input_student_course, "<br/>", $input_student_payble_ammount, "<br/>", $input_student_paid_date; 

    mysql_close($billing_db_connection); 

} 
+0

任何人都請回答 –

回答

1

首先,你沒有引用您的字符串。您的查詢應該是這樣的:

$billing_data_insert = "INSERT INTO `billing` (`student_name`, `student_roll`, `student_batch_id`, `students_course`, `students_payble_ammount`, `payment_recived_date`) VALUES ('$input_student_name', '$input_student_roll', '$input_student_batch', '$input_student_course', '$input_student_payble_ammount', '$input_student_paid_date')"; 

而第二,您沒有執行查詢。執行這樣的查詢:

mysql_query($billing_data_insert, $billing_db_connection); 

這裏的參考:

所以,你的代碼應該是這樣的:

// your code 

$billing_data_insert = "INSERT INTO `billing` (`student_name`, `student_roll`, `student_batch_id`, `students_course`, `students_payble_ammount`, `payment_recived_date`) VALUES ('$input_student_name', '$input_student_roll', '$input_student_batch', '$input_student_course', '$input_student_payble_ammount', '$input_student_paid_date')"; 
if(mysql_query($billing_data_insert, $billing_db_connection)){ 

    // success 

}else{ 

    // failure 

} 

旁註:唐不要用mysql_*函數,它們自PHP 5.5起棄用,並在PHP 7.0中完全刪除。改爲使用mysqlipdoAnd this is why you shouldn't use mysql_* functions

+0

它不工作。解決錯誤後請評論php代碼 –

+0

解決錯誤後請評論php代碼 –

+0

@GourabMazumder我已更新我的答案。並利用['mysql_error()'](http://php.net/manual/en/function.mysql-error.php)函數來調試MySQL錯誤。 –

0

此外,如果您使用mysql_不mysqli_寫

if(mysql_query($billing_data_insert)) 

if(mysql_query($billing_data_insert, $billing_db_connection)){