PHP - 将复选框表单中的数据插入MYSQL

时间:2012-06-20 18:18:16

标签: php mysql

这是我的第一个使用服务器端技术的网站。 这也是我第一次深入研究PHPMYSQL一起工作。 我一直在工作和学习。

我为一个小型舞会礼服业务的产品条目数据库创建了一个基本管理页面。

我一直致力于管理员可用于将新条目上传到数据库的表单功能。

其中一些条目包括:

  

着装名称[文字输入]

     

颜色可用性   [复选框]

我在将多个复选框中的数据从HTML表单中导入数据库时​​遇到了麻烦。

复选框中的选项应表示布尔值(是/否 - 1/0)

这是表单结构:

    <form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
    <table width="90%" border="0" cellspacing="0" cellpadding="6">
  <tr>
    <td width="20%" align="right">Dress Name</td>

    <td width="80%"><label>
    <input name="Dress_name" type="text" id="Dress_name" size="64" />
    </label>
    </td>
  </tr>

  <tr>
    <td align="right">Collections</td>
    <td>
    <label>
      <select name="collections" id="collections">
            <option value="other">Other</option>
            <option value="hermione">Hermione</option>
            <option value="jora">Jora</option>
            <option value="manon">Manon</option>
      </select>
    </label></td>
  </tr>
  <tr>
    <td align="right">Available Sizes</td>
    <td>
        <input type="checkbox" name="sizes[]" value="1" /> Sizes 2-6<br />
        <input type="checkbox" name="sizes[]" value="1" />  Sizes 6-10 <br />
        <input type="checkbox" name="sizes[]" value="1" /> Sizes 10-14<br />
        <input type="checkbox" name="sizes[]" value="1" />  Sizes 14-18 <br />
        <input type="checkbox" name="sizes[]" value="1" /> Sizes 18-22<br />
        <input type="checkbox" name="sizes[]" value="1" />  Sizes 22-26 <br />

   </td>
  </tr>
  <tr>
    <td align="right">Dress Colours</td>
    <td>
        <input type="checkbox" name="colours[]" value="1" /> Pinks/Reds <br />
        <input type="checkbox" name="colours[]" value="1" />  Blues/Greens <br />
        <input type="checkbox" name="colours[]" value="1" /> Violet/Purple<br />
        <input type="checkbox" name="colours[]" value="1" />  Yellow/Orange/Brown/Gold <br />
        <input type="checkbox" name="colours[]" value="1" /> Black/White<br />

   </td>
  </tr>
  <tr>
    <td align="right">Product Image</td>
    <td><label>
      <input type="file" name="fileField" id="fileField" />
    </label></td>
  </tr>      
  <tr>
    <td>&nbsp;</td>
    <td><label>
      <input type="submit" name="button" id="button" value="Add This Item Now" />
    </label></td>
  </tr>
</table>
</form>

这是我为处理将数据提交到数据库的表单而构建的PHP。

