从下拉框中的选定选项中将记录插入表中

时间:2016-03-28 22:35:52

标签: php mysql

我对PHP很新,我想要实现的是根据选项中表格中选择元素的选定选项将表中的选项插入到表中。

PHP:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "inserir")) {
$insertSQL = sprintf("INSERT INTO ['tabela'] (imagem, hipermercado, departamento, descricao, `desc`, tipo_desc, preco_ant, preco_desc, validade) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['imagemurl'], "text"),
                   GetSQLValueString($_POST['hipermercado'], "text"),
                   GetSQLValueString($_POST['departamento'], "text"),
                   GetSQLValueString($_POST['descricao'], "text"),
                   GetSQLValueString($_POST['desc'], "text"),
                   GetSQLValueString($_POST['tipo_desc'], "text"),
                   GetSQLValueString($_POST['preco_ant'], "double"),
                   GetSQLValueString($_POST['preco_desc'], "double"),
                   GetSQLValueString($_POST['validade'], "text"));
  mysql_select_db($database_promocaototal, $promocaototal);
  $Result1 = mysql_query($insertSQL, $promocaototal) or die(mysql_error());

HTML:

<select name="table" id="table" style="color:#000000; font-size:16px">
      <option>Select</option>
      <option>table1</option>
      <option>table2</option>
    </select>

基本上用选择的选项替换php代码中的表名,即“table1”或“table2”这可能吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,您似乎错过了如何将下拉信息提供给PHP脚本的链接。

尝试使用yourscript.php$_GET["variableName"]。通过提交表单(例如,通过按钮,如教程中所述),您可以使用{{1}} forms在{{1}}中获取输入。确保清理输入以避免SQL注入。

相关问题