无法将数据从表单插入数据库

时间:2013-12-29 12:40:35

标签: php mysql database insert

我有以下代码:它包含一个在将数据上传到数据库之前先收集数据的表单:我怀疑的部分如下:

///////////////////////////////////////////////

    if (isset($_POST['submit'])) {

    $i = 0;
foreach ($_POST as $val) {

$business_name = trim(mysql_real_escape_string($_POST["business_name"][$i]));
        $business_type = trim(mysql_real_escape_string($_POST["business_type"][$i]));
        $country = trim(mysql_real_escape_string($_POST["country"][$i]));
        $province = trim(mysql_real_escape_string($_POST["province"][$i]));
        $address = trim(mysql_real_escape_string($_POST["address"][$i]));
        $postcode = trim(mysql_real_escape_string($_POST["postcode"][$i]));
        $telephone1 = trim(mysql_real_escape_string($_POST["telephone1"][$i]));
        $telephone2 = trim(mysql_real_escape_string($_POST["telephone2"][$i]));
        $category = trim(mysql_real_escape_string($_POST["category"][$i]));
        $email = trim(mysql_real_escape_string($_POST["email"][$i]));


mysql_query("INSERT INTO company_details (company_id, business_name, business_type, country, province, address, postcode, telephone1, telephone2, category, email) VALUES (NULL, '$business_name', '$business_type', '$country', '$province', '$address', '$postcode', '$telephone1', '$telephone2', '$category', '$email')");
$i++;
}
} 

///////////////////////

我正在粘贴下面的完整代码。不确定我错过了什么。有人可以帮我一下。感谢您的帮助。

    <!DOCTYPE html>
<head>
<title>Title</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
<style type="text/css">
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
       <title>Page Title</title>
       <style type="text/css" charset="utf-8"/>
*{background:#eee;padding:2px;box-sizing:border-box;
-moz-box-sizing:border-box; /* Firefox */}
</style>
</head>
<body>
</body>
</html>
</form>



<?php
$fullstring = file_get_contents('cached-process.php');

function get_string_between($string, $start, $end) {
    $start = preg_quote($start);
    $end = preg_quote($end);
    $pattern = "~$start\s*(.*?)$end\s*~";
    $match = preg_match_all($pattern, $string, $matches);
    if ($match) {
        return $matches[1];
    }
}
$parsed = get_string_between($fullstring, "<h3>", "</h3>");
$parsed1 = get_string_between($fullstring, "<b>", "</b>");
$parsed2 = get_string_between($fullstring, "Forme Juridique : ", "<br>");
$parsed3 = get_string_between($fullstring, "Pays :", "<br>");
$parsed4 = get_string_between($fullstring, "province : ", "<br>");
$parsed5 = get_string_between($fullstring, "Adresse du Siège Social : ", "<br>");
$parsed6 = get_string_between($fullstring, "Code postal : ", "<br>");
$parsed7 = get_string_between($fullstring, "Téléphone(1) : ", "<br>");
$parsed8 = get_string_between($fullstring, "Téléphone(2) : ", "<br>");

echo '<h3>'. $parsed[0].'</h3>';
echo '<form  action="" method="POST">';
//echo "Category:<input name=\"category\" type=\"text\" value=\"". $parsed[0]. "\">";
echo '<div class="content">';
echo '<div style="clear: both;"></div>';
for ($i=0; $i < count($parsed4); $i++) { 

echo '<div class="theform">';
echo "Business:  <input name=\"business_name\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Business type:  <input name=\"business_type\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
echo "Country:  <input name=\"country\" type=\"text\" value=\"" . strip_tags($parsed3[$i]) . "\">","<br>" ;
echo "province:  <input name=\"province\" type=\"text\" value=\"" . strip_tags($parsed4[$i]) . "\">","<br>" ;
echo "Address:  <input name=\"address\" type=\"text\" value=\"" . strip_tags($parsed5[$i]) . "\">","<br>" ;
echo "Postal code:  <input name=\"postcode\" type=\"text\" value=\"" . strip_tags($parsed6[$i]) . "\">","<br>" ;
echo "Telephone 1:  <input name=\"telephone1\" type=\"text\" value=\"" . strip_tags($parsed7[$i]) . "\">","<br>";
echo "Telephone 2:  <input name=\"telephone2\" type=\"text\" value=\"" . strip_tags($parsed8[$i]) . "\">","<br>";
echo '</div>';

}
echo '<div style="clear: both;"></div>';
echo '</div>';
echo '<input type="submit" value="Submit">';
echo '</form>'; 

?>

<?php
$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
//test if the connection is established successfully then it will proceed in next process else it will throw an error message
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

//we specify here the Database name we are using
mysql_select_db('db');



if (isset($_POST['submit'])) {

    $i = 0;
foreach ($_POST as $val) {

$business_name = trim(mysql_real_escape_string($_POST["business_name"][$i]));
        $business_type = trim(mysql_real_escape_string($_POST["business_type"][$i]));
        $country = trim(mysql_real_escape_string($_POST["country"][$i]));
        $province = trim(mysql_real_escape_string($_POST["province"][$i]));
        $address = trim(mysql_real_escape_string($_POST["address"][$i]));
        $postcode = trim(mysql_real_escape_string($_POST["postcode"][$i]));
        $telephone1 = trim(mysql_real_escape_string($_POST["telephone1"][$i]));
        $telephone2 = trim(mysql_real_escape_string($_POST["telephone2"][$i]));
        $category = trim(mysql_real_escape_string($_POST["category"][$i]));
        $email = trim(mysql_real_escape_string($_POST["email"][$i]));


mysql_query("INSERT INTO company_details (company_id, business_name, business_type, country, province, address, postcode, telephone1, telephone2, category, email) VALUES (NULL, '$business_name', '$business_type', '$country', '$province', '$address', '$postcode', '$telephone1', '$telephone2', '$category', '$email')");
$i++;
}
} 

?>

1 个答案:

答案 0 :(得分:0)

对于初学者来说,你甚至根本不需要迭代逻辑。以下应该可以正常工作。

if (isset($_POST['submit'])) {
    $business_name = trim(mysql_real_escape_string($_POST["business_name"][$i]));
    $business_type = trim(mysql_real_escape_string($_POST["business_type"][$i]));
    $country = trim(mysql_real_escape_string($_POST["country"][$i]));
    $province = trim(mysql_real_escape_string($_POST["province"][$i]));
    $address = trim(mysql_real_escape_string($_POST["address"][$i]));
    $postcode = trim(mysql_real_escape_string($_POST["postcode"][$i]));
    $telephone1 = trim(mysql_real_escape_string($_POST["telephone1"][$i]));
    $telephone2 = trim(mysql_real_escape_string($_POST["telephone2"][$i]));
    $category = trim(mysql_real_escape_string($_POST["category"][$i]));
    $email = trim(mysql_real_escape_string($_POST["email"][$i]));
    mysql_query("INSERT INTO company_details (company_id, business_name, business_type,     country, province, address, postcode, telephone1, telephone2, category, email) VALUES (NULL, '$business_name', '$business_type', '$country', '$province', '$address', '$postcode', '$telephone1', '$telephone2', '$category', '$email')");
} 
相关问题