【问题标题】:Form submited with php stores 0s表单提交与 php 存储 0s
【发布时间】:2013-10-04 14:39:25
【问题描述】:

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

【问题讨论】:

  • 检查列的数据类型是否设置为int或varchar?
  • var_dump($_POST, $_POST['gradeone']);
  • mysql_* 函数已被弃用,因此会出现意外行为。使用mysqli_* 函数。
  • @TimValishin 你对$_POST['subject']有任何价值吗?
  • 尝试在选择标签中给出值..

标签: php html mysql css


【解决方案1】:
'id' => $_POST['id'],
    'subject' => $_POST['subject'],
    'gradeone' => $_POST['gradeone'], 
    'gradetwo' => $_POST['gradetwo'], 
    'gradethree' => $_POST['gradethree']
);

这样尝试你不会添加引号

【讨论】:

  • 放置所有选项标签的地方 将您的值放在“math”中。
  • 主题部分工作..它不发送到数据库的等级数字输入
【解决方案2】:

您缺少引号,请像这样更改它

 $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

【讨论】:

  • echo "&lt;pre&gt;"; print_r($_POST); 为您提供了什么......在您的问题中包含该输出
【解决方案3】:

嗯,让我困惑的是这部分

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

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

【讨论】:

  • 整个想法是输入现有学生的成绩。如果学生不存在,则不会输入成绩。所以该语句只是检查 id 是否存在于另一个表中
猜你喜欢
  • 2015-08-07
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
相关资源
最近更新 更多