我正在尝试提交用户完成的表单条目,但是在输入数字时,将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时,它将存储到mysql数据库中为0 ...
答案 0 :(得分:1)
'id' => $_POST['id'],
'subject' => $_POST['subject'],
'gradeone' => $_POST['gradeone'],
'gradetwo' => $_POST['gradetwo'],
'gradethree' => $_POST['gradethree']
);
尝试这样你就不会添加引号
答案 1 :(得分:0)
你错过了引号改变它
$data = array(
'id' => $_POST['id'],
'subject' => $_POST['subject'],
'gradeone' => $_POST['gradeone'], //changed added quotes
'gradetwo' => $_POST['gradetwo'], //changed added quotes
'gradethree' => $_POST['gradethree'], //changed added quotes
);
更改此行
$values = implode(', ', array_values($data));
像
$values = implode("', '", array_values($data));
$values = "'".$values."'";
$ _ POST是通过HTTP POST方法传递给当前脚本的关联变量数组。 Reference
答案 2 :(得分:0)
嗯,令我困惑的是这部分
$result = mysql_query("SELECT id FROM newstudent WHERE id = '$id'");
如果您只选择id
,您如何获得$_POST['gradeone'], $_POST['gradetwo'],$_POST['gradethree'] , $_POST['subject']
的值。您没有选择所有这些列的任何其他查询。