表单输入不会显示在mysql中

时间:2015-09-15 21:21:00

标签: php mysql forms post input

当我提交以下代码时,我的数据库中唯一的东西是第二个选择部分(49/43)。无论我提交什么输入,其他输入都会写入值为“0”的数据库。我会非常感谢一些帮助!

<?php 
if ( ! empty( $_POST ) ) {

    $mysqli = new mysqli( 'localhost', 'XXX', 'XXX', 'XXX' );

    if ( $mysqli->connect_error ) {
        die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
    }

    $sql = "INSERT INTO user ( sex, pre, nmbr) VALUES ( '{$mysqli->real_escape_string($_POST['sex'])}', '{$mysqli->real_escape_string($_POST['pre'])}', '{$mysqli->real_escape_string($_POST['nmbr'])}' )";
    $insert = $mysqli->query($sql);

    if ( $insert ) {
        echo "Sucess!";
    } else {
        die("Error: {$mysqli->errno} : {$mysqli_error}");
    }

    $mysqli->close();
}

&GT;

<form method="post" action="">
        <select name="sex">
            <option>W</option>
            <option>M</option>
        </select>
        <select name="pre">
          <option>+49</option>
          <option>+43</option>
         </select>

        <input name"nmbr" type="text" placeholder=" Deine Handynummer hier eingeben">
        <button> <i class="fa fa-arrow-right"></i></button>
    </form>

1 个答案:

答案 0 :(得分:1)

在评论部分的帮助下,现在效果很好。

附加信息:数据库中的列类型为“sex”和“pre”的VARCHAR以及“nmbr”输入的INT。评论部分的另一个旁注:“因为你显然在同一个文件中使用了所有东西,你需要对所有的POST数组使用isset()和/或!empty()。”

<?php 
if ( ! empty( $_POST ) ) {



    $mysqli = new mysqli( 'localhost', 'XXX', 'XXX', 'XXX' );

    if ( $mysqli->connect_error ) {
        die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
    }

    $sql = "INSERT INTO user ( sex, pre, nmbr) VALUES ( '{$mysqli->real_escape_string($_POST['sex'])}', '{$mysqli->real_escape_string($_POST['pre'])}', '{$mysqli->real_escape_string($_POST['nmbr'])}' )";
    $insert = $mysqli->query($sql);

    if ( $insert ) {
        echo "Success!";

    } else {
        die("Error: {$mysqli->errno} : {$mysqli_error}");
    }

    $mysqli->close();
}

&GT;

<form method="post" action="">
        <select name="sex">
            <option>W</option>
            <option>M</option>
        </select>
        <select name="pre">
          <option>+49</option>
          <option>+43</option>
         </select>

        <input name="nmbr" type="text" placeholder=" Deine Handynummer hier eingeben">
        <button> <i class="fa fa-arrow-right"></i></button>
    </form>