【问题标题】:Parse error: syntax error, unexpected '===' (T_IS_IDENTICAL) [duplicate]解析错误:语法错误,意外'==='(T_IS_IDENTICAL)[重复]
【发布时间】:2017-08-30 07:45:32
【问题描述】:

enter image description hereerror:解析错误:语法错误,第 7 行 C:\xampp\htdocs\new\add.php 中出现意外的 '===' (T_IS_IDENTICAL)

代码:

<?php

require_once('db.php');

if (
!empty($_POST['name']) &&!empty($_POST['alias']) &&!empty($_POST['date']) &&!empty($_POST['address']) &&!empty($_POST['educational']) &&!empty($_POST['father']) &&!empty($_POST['mother']) &&!empty($_POST['brother']) &&!empty($_POST['sister']) &&!empty($_POST['spouse']) &&!empty($_POST['children']) &&!empty($_POST['training']) &&!empty($_POST['employment']) &&!empty($_POST['organization']) &&!empty($_POST['affiliation']) &&!empty($_POST['criminal']) &&!empty($_POST['activities']) &&
is_array($_POST['name']) && is_array($_POST['alias']) && is_array($_POST['date']) && is_array($_POST['address']) && is_array($_POST['educational']) && is_array($_POST['father']) && is_array($_POST['mother']) && is_array($_POST['brother']) && is_array($_POST['sister']) && is_array($_POST['spouse']) && is_array($_POST['children']) && is_array($_POST['training']) && is_array($_POST['employment']) && is_array($_POST['organization']) && is_array($_POST['affiliation']) && is_array($_POST['criminal']) && is_array($_POST['activities']) &&
count($_POST['name']) === count($_POST['alias']) === count($_POST['date']) === count($_POST['address']) === count($_POST['educational']) === count($_POST['father']) === count($_POST['mother']) === count($_POST['brother']) === count($_POST['sister']) === count($_POST['spouse']) === count($_POST['children']) === count($_POST['training']) === count($_POST['employment']) === count($_POST['organization']) === count($_POST['affiliation']) === count($_POST['criminal']) === count($_POST['activities'])
) {

  $name_array = $_POST['name'];
  $alias_array = $_POST['alias'];
  $date_array = $_POST['date'];
  $address_array = $_POST['address'];
  $educational_array = $_POST['educational'];
  $father_array = $_POST['father'];
  $mother_array = $_POST['mother'];
  $brother_array = $_POST['brother'];
  $sister_array = $_POST['sister'];
  $spouse_array = $_POST['spouse'];
  $children_array = $_POST['children'];
  $training_array = $_POST['training'];
  $employment_array = $_POST['employment'];
  $organization_array = $_POST['organization'];
  $affiliation_array = $_POST['affiliation'];
  $criminal_array = $_POST['criminal'];
  $activities_array = $_POST['activities'];

  for ($i = 0; $i < count($name_array); $i++) {

    $name = mysql_real_escape_string($name_array[$i]);
    $alias = mysql_real_escape_string($alias_array[$i]);
    $date = mysql_real_escape_string($date_array[$i]);
    $address = mysql_real_escape_string($address_array[$i]);
    $educational = mysql_real_escape_string($educational_array[$i]);
    $father = mysql_real_escape_string($father_array[$i]);
    $mother = mysql_real_escape_string($mother_array[$i]);
    $brother = mysql_real_escape_string($brother_array[$i]);
    $sister = mysql_real_escape_string($sister_array[$i]);
    $spouse = mysql_real_escape_string($spouse_array[$i]);
    $children = mysql_real_escape_string($children_array[$i]);
    $training = mysql_real_escape_string($training_array[$i]);
    $employment = mysql_real_escape_string($employment_array[$i]);
    $organization = mysql_real_escape_string($organization_array[$i]);
    $affiliation = mysql_real_escape_string($affiliation_array[$i]);
    $criminal = mysql_real_escape_string($criminal_array[$i]);
    $activities = mysql_real_escape_string($activities_array[$i]);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    mysql_query("INSERT INTO student (name, alias, date, address, educational, father, mother, brother, sister, spouse, children, training, employment, organization, affiliation, criminal, activities) 
                VALUES ('$name', '$alias', '$date', '$address', '$educational', '$father', '$mother', '$brother', '$sister', '$spouse', '$children', '$training', '$employment', '$organization', '$affiliation', '$criminal', '$activities')");
  }
}

$conn->exec($sql);
echo "<script>alert('Account successfully added!'); window.location='pis.php'</script>";
?>

【问题讨论】:

  • 请在您的问题中添加相关代码。
  • 你想用这段代码实现什么?
  • 由于它使用$_POST 数组,因此可以假设这是查看是否已提交表单并且是否存在所有想要的字段。
  • 我正在尝试使用 textarea 将多个数组保存到我的数据库中
  • @Karl Danao:用count($_POST['name']) === ......代替isset($_POST['name']) &amp;&amp; ......

标签: php html mysql ajax post


【解决方案1】:

您不能像在 PHP 中那样将比较运算符串在一起。

改为尝试类似这里建议的方法https://stackoverflow.com/a/6920551/635522

编辑:考虑到我们希望在这里比较数组计数。

$arrays = [$_POST['name'],$_POST['alias'],$_POST['date'],$_POST['address'],...];

//create a second array with the lengths of all the arrays we want to compare
$lengths = [];
foreach($arrays as $array){
    $lengths[] = count($array);
}

//check if all the lengths are the same
if(count(array_unique($lengths)) == 1){
  // all match
}

【讨论】:

  • 通过将大量比较运算符串在一起,事情变得令人困惑,不仅对您而言,对 PHP 也是如此;最好调用一个函数来检查您的输入变量是否正常。类似于:if(variablesOkay()) 这样,您可以有序地执行所有这些检查。以上来自 Bananaapple 的代码一定会对你有所帮助!
  • 我非常怀疑 OP 是否想要检查所有变量是否相同。该代码表明$_POST['name'] 等是必须具有相同长度 的数组。分配数组时,您可以在每个 POST 变量上使用 count() 来解决这个问题。
  • 这与 OP 试图做的事情完全不同。而且您可以 像这样将比较字符串放在一起,但它可能无法达到您的预期。
  • @Will 如果不是语法错误,PHP 永远不会“混淆”。它可能会做一些你想要做的事情。
  • 好点 jeroen 和 @deceze - 相应地编辑了我的答案
猜你喜欢
  • 2018-06-28
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 2013-06-28
相关资源
最近更新 更多