解析错误:语法错误,第 20 行 C:\Program Files (x86)\EasyPHP-5.3.9\www\Inventory\addavaya.php 中的意外 T_VARIABLE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10377873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:03:03  来源:igfitidea点击:

Parse error: syntax error, unexpected T_VARIABLE in C:\Program Files (x86)\EasyPHP-5.3.9\www\Inventory\addavaya.php on line 20

php

提问by Rio Salonoy

I'm new to PHP and get the message Parse error: syntax error, unexpected T_VARIABLE in .. on line 20. I would be ever so grateful if someone could help me with this error as it's really started to stress me out. Been on dreamweaver and it says error where it says $Username=... But I just can't seem to fix it

我是 PHP 新手,在第 20 行收到消息解析错误:语法错误,意外的 T_VARIABLE in ..。如果有人能帮助我解决这个错误,我将不胜感激,因为它真的开始让我感到压力。一直在 Dreamweaver 上,它说 $Username= 的错误...但我似乎无法修复它

<?php
    $host="localhost"; // Host name 
    $username="xxx"; // Mysql username 
    $password="xxx"; // Mysql password 
    $db_name="xxx"; // Database name 
    $tbl_name="avaya"; // Table name

    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");


    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("inventory", $con)


    $addavaya="INSERT INTO avaya_pabx(critical_spare_id, serial_no, ,comcode, version, circuit_pack, classification, location, availability, date, client)
    VALUES ('$_POST[critical_spare_id]', '$_POST[serial_no]', '$_POST[comcode]', '$_POST[version]', '$_POST[circuit_pack]', 
    '$_POST[classification]', '$_POST[location]' , '$_POST[availability]', '$_POST[date]', '$_POST[client]')";


    mysql_query($addavaya,$con)

    if (!mysql_query($addavaya,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";

    mysql_close($con);

    ?>

回答by PeeHaa

You have a missing semi colon:

您缺少一个分号:

mysql_select_db("inventory", $con);
                                  ^

You should concatenate the query:

您应该连接查询:

$addavaya="INSERT INTO avaya_pabx(critical_spare_id, serial_no, ,comcode, version, circuit_pack, classification, location, availability, date, client)
    VALUES ('". $_POST['critical_spare_id'] . "', '" . $_POST['serial_no']. "', etc...

Also please stop using the old mysql_*functions. Use either mysqli_*or PDO. mysql_* functions will be deprecated in the future.

另外请停止使用旧mysql_*功能。使用mysqli_*或 PDO。mysql_* 函数将在未来被弃用。

And please please pleasesanitize the input before querying the database!

请请净化输入查询数据库之前!

回答by Logan Serman

$_POSTis an associative array, so you need to be accessing it using single quotes, for example:

$_POST是一个关联数组,因此您需要使用单引号访问它,例如:

$_POST['critical_spare_id'] instead of $_POST[critical_spare_id]

This means you will have to use the concatenation operator in your query.

这意味着您必须在查询中使用连接运算符。

$addavaya="INSERT INTO avaya_pabx(critical_spare_id, serial_no, ,comcode, version, circuit_pack, classification, location, availability, date, client)
    VALUES ('" . $_POST['critical_spare_id'] . "', ...

Alternatively (and for your own good), you could use parameterized queries with PDOor mysqli.

或者(为了您自己的利益),您可以使用带有PDOmysqli 的参数化查询。

回答by Ethan H

Add a ;to the end of mysql_select_db("inventory", $con)

;在末尾添加一个mysql_select_db("inventory", $con)