我尝试使用PHP在sql DB上插入数据,但它无法正常工作。告诉我这里有什么问题
此处的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中完全删除它们。请改用mysqli
或pdo
。 And this is why you shouldn't use mysql_*
functions
答案 1 :(得分:0)
另外如果你使用mysql_而不是mysqli_ write
if(mysql_query($billing_data_insert))
不
if(mysql_query($billing_data_insert, $billing_db_connection)){