PHP-MySQL INSERT操作不起作用

时间:2017-03-21 08:43:32

标签: php mysql insert

我正在开发一个网络平台。我使用的是PHP和MySQL。我想将数据插入db。我的代码如下。

<?php
session_start();
require_once('../../system/database.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$ownerid     = (int)$_SESSION['id'];

$record_time = date('H:i:s');
$record_date = date('Y-m-d');

// form_data
$name        = strip_tags($_POST['name']);
$surname     = strip_tags($_POST['surname']);
$phone1      = strip_tags($_POST['phone1']);
$birthday    = strip_tags(trim($_POST['birthday']));
$gender      = strip_tags(trim($_POST['gender']));
$company     = strip_tags(trim($_POST['company']));
$address1    = strip_tags(trim($_POST['address1']));
$address2    = strip_tags(trim($_POST['address2']));
$phone2      = strip_tags(trim($_POST['phone2']));
$mail1       = strip_tags(trim($_POST['mail1']));
$mail2       = strip_tags(trim($_POST['mail2']));
$about       = strip_tags(trim($_POST['about']));
$type_of     = strip_tags(trim($_POST['type_of']));
$visible     = strip_tags(trim($_POST['visible']));

$query = "INSERT INTO contact (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES('$ownerid', '$name', '$surname', '$birthday', '$gender', '$address1', '$address2', '$phone1', '$phone2', '$mail1', '$mail2', '$mail1', '$mail2', '$about', '$type_of', '$visible', '$record_time', '$record_date')";
$result = mysqli_query($connection, $query);
mysqli_error($connection);

} else {
header('Location: ../new_contact.php');
}

但我的MySQL代码不起作用并写出任何错误信息!

4 个答案:

答案 0 :(得分:1)

值子句中的列数错误(您重复两次mail1和mail2)尝试

$query = "INSERT INTO contact 
         (ownerid, name, surname, birthday, gender, address1, address2, phone1,
   phone2, mail1, mail2, about, type_of, visible, time, date) 
VALUES('$ownerid', '$name', '$surname', '$birthday', '$gender', '$address1', '$address2', '$phone1', 
  '$phone2', '$mail1', '$mail2', '$about', '$type_of', '$visible', '$record_time', '$record_date')";

答案 1 :(得分:1)

编辑插入变量就像这样

$query = "INSERT INTO contact (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES(".$ownerid.", '".$name."', '".$surname."', '".$birthday."', '".$gender."', '".$address1."', '".$address2."', '".$phone1."', '".$phone2."', '".$mail1."', '".$mail2."', '".$about."', '".$type_of."', '".$visible."', '".$record_time."', '".$record_date."')";

答案 2 :(得分:0)

您需要正确地连接PHP变量,并且还需要正确的查询格式。试试这个

$query = "INSERT INTO `contact` (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES('".$ownerid."','".$name."', '".$surname."', '".$birthday."','".$gender."', '".$address1."', '".$address2."', '".$phone1."','".$phone2."', '".$mail1."','".$mail2."', '".$about."', '".$type_of."', '".$visible."', '".$record_time."', '".$record_date."')";

答案 3 :(得分:0)

如果您有权访问PHPMyAdmin,请将查询放在SQL部分并进行测试。有时,它可以为您提供寻找问题的线索。

正如ScaisEdge所说,您有16个条目作为键,但有18个条目作为插入值的值。在使用强调文字的PHP时使用编码软件会很有帮助(记事本++是免费的并且这样做)Eclipse是另一个。此外,将您的查询放入PHPMyAdmin“SQL”并测试您的查询,看它是否给你任何结果或引发错误。它将指向字符串中的哪个位置开始查找错误。