Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP&;MYSQL-在数据库中插入数据失败_Php_Mysql_Sql_Database_Insert - Fatal编程技术网

PHP&;MYSQL-在数据库中插入数据失败

PHP&;MYSQL-在数据库中插入数据失败,php,mysql,sql,database,insert,Php,Mysql,Sql,Database,Insert,我创建了一个用于插入新公司的表单,并且在该页面上,是PHP脚本将数据插入数据库 我不知道这个密码的错误在哪里 <?php if (isset($_POST['submit'])) { // Form has been submitted. $query = mysql_query("INSERT INTO companies (name, subdomain0, subdomain1, subdomain2, position, country, city, di

我创建了一个用于插入新公司的表单,并且在该页面上,是PHP脚本将数据插入数据库

我不知道这个密码的错误在哪里

<?php
if (isset($_POST['submit']))
{
    // Form has been submitted.
    $query = mysql_query("INSERT INTO companies (name, subdomain0, subdomain1, subdomain2, 
    position, country, city, district, contact, set_up_date, address, phone, area_phone_code, website, fax, email)
    VALUES ('{$_POST['name']}', '{$_POST['domain']}', '{$_POST['subdomain1']}',
    '{$_POST['subdomain2']}', '{$_POST['position']}', '{$_POST['country']}', '{$_POST['city']}',
    '{$_POST['district']}', '{$_POST['contact']}', '{$_POST['setdate']}', '{$_POST['address']}', '{$_POST['phone']}',
    '{$_POST['areacode']}, '{$_POST['website']}', '{$_POST['fax']}', '{$_POST['email']}')");

    $result = mysql_query($query, $connection);
    if (!$result) {
        echo  "The company was not created.";
    } else {
        echo  "The company was successfully created.";
    }
}
?>

重写您的代码,并从这样的变量中删除那些
{}

    VALUES ('$_POST['name']','$_POST['domain']', '$_POST['subdomain1']',...
    VALUES ('$name', .... <-- same with other columns
1-在将它们发送到数据库之前,请确保将其转义

2-不要使用mysql,使用pdo或mysqli

要想逃离他们,就这样做:

 $name = mysql_real_escape_string($_POST['name']) ;
然后像这样把它传递给你的查询

    VALUES ('$_POST['name']','$_POST['domain']', '$_POST['subdomain1']',...
    VALUES ('$name', .... <-- same with other columns

因此,你必须小心。您可以通过该链接了解mysql_*函数的其他选项,因为它已被弃用


另外,最好是通过使用函数打印错误来找出错误。(检查链接中的备选方案,因为它也被弃用)

您遇到了什么错误?