我是编码方面的初学者,我一直在从事此类项目,在该项目中我想使用PHP将数据插入SQL服务器,所以我一直在努力的是要插入的部分数据,并且我想将选择标签对象放入变量中,所以我一直在获取未定义索引,因此,如果您能帮助我,将不胜感激。
HTML代码:
<form action="" method="POST">
<input type="text" class="puo" id="modeloPU" name="Nmod" style="width:
9.6%">
<select class="puo" id="familiaSPU" name="Nfam">
<option value="-1 "></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">ENFRIADORES</option>
<option value="2">FROSTER</option>
<option value="3">CONGELADORES</option>
<option value="4">MIRAGE</option></select>
<select class="puo puh" id="odpSPU" name="Nodp">
<option value="-1"></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">HORIZONTAL</option>
<option value="2">VERTICAL</option></select>
<select class="puo puh" id="tdcSPU" name="Ntdc">
<option value="-1"></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">CLIENTE PARTICULAR</option>
<option value="2">VALIDADOS</option>
<option value="3">DEFINIDOS</option>
<option value="4">ESTÁNDAR</option></select>
<select class="puo puh" id="clienteSPU" name="Ncliente">
<option value="-1"></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">CBC</option>
<option value="2">COCA-COLA</option>
<option value="3">FASA</option>
<option value="4">FLORIDA</option>
<option value="5">HOSHISAKI</option>
<option value="6">JALEEL</option></select>
<select class="puo puh" id="voltajeSPU" name="Nvoltaje">
<option value="-1"></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">110V(50Hz - 60Hz)</option>
<option value="2">140V(50Hz - 60Hz)</option>
<option value="3">220V(50Hz - 60Hz)</option></select>
<select class="puo puh" id="gradoSPU" name="Ngrado">
<option value="-1"></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">C°</option>
<option value="2">F°</option></select>
<select class="puo puh" id="termostatoSPU" name="Ntermostato">
<option value="-1"></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">CAREL</option>
<option value="2">COEL</option>
<option value="3">ELSTAT</option>
<option value="4">FULL GAUGE</option>
<option value="5">SOLLATEK</option>
<option value="6">WELLINGTON</option></select>
<select class="puo puh" id="tdpSPU" name="Ntdp">
<option value="-1"></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">PVP</option>
<option value="2">PVPH</option>
<option value="3">PS</option></select>
<select class="puo puh" id="paisSPU" name="Npais">
<option value="-1"></option>
<option value="0" style="font-style: italic; color:grey">indefinido</option>
<option value="1">USA</option>
<option value="2">Puerto Rico</option>
<option value="3">Latino America</option></select>
<input type="submit" class="Boton" name="submit" id="AddButton"
value="Agregar" style="box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px
50px 0 rgba(0,0,0,0.19); cursor: pointer;">
</form>
PHP代码:
<?php
include 'conn.php';
$myDate = date('Y/m/d');
$model = $_POST['Nmod'];
$fam = $_POST['Nfam'];
$orientacion = $_POST['Nodp'];
$tdcliente = $_POST['Ntdc'];
$cli = $_POST['Ncliente'];
$volt = $_POST['Nvoltaje'];
$grad = $_POST['Ngrado'];
$termo = $_POST['Ntermostato'];
$tdpuerta = $_POST['Ntdp'];
$pai = $_POST['Npais'];
$SQL = "INSERT INTO usuario (modelo ,familia ,odp ,tdc ,cliente ,voltaje ,
grado ,termostato, tdp, pais, fecha)
VALUES ('$model', '$fam',
'$orientacion', '$tdcliente', '$cli', '$volt',
'$grad', '$termo', '$tdpuerta', '$pai','$myDate')";
if ($conn->query($SQL) == TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $SQL . "<br>" . $conn->error;
}
$conn->close();
?>
答案 0 :(得分:0)
您会收到 未定义的索引错误 ,因为尚未创建第一次加载表单$_POST
,因为尚未提交表单,但您仍然运行引用$_POST
的代码。在尝试使用$_POST
有很多方法可以做到这一点,
<?php
include 'conn.php';
// has the form been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$myDate = date('Y/m/d');
$model = $_POST['Nmod'];
$fam = $_POST['Nfam'];
$orientacion = $_POST['Nodp'];
$tdcliente = $_POST['Ntdc'];
$cli = $_POST['Ncliente'];
$volt = $_POST['Nvoltaje'];
$grad = $_POST['Ngrado'];
$termo = $_POST['Ntermostato'];
$tdpuerta = $_POST['Ntdp'];
$pai = $_POST['Npais'];
$SQL = "INSERT INTO usuario (modelo ,familia ,odp ,tdc ,cliente ,voltaje ,
grado ,termostato, tdp, pais, fecha)
VALUES ('$model', '$fam',
'$orientacion', '$tdcliente', '$cli', '$volt',
'$grad', '$termo', '$tdpuerta', '$pai','$myDate')";
if ($conn->query($SQL) == TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $SQL . "<br>" . $conn->error;
}
$conn->close();
}
?>
我还必须提到,您的脚本可以向SQL Injection Attack开放 甚至if you are escaping inputs, its not safe! 在
MYSQLI_
或PDO
API中使用prepared parameterized statements