Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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_Insert - Fatal编程技术网

PHP MySQL插入操作不是';行不通

PHP MySQL插入操作不是';行不通,php,mysql,insert,Php,Mysql,Insert,我正在开发一个网络平台。我正在使用PHP和MySQL。我想把数据插入数据库。下面是我的代码 <?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('

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

<?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');
}

values子句中的列数错误(重复两次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."')";

您需要正确地连接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."')";

如果您有权访问PHPMyAdmin,请将查询放在SQL部分并对其进行测试。有时它可以给你一些线索,比如在哪里寻找问题


正如ScaisEdge所说,在insert中有16个条目作为键,但有18个条目作为值。当使用像文字一样突出显示的PHP时,使用编码软件是很有帮助的(notepad++是免费的,并且可以这样做),Eclipse是另一个。另外,将您的查询放入PHPMyAdmin“SQL”中,并测试您的查询,看看它是否给出任何结果或抛出错误。它将指向字符串中开始查找错误的位置

即使您已经厌倦了“”,也会有一些整数值。请确保在$query中不使用单引号,并直接在phpmyadmin中运行它,以获得更好的反馈,因为您在查询中将变量用作字符串。你应该用Concatatite哦天哪!谢谢@JamesSawyer如果我的答案是正确的,请将其标记为已接受…请参见此处的重要说明:当字符串用双引号或heredoc指定时,将在其中解析变量。
$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."')";