将数据从表单导出到我的数据库(PHP / MySQL)

时间:2014-07-15 00:38:24

标签: php mysql html-form

我的网络应用有问题,当我填写表单并验证时,我的phpmyadmin页面中没有任何内容!连接很好,但没有数据导出。

这是我的PHP代码:

  <?php
      $connect=mysqli_connect("localhost","root","","ramsa");

      if (mysqli_connect_errno()) {
        echo ("Échec de la connexion : %s\n" . mysqli_connect_error());
        exit();
    }


$db_selected = mysqli_select_db($connect,"ramsa");

if (!$db_selected)
  {
  die ("Can\'t use this databse : " . mysqli_error());
  }


$query = " INSERT INTO 'reservoir' (CodeReservoir, NomReservoir, AdresseReservoir, Latitude, Longtitude, Capacite, CodeRadial, Type, PseudoType, Alimentation)
           VALUES ('$_POST[coderes]', '$_POST[nomres]', '$_POST[adressres]', '$_POST[latitude]', '$_POST[longitude]', '$_POST[capaciteres]', '$_POST[coteradres]', '$_POST[typeres]','$_POST[pseutype]', '$_POST[alimentationres]')";

echo "Resvoir bien ajouté.";
mysqli_query($connect,$query);
mysqli_close($connect);



?>

这是我的表单的HTML代码:

    <form id="formulaire" role="form" action="send.php" method="POST">
                    <h3 style="color:red;">Ajouter un reservoir</h3>
                    <label for="coordlat" style="margin-bottom:7px;">Latitude du point</label>
                    <input type="text" class="form-control" id="latitude" name="latitude" placeholder="Latitude"  style="margin-bottom:7px;" required>
                    <label for="coordlng" style="margin-bottom:7px;">Longitude de point</label>
                    <input type="text" class="form-control" id="longitude" name="longitude"  placeholder="Longitude" style="margin-bottom:7px;" required>
                    <label for="coordlng" style="margin-bottom:7px;">Code du reservoir</label>
                    <input type="text" class="form-control" id="coderes" name="coderes" placeholder="Code" style="margin-bottom:7px;" required>
                    <label for="coordlng" style="margin-bottom:7px;">Nom du reservoir</label>
                    <input type="text" class="form-control" id="nomres" name="nomres"  placeholder="Nom" style="margin-bottom:7px;" required>
                    <label for="coordlng" style="margin-bottom:7px;">Adress du reservoir</label>
                    <input type="text" class="form-control" id="adressres" name="adressres"  placeholder="Adress" style="margin-bottom:7px;" required>
                    <label for="coordlng" style="margin-bottom:7px;">Capacité du reservoir</label>
                    <input type="text" class="form-control" id="capaciteres" name="capaciteres"  placeholder="Capacité" style="margin-bottom:7px;" required>
                    <label for="coordlng" style="margin-bottom:7px;">Alimentation</label>
                    <input type="text" class="form-control" id="alimentationres" name="alimentationres"  placeholder="Alimentation" style="margin-bottom:7px;" required>
                    <label for="coordlng" style="margin-bottom:7px;">Cote radial du reservoir</label>
                    <input type="text" class="form-control" id="coteradres" name="coteradres"  placeholder="Cote radial" style="margin-bottom:7px;" required>
                    <div style="padding-top:10px;">
                        <select name="typeres" style="margin-bottom:7px;" required>
                                <option value="" disabled selected>Type du reservoir</option>
                                <option value="enterre">Enterré</option>
                                <option value="semi-enterre">Semi enterré</option>
                        </select>
                        <select name="pseutype" style="margin-bottom:7px;" required>
                                <option value="" disabled selected>Pseudo-type du reservoir</option>
                                <option value="onep">Reservoir ONEP</option>
                                <option value="ramsa">Reservoir RAMSA</option>
                                <option value="onep">Forage C</option>
                                <option value="onep">Forage RA</option>
                        </select>
                    </div>
                    <div style="padding-top:10px;">
                        <input type="submit" value="Valider" id="Reservoirbtn" class="btn btn-success" name="submit">
                        <input type="reset" value="Vider es champs" id="Reservoirbtn" class="btn btn-danger"   style="margin-left:25px;">
                    </div>
            </form>

1 个答案:

答案 0 :(得分:0)

首先,我要说你绝对不需要将$ _POST超级全局中的任何内容直接添加到类似的SQL查询中。您可以广泛接受SQL注入攻击。

话虽如此,您是否验证了列名和值类型?

我建议先在你的php中尝试这样的东西来识别错误

<?php
      $connect=mysqli_connect("localhost","root","","ramsa");

      if (mysqli_connect_errno()) {
        echo ("Échec de la connexion : %s\n" . mysqli_connect_error());
        exit();
    }


$db_selected = mysqli_select_db($connect,"ramsa");

if (!$db_selected)
  {
  die ("Can\'t use this databse : " . mysqli_error());
  }


$testData = Array(); //fill this with a false data set for each column of data

$testInput = "'".implode("','",$testData)."'";

$query = " INSERT INTO `reservoir` (`CodeReservoir`, `NomReservoir`, `AdresseReservoir`, `Latitude`, `Longtitude`, `Capacite`, `CodeRadial`, `Type`, `PseudoType`, `Alimentation`)
           VALUES ($testInput)";

echo $query."<br/>";  //so that you can run it directly in the server using PHPMyAdmin or MySQL Workbench or similar application
mysqli_query($connect,$query);
echo mysqli_error($connect);
mysqli_close($connect);
?>

显然,Insert因某些原因失败了。根据我的经验,这几乎总是由于字段名称不正确。如果没有看到您的字段列表,那么如果确实如此,则很难确定。无论如何,上面的调试代码应该可以帮助您确定错误的位置,以便您可以纠正错误。

编辑:注意到您将表名括在单引号中。这是无效的语法。您希望将它们包含在刻度`(或重音符号,即转义键下方的键)中。同时将所有字段名称括在刻度线中(我已经编辑了上面的代码示例以反映这些更改)。