PHP语法错误,请查看我的代码,我不需要链接来盲目地查看无尽的代码

Posted

技术标签:

【中文标题】PHP语法错误,请查看我的代码,我不需要链接来盲目地查看无尽的代码【英文标题】:PHP Syntax error, please review my code, I don't need a link to mindlessly view endless code 【发布时间】:2017-09-09 01:33:34 【问题描述】:

我搜索了许多 *** 问题和提供的链接,例如“php Parse/Syntax Errors;以及如何解决它们?”

谁能查看我的代码部分并告诉我我的错误是什么?我在这件事上大发雷霆。

这是一个简单的学校项目。我不在乎更改代码以使用 mysqli

错误:语法错误,意外的“”,需要标识符(t_string) 或变量(t_variable)或数字(t_num_string)

谢谢!

            <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        form
            width: 400px;
            height: 600px;
            border: 1px solid black;
            display: block;
            margin: 0 auto;
        
        span
            display: block;
        

    </style>
</head>
<body>

<?php
    // A simple PHP script demonstrating how to connect to MySQL.
    // Provides a form for adding, and deleting a student record to the database

    $servername = getenv('IP');
    $username = getenv('JSMITH');
    $password = "1234";
    $database = "JSMITH";
    $dbport = 3306;

    // Create connection
    $db = new mysqli($servername, $username, $password, $database, $dbport);

    // Check connection
    if ($db->connect_error) 
        die("Connection failed: " . $db->connect_error);
     
    echo "Connected successfully (".$db->host_info.")";

    $thisPHP = $_SERVER['PHP_SELF'];
    echo <<<EOE
    <form action="$thisPHP" method="POST">
        Name:</span>
        <input type="text" name="Name"><br>

        <span>Address:</span>
        <input type="text" name="Address"><br>

        <span>Phone:</span>
        <input type="number" name="Phone_number"><br>

        <span>Email:</span>
        <input type="text" name="Email">

        <span>Avaliability:</span>
        <input type="radio" name="Avaliable" value="Now">Now: (&lt; 1 Month) <br>
        <input type="radio" name="Avaliable" value="Soon" >Soon: (1-3 months)<br>
        <input type="radio" name="Avaliable" value="Exploring">Upcoming: (3+ months)<br>


        <span>Company:</span>
        <input type="text" name="Company"><br>

        <span>Job:</span>
        <input type="text" name="Job"><br>

        <span>Description:</span>
        <input type="text" name="Description"><br>

        <span>Skills</span>
        <select name="Skill">
            <option value="SQL">SQL</option>
            <option value="Java">Java</option>
            <option value="HTML">HTML</option>
            <option value="javascript">Javascript</option>
            <option value="C++">C++</option>
            <option value="C#">C#</option>
            <option value="XML">XML</option>
            <option value="C">C</option>
            <option value="Perl">Perl</option>
            <option value="Python">Python</option>
            <option value="PHP">PHP</option>

        </select>

        <span>Experience:</span>
        <select name="Period">
        <option value="0" >0</option>
        <option value="1-3">1-3</option>
        <option value="3-5">3-5</option>
        <option value="5-10">5-10</option>
        <option value="10+">10+</option>
        </select>
        <br>

        <span>Position:</span>
        <input type="radio" name="Position" value="Team-Member" >Team-Member<br>
        <input type="radio" name="Position" value="Team Leader">Team Leader<br>
        <input type="radio" name="Position" value="Executive">Executive<br>
        <input type="submit" name="Add" value="Add"><br>

    </form>  
EOT;

    // Start executing the script
    
    $Name = $_POST["Name"];
    $Address = $_POST["Address"];
    $Phone = $_POST["Phone"];
    $Email = $_POST["Email"];
    $Availability = $_POST['Availability'];
    $Company = $_POST["Company Name"];
    $Job = $_POST['Job Title'];
    $description = $_POST['Description'];
    $Skill = $_POST['Skill'];
    $Period = $_POST['Period'];
    $Position = $_POST['Position'];
    $Update=$_POST["Update"];

 if(!empty($Name))
    $sql="INSERT INTO employee(Name, Address, Phone, Email, Availability, Company Name, Job Title, Description, Skill, Period, Position) 
    VALUES ('$Name','$Address','$Phone','$Email','$Avaliabilty','$Company','$Job','$Description','$Period','$Skill','$Position')";
    if($db->query($sql) == TRUE)
        echo ("Record added");
    



 if (isset($Update))
    $sql="UPDATE employee SET Address='$Address',Phone='$Phone',Email='$Email',Availability='$Availability',Company='$Company',Job='$Job',Description='$Description',Skill = '$Skill',Period='$Period',Position='$Position' WHERE Name='$Name' ";
    $db->query($sql);

  
    $db->close();
?>

【问题讨论】:

请给我们看完整的代码! 会的!现在更新 上传完整代码 我需要获取用户信息的简单项目。将信息输入表格。将其发送到 sql 文件。并允许用户编辑或删除表单提交的数据 你还没有在$Name = $_POST["Name"];上方开始&lt;?php。也许是错误! 【参考方案1】:

