我正在尝试做一个将值插入数据库的表单,但是我总是收到ID_team,名称和img的错误消息:
Notice: Undefined variable: id_team in *** on line 12
所以说错误来自这一行:
$query1 = "INSERT INTO player (id, id_team, name, img) VALUES(NULL, $id_team, $name, $img)";
但是我的插入查询中没有看到任何错误。
这是我的php:
include('../../../connexion.php');
$connexion=connexionBd();
if(isset($_POST['id_team']) && isset($_POST['name'])&& isset($_POST['img'])&& !empty($_POST['id_team'])&& !empty($_POST['name'])&& !empty($_POST['img'])){
$id_team=$_POST['id_team'];
$name=$_POST['name'];
$img=$_POST['img'];
}
$query1 = "INSERT INTO player (id, id_team, name, img) VALUES(NULL, $id_team, $name, $img)";
$query3 = "SELECT * FROM team";
$resultat3=$connexion->query($query3);
$res3=$resultat3->fetchALL(PDO::FETCH_ASSOC);
这是我的表格:
<form method="post" action="add.php">
<fieldset>
<legend>Add a player</legend>
<p>
<label>Team</label>
<select name="id_team">
<?php foreach($res3 as $key => $value): ?>
<option value="<?=$value['id']?>"><?=$value['name']?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label>name</label>
<input type="text" name="name" />
</p>
<p>
<label>Image</label>
<input type="file" name="img" style="width:200px; font-size:10px;" />
</p>
<p>
<input type="submit" name="send" value="Envoyer" />
</p>
</fieldset>
</form>
答案 0 :(得分:0)
尝试这个:
$query1 = "INSERT INTO player (`id`, `id_team`, `name`, `img`) VALUES(NULL, '$id_team', '$name', '$img')";
答案 1 :(得分:0)
我收到此错误,是因为我忘记将查询放入if循环中,因此无法识别值。
if(isset($_POST['id_team']) && isset($_POST['name'])&& isset($_POST['img'])&& !empty($_POST['id_team'])&& !empty($_POST['name'])&& !empty($_POST['img'])){
$id_team=$_POST['id_team'];
$name=$_POST['name'];
$img=$_POST['img'];
$query1 = "INSERT INTO player (`id`, `id_team`, `name`, `img`) VALUES(NULL, '$id_team', '$name', '$img')";
}