我正在尝试提交由用户完成的表单条目,但是当输入数字时,0 被输入到数据库中,而不是用户输入的值。请帮忙
这是我的
<?php
//once the submit the button is submited it connects to the database
if(isset($_POST['submitGrades'])) {
include ("account.php") ;
( $dbh = mysql_connect ( $hostname, $username, $password ) )
or die ( "Unable to connect to MySQL database" );
mysql_select_db( $project );
// this part checks the value of "id" that it exist in another table first
$id = mysql_real_escape_string($_POST['id']);
$result = mysql_query("SELECT id FROM newstudent WHERE id = '$id'");
if (mysql_num_rows($result)) {
// Go ahead and insert everything in table1
$data = array(
'id' => $_POST['id'],
'subject' => $_POST['subject'],
'gradeone' => $_POST[gradeone], //this stores as 0 in the database
'gradetwo' => $_POST[gradetwo], //this stores as 0 in the database
'gradethree' => $_POST[gradethree], //this stores as 0 in the database
);
// Make sure all the data is safe for entry into the database
foreach ($data as $key => $val) {
$data[$key] = "'" . mysql_real_escape_string($val) . "'";
}
$fields = implode(', ', array_keys($data));
$values = implode(', ', array_values($data));
$result = mysql_query("INSERT INTO grades ($fields) VALUES ($values)");
echo 'Your grades were submited.';
} else {
echo 'The Student does not exist. Please Enter the student first.';
}}?>
这是HTML代码
<form id="grades" action="php/grades.php" method="post" OnSubmit="setTimeout('reset_form()', 200); return true" >
<label>STUDENT ID</label>
<input type="text" placeholder="PLEASE ENTER STUDENT ID" class="input" maxlength="9" name="id" id="id"></br>
<label>SUBJECT</label>
<select class="input" name="subject" id="subject">
<option>Select One</option>
<option>Math</option>
<option>Chemistry</option>
<option>English</option>
<option>Physics</option>
<option>French</option>
<option>Computer Science</option>
<option>Network</option>
</select></br>
<label>GRADE 1</label>
<input type = "text" placeholder="OUT OF 100" class="input" maxlength="3" name="gradeone" id="gradeone"></br>
<label>GRADE 2</label>
<input type = "text" placeholder="OUT OF 100" class="input" maxlength="3" name="gradetwo" id="gradetwo"></br>
<label>GRADE 3</label>
<input type = "text" placeholder="OUT OF 100" class="input" maxlength="3" name="gradethree" id="gradethree"></br>
<input type="submit" value="Submit" name="submitGrades" id="submitGrades"/> </form>
正如我之前所说,我需要帮助来弄清楚为什么当用户将值输入到 Gradeone、gradetwo、gradethree 时,它会以 0 的形式存储到 mysql 数据库中......