检查下面的代码我已经更正了表单并删除了导致错误的“”

        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style>
                form
                    width: 400px;
                    height: 600px;
                    border: 1px solid black;
                    display: block;
                    margin: 0 auto;
                
                span
                    display: block;
                

            </style>
        </head>
        <body>

        <?php
            // A simple PHP script demonstrating how to connect to MySQL.
            // Provides a form for adding, and deleting a student record to the database

            $servername = getenv('IP');
            $username = getenv('JSMITH');
            $password = "1234";
            $database = "JSMITH";
            $dbport = "3306";

            // Create connection
            $db = new mysqli($servername, $username, $password, $database, $dbport);

            // Check connection
            if ($db->connect_error) 
                die("Connection failed: " . $db->connect_error);
             
            echo "Connected successfully (".$db->host_info.")";

            $thisPHP = $_SERVER['PHP_SELF'];
        ?>
            <form action="$thisPHP" method="POST">
                Name:</span>
                <input type="text" name="Name"><br>

                <span>Address:</span>
                <input type="text" name="Address"><br>

                <span>Phone:</span>
                <input type="number" name="Phone_number"><br>

                <span>Email:</span>
                <input type="text" name="Email">

                <span>Avaliability:</span>
                <input type="radio" name="Avaliable" value="Now">Now: (&lt; 1 Month) <br>
                <input type="radio" name="Avaliable" value="Soon" >Soon: (1-3 months)<br>
                <input type="radio" name="Avaliable" value="Exploring">Upcoming: (3+ months)<br>


                <span>Company:</span>
                <input type="text" name="Company"><br>

                <span>Job:</span>
                <input type="text" name="Job"><br>

                <span>Description:</span>
                <input type="text" name="Description"><br>

                <span>Skills</span>
                <select name="Skill">
                    <option value="SQL">SQL</option>
                    <option value="Java">Java</option>
                    <option value="HTML">HTML</option>
                    <option value="Javascript">Javascript</option>
                    <option value="C++">C++</option>
                    <option value="C#">C#</option>
                    <option value="XML">XML</option>
                    <option value="C">C</option>
                    <option value="Perl">Perl</option>
                    <option value="Python">Python</option>
                    <option value="PHP">PHP</option>

                </select>

                <span>Experience:</span>
                <select name="Period">
                <option value="0" >0</option>
                <option value="1-3">1-3</option>
                <option value="3-5">3-5</option>
                <option value="5-10">5-10</option>
                <option value="10+">10+</option>
                </select>
                <br>

                <span>Position:</span>
                <input type="radio" name="Position" value="Team-Member" >Team-Member<br>
                <input type="radio" name="Position" value="Team Leader">Team Leader<br>
                <input type="radio" name="Position" value="Executive">Executive<br>
                <input type="submit" name="Add" value="Add"><br>

            </form>  
        <?php

            // Start executing the script
            if (!empty($_POST))
            
            $Name = $_POST["Name"];
            $Address = $_POST["Address"];
            $Phone = $_POST["Phone"];
            $Email = $_POST["Email"];
            $Availability = $_POST['Availability'];
            $Company = $_POST["Company Name"];
            $Job = $_POST['Job Title'];
            $description = $_POST['Description'];
            $Skill = $_POST['Skill'];
            $Period = $_POST['Period'];
            $Position = $_POST['Position'];
            $Update=$_POST["Update"];

         if(!empty($Name))
            $sql="INSERT INTO employee(Name, Address, Phone, Email, Availability, Company Name, Job Title, Description, Skill, Period, Position) 
            VALUES ('$Name','$Address','$Phone','$Email','$Avaliabilty','$Company','$Job','$Description','$Period','$Skill','$Position')";
            if($db->query($sql) == TRUE)
                echo ("Record added");
            


        
         if (isset($Update))
            $sql="UPDATE employee SET Address='$Address',Phone='$Phone',Email='$Email',Availability='$Availability',Company='$Company',Job='$Job',Description='$Description',Skill = '$Skill',Period='$Period',Position='$Position' WHERE Name='$Name' ";
            $db->query($sql);

          

            $db->close();
        
        ?>

【讨论】:

谢谢!当我尝试将其与本地数据库文件一起运行时,我收到“在此服务器上找不到请求的 URL /$thisPHP”。提交数据时出错,有什么想法吗? 这意味着action="$thisPHP" 不工作。将其替换为action="&lt;?php echo $_SERVER['PHP_SELF']?&gt;" 【参考方案2】:

&lt;form action="$thisPHP" method="POST"&gt; 之前添加?&gt; 并在// Start executing the script 行之前添加&lt;?php 并检查会发生什么!

【讨论】:

不要忘记在表单操作中添加&lt;?php echo $thisPHP; ?&gt; 并替换所有变量! 我很困惑,你能给我举个例子吗? :o 兄弟,您的代码似乎并非完全由您编写。我试图给你最简单的解决方案,你无法理解。我不能为你调试整个代码。我告诉过你你的错误,你现在可以解决它了。 我在我的第一个 php 课程中。只是努力学习。感谢您的帮助! 太棒了!一个快速提示:您不能在 &lt;?php?&gt; 之间编写 html 代码。 :)

以上是关于PHP语法错误,请查看我的代码,我不需要链接来盲目地查看无尽的代码的主要内容,如果未能解决你的问题,请参考以下文章

PHP:您的 SQL 语法有错误,请查看手册

我的 PHP 登录脚本出现以下错误

编写查询时的 SQL 语法错误

如何更正命令生成器中的语法错误? [复制]

MySQL 语法错误?奇怪的错误

为啥总是在我的代码中显示语法错误?