English 中文(简体)
如何利用阵列将数据输入我的ql数据库?
原标题:How to enter form data into a mysql database using an array?
  • 时间:2012-01-13 00:20:29
  •  标签:
  • php

我的表格提出了我为另一个项目所实践的对应措施的步骤和内容。 我已经建立了提交数据阵列的表格,但我无法正确将数据输入数据库。 我在此重复了形式。 表格是作为另一个PHP页面的一部分,用户在进入一个对应名称以补充数据库时被称作。 如果我能说明如何正确将这些表格插入数据库,那么我就希望有10个单独的表格条目。

<form action="add_recipe2.php" method="post">
    <fieldset>
        <legend>Add a Recipe</legend>
        <table>
            <tr>
                <td>Recipe Name:</td>
                <td><input type="text" name="recipename" value="$recipename"></td>
           </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[0][step]" placeholder="1"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[0][ingredients]" placeholder="Ingredients"></td>
            </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[1][step]" placeholder="2"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[1][ingredients]" placeholder="Ingredients"></td>
            </tr>
            <tr>
                <td>Step:</td>
                <td><input type="text" name="recipe[2][step]" placeholder="3"></td>
                <td>Ingredients:</td>
                <td><input type="text" name="recipe[2][ingredients]" placeholder="Ingredients"></td>
            </tr>

        </table>
        <button type="submit">Add a Recipe</button>
        <button type="reset">Reset</button>

    </fieldset>
</form>

这是将数据输入数据库的购买力平价。 问题是,在我只给数据库增加两个记录时,最后的记录仍加进,但插入了一个空白线。 我只需要增加从表格中删除的数据,即使数据只是一线。 我进行了长时间的研究,这是我发现的答案之一。 但是,如果表格中的数据没有增加,它仍然不会停止输入数据库。

$recipename = $_REQUEST["recipename"];

$conn = mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("test");

foreach($_POST[ recipe ] as $recipe) { 

// Add to database
$sql1 = "INSERT INTO `recipes` (recipe, step, ingredients) VALUES   ( ".$_POST[ recipename ]." ,  ".$recipe[ step ]." ,  ".$recipe[ ingredients ]." )";
mysql_query($sql1, $conn) or die(mysql_error());
 } //end foreach

我可以这样说。 我需要帮助。 我怀疑,如果我不是现有表格条目的数量,我必须设法说明我实际发送的记录有多少。

最佳回答

如果在查询这些数值之前填充阵列部分的数值,你将需要加以测试。 并且,您的“科学与科学学会”通过<密码>mysql_real_爱戴_string(,从中删除所有“反射”的数值:

$recipename = mysql_real_escape_string($_POST[ recipename ]);

foreach($_POST[ recipe ] as $recipe) { 
  // Only insert if step is non-empty.
  if (!empty($recipe[ step ]) {
    // Add to database

    // Escape against SQL injection
    $recipe[ step ] = mysql_real_escape_string($recipe[ step ];
    $recipe[ ingredients ] = mysql_real_escape_string($recipe[ ingredients ];

    $sql1 = "INSERT INTO `recipes` (recipe, step, ingredients) VALUES   ( ".$recipename." ,  ".$recipe[ step ]." ,  ".$recipe[ ingredients ]." )";
    mysql_query($sql1, $conn) or die(mysql_error());
  }
} 
问题回答

事实是,即使用户以对应形式填充信息,但空数值仍在提交。

在将数据输入数据库之前,你必须验证数据:

function isValidRecipe($recipe){
    // returns true if ingredients and step are not empty
    return !(empty($recipe[ ingredients ]) || empty($recipe[ step ]));
}
foreach($_POST[ recipe ] as $recipe) {
if (isValidRecipe($recipe)){
    // Add to database
    $sql1 = "INSERT INTO `recipes` (recipe, step, ingredients) VALUES ( ".$_POST[ recipename ]." ,  ".$recipe[ step ]." ,  ".$recipe[ ingredients ]." )";
        mysql_query($sql1, $conn) or die(mysql_error());
    }
}

请注意,这是最起码的验证,你或许应该更彻底地检查所有情况。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签