<?php 
if (isset($_POST['Dress_name'])) {

$dress_name = mysql_real_escape_string($_POST['Dress_name']);
$collections = mysql_real_escape_string($_POST['collections']);
$sizesArray = $_POST['sizes'];
$coloursArray = $_POST['colours'];

foreach ($sizesArray as $key => $value)
{
    echo "Key = $key || Value = $value<br />";
}

foreach ($coloursArray as $ckey => $cvalue)
{
    echo "Key = $ckey || Value = $cvalue<br />";
}

//See if that product name is an identical match to another product in the system
$sql = mysql_query("SELECT ID FROM names WHERE Dress='$dress_name' LIMIT 1");
$productMatch = mysql_num_rows($sql); // count the output amount
if ($productMatch > 0) {
    echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
    exit();
}


 //Add this product into the database now
$sql = mysql_query("INSERT INTO names (Dress) 
    VALUES('$dress_name')") or die (mysql_error());
 $pid = mysql_insert_id();



 //Place image in the folder 
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
header("location: inventory_list.php"); 
exit();
}
?>

正如您所看到的,我已将数据放入数组中。而现在我已经碰到了一堵砖墙,我似乎找不到任何有意义的答案。

如何将布尔值转换为数据库表中的正确列?

感谢您的时间!

2 个答案:

答案 0 :(得分:9)

首先要知道的是,HTML复选框会在选中后返回结果,如果未选中则会返回任何内容。因此,在html中重要的是增加值:

<td align="right">Available Sizes</td>
<td>
  <input type="checkbox" name="sizes[]" value="1" /> Sizes 2-6<br />
  <input type="checkbox" name="sizes[]" value="2" />  Sizes 6-10 <br />
  <input type="checkbox" name="sizes[]" value="3" /> Sizes 10-14<br />
  <input type="checkbox" name="sizes[]" value="4" />  Sizes 14-18 <br />
  <input type="checkbox" name="sizes[]" value="5" /> Sizes 18-22<br />
  <input type="checkbox" name="sizes[]" value="6" />  Sizes 22-26 <br />
</td>

完全规范化的数据库模式如下所示:

//Note that 'names' and 'collections are overloaded
  //words and should be avoided
table BRANDS 
  id INT AUTOINCREMENT
  collectionname VARCHAR

table SIZES
  id INT AUTOINCREMENT
  sizecategory VARCHAR //ie 'Sizes 18-22'

table COLOURS
  id INT AUTOINCREMENT
  colourname VARCHAR 

table INVENTORY
  id INT AUTOINCREMENT
  brandid INT FOREIGN KEY (BRAND)
  imageurl VARCHAR

table INVENTORY_SIZES
  inventoryid INT FOREIGN KEY
  sizeid FOREIGN KEY

table INVENTORY_COLOURS
  inventoryid INT FOREIGN KEY
  colourid FOREIGN KEY

您的BRANDCOLOURSSIZES表格需要使用您的可用选项正确填充,理论上您可以使用这些表格中的数据动态加载您的页面。这些表中每一行的ID应最终作为复选框数组中的值。

//get your inputs
$query_values = array();
$query_values[':brandid'] = $dress_name = $_POST['Dress_name'];//note that you should change the value in your options here to the unique ids in your database.
$query_values[':sizeid'] = getSize($_POST[]);
$query_values[':colourid'] = getColour($_POST[]);
$query_values[':quantity'] = $_POST['quantity'];
$query_values[':imageurl'] = $_POST['imageurl'];


//Prepare and execute your sql
  //Note that PDO will handle escaping problematic inputs.
$sql = "INSERT INTO INVENTORY ('brandid', 'imageurl') VALUES (:brandid, :sizeid, :colourid, :quantity, :imageurl)";
executeInsert($sql, $query_values);
$inventoryid = mysql_insert_id();

saveSizes($_POST['sizes'], $inventoryid);
saveColours($_POST['colours'], $inventoryid);

function saveSizes($size_array, $inventoryid) {
  $sql = "INSERT INTO INVENTORY_SIZES ('inventoryid', 'sizeid') VALUES (:inventoryid, :sizeid)";
  foreach ($size_array as $sizeid) {
    $query_values = array();
    $query_values[':inventoryid'] = $inventoryid;
    $query_values[':sizeid'] = $sizeid;
    executeInsert($sql, $query_values);
  }
}

function saveColours($colour_array) {
  $sql = "INSERT INTO INVENTORY_COLOURS ('inventoryid', 'colourid') VALUES (:inventoryid, :colourid)";
  foreach ($size_array as $sizeid) {
    $query_values = array();
    $query_values[':inventoryid'] = $inventoryid;
    $query_values[':colourid'] = $colourid;
    executeInsert($sql, $query_values);
  }
}

function executeInsert($sql, $query_values) {
  $query = $conn->prepare($sql);
  $query->execute($query_values);
}

请务必使用PHP PDO来防范安全问题。我相当肯定其中一些可以被抽象出来/清理得更好,但这应该可以胜任。 (虽然我也很确定,因为我正在做这种感冒并没有进行测试,所以有一些小错误。)

答案 1 :(得分:0)

您需要在'if'之后将insert语句放在'else'语句中,以检查数据库中是否已存在该名称。

我不确定你到底想要做什么,因为没有声明你正在执行将'checkbox'值放在数据库中。您正在回显sizes数组作为管理员在提交此表单后看到的输出,但您根本没有将它放在数据库中。

如果您可以共享数据库结构(列名称),将会有什么帮助。如果您没有任何尺寸为2-5&amp;的柱子其他类别,添加&amp;然后将insert语句更改为以下内容:

INSERT INTO names (Dress,Size2to5,Size5to10,Size10to15,Size15to20) VALUES('$dress_name','$size[0]','$size[1]','size[2]','size[3]')

希望这会有所帮助。如果这对您有用,请告诉我们。