-1

我正在尝试提交由用户完成的表单条目,但是当输入数字时,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 数据库中......

4

3 回答 3

1
'id' => $_POST['id'],
    'subject' => $_POST['subject'],
    'gradeone' => $_POST['gradeone'], 
    'gradetwo' => $_POST['gradetwo'], 
    'gradethree' => $_POST['gradethree']
);

像这样尝试你没有添加引号

于 2013-10-04T06:11:47.083 回答
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 方法传递给当前脚本的变量关联数组。参考

于 2013-10-04T06:11:09.193 回答
0

好吧,让我困惑的是这部分

$result = mysql_query("SELECT id FROM newstudent WHERE id = '$id'");

如果您只选择id,您如何获得$_POST['gradeone'], $_POST['gradetwo'],$_POST['gradethree'] , $_POST['subject']. 您没有选择所有这些列的任何其他查询。

于 2013-10-04T06:39:25.750 回答