Closed. This question is
off-topic。它当前不接受答案。
想改善这个问题吗?
Update the question,所以它是
on-topic,用于堆栈溢出。
3年前关闭。
我尝试使用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);
}
首先,您没有引用字符串。您的查询应如下所示:
$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);
这是参考:
http://php.net/manual/en/function.mysql-query.php
因此,您的代码应如下所示:
// 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。
我是一名优秀的程序员,十分优秀!