我正在处理一个项目(PHP / MySQL),但我在验证来自数据时遇到了问题。 当我提交信息时,我没有在我的数据库(PHPmyadmin)中找到它。我认为这是一个连接问题! 这是我的表格:
<form id="formulaire" role="form" action="index.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" >
<input type="reset" value="Vider es champs" id="Reservoirbtn" class="btn btn-danger" style="margin-left:25px;">
</div>
</form>
这是我在我的数据库中插入新记录的PHP代码:
<?php
$connect=mysqli_connect("localhost","root","","mydb");
if (mysqli_connect_errno()) {
echo ("Échec de la connexion : %s\n" . mysqli_connect_error());
exit();
}
if(isset($_POST['submit'])){
$query = " INSERT INTO 'reservoir' (Code_reservoir, nom_reservoir, adress_reservoir, latitude, longtitude, capacite, cote_radial, espace, pseudo_espace, alimentation)
VALUES ('$_POST[coderes]', '$_POST[nomres]', '$_POST[adressres]', '$_POST[latitude]', '$_POST[longitude]', '$_POST[capaciteres]', '$_POST[coteradres]', '$_POST[typeres]','$_POST[pseutype]', '$_POST[alimentationres]')";
mysql_query($query,$connect);
mysql_close($connect);
}
?>
请帮助:)谢谢
答案 0 :(得分:0)
我认为错误在于您没有选择数据库。
试试这个:
<?php
$connect=mysqli_connect("localhost","root","","mydb");
if (mysqli_connect_errno()) {
echo ("Échec de la connexion : %s\n" . mysqli_connect_error());
exit();
}
$db_selected = mysqli_select_db("//database name", $connect);
if (!$db_selected)
{
die ("Can\'t use this databse : " . mysqli_error());
}
if(isset($_POST['submit'])){
$query = " INSERT INTO 'reservoir' (Code_reservoir, nom_reservoir, adress_reservoir, latitude, longtitude, capacite, cote_radial, espace, pseudo_espace, alimentation)
VALUES ('$_POST[coderes]', '$_POST[nomres]', '$_POST[adressres]', '$_POST[latitude]', '$_POST[longitude]', '$_POST[capaciteres]', '$_POST[coteradres]', '$_POST[typeres]','$_POST[pseutype]', '$_POST[alimentationres]')";
mysql_query($query,$connect);
mysql_close($connect);
}
?>
可以从以下链接找到更多帮助:mysqli_select_db
我不确定,但希望这会奏效。
答案 1 :(得分:0)
下面显示了用于将多个值从html表单传递到数据库的最简单方法之一。但是你需要关心像......
这样的事情1.必须使用输入类型提交的名称:您可以使用此
替换表单中的提交行<input type="submit" value="Valider" id="Reservoirbtn" class="btn btn-success" name="submit_form">
2.您需要使用与html表单输入名称中使用的名称相同的数据库表字段名称。即,表列名称必须是纬度,经度,代码等...
如果您认为您的代码只有连接问题。您可以使用以下代码中的连接语句,显示相应的连接错误(第1-7行)
php code
<?php
define(“DBHOST”,”localhost”);
define(“DBUNAME”,”root”);
define(“DBPASSWORD”,””);
define(“DBNAME”,”mydb”);
mysql_connect(DBHOST,DBUNAME,DBPASSWORD)or die(‘cant connect to DB’);
mysql_select_db(DBNAME)or die(“cant select db”);
$inputs = array();
$skip_input=array(‘submit_form’);
$error =array();
if(isset($_POST['submit_form']))
{
foreach($_POST as $key=>$value)
{
if(!in_array($key,$skip_input))
{
$inputs[$key] =$value;
}
}
}
$fieldValues = implode(“‘,’”,array_values($inputs));
$table = ‘reservoir’;
$sql=”INSERT INTO $table VALUES (‘$fieldValues’)”;
mysql_query($sql);
echo “inserted successfully”;
?>