Php 您的SQL语法有错误;-和未定义的变量错误-可能已连接

Php 您的SQL语法有错误;-和未定义的变量错误-可能已连接,php,mysql,mysqli,syntax,Php,Mysql,Mysqli,Syntax,我对使用php和SQL进行编码非常陌生,所以我可能会有很多问题。但正如标题所说,我得到了这个错误 您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以获取第1行“”附近要使用的正确语法 我不确定这是指什么。我已经尽可能多地检查了代码,但没有发现语法错误。也许我还不知道 <?php // including the database connection file include_once("config.php"); if(isset($_POST['update']) &

我对使用php和SQL进行编码非常陌生,所以我可能会有很多问题。但正如标题所说,我得到了这个错误

您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以获取第1行“”附近要使用的正确语法

我不确定这是指什么。我已经尽可能多地检查了代码,但没有发现语法错误。也许我还不知道

<?php 
// including the database connection file
include_once("config.php");

if(isset($_POST['update']) && isset($_GET['site']))
{    
$sitenumber = $_POST['sitenumber'];
$videolink = $_POST['videolink'];
$daynight = $_POST['daynight'];
$maxtents = $_POST['maxtents'];
$maxpersons = $_POST['maxpersons'];
$geography = $_POST['geography'];
$view = $_POST['view'];
$forestcover = $_POST['forestcover'];
$waterfront = $_POST['waterfront'];
$firepit = $_POST['firepit'];
$description = $_POST['description'];
$reslink = $_POST['reslink'];   

// checking empty fields
if(empty($sitenumber) || empty($videolink) || empty($daynight) || 
empty($maxtents) || empty($maxpersons) || empty($geography) || 
    empty($view) || empty($forestcover) || empty($waterfront) || 
empty($firepit) || empty($description) || empty($reslink)) {                
    if(empty($sitenumber)) {
        echo "<font color='red'>Site Number field is empty.</font><br/>";
    }

    if(empty($videolink)) {
        echo "<font color='red'>YouTube Link field is empty.</font><br/>";
    }

    if(empty($daynight)) {
        echo "<font color='red'>Day or overnight field is empty.</font> 
<br/>";
    }        

    if(empty($maxtents)) {
        echo "<font color='red'>Maximum Tents field is empty.</font><br/>";
    }

    if(empty($maxpersons)) {
        echo "<font color='red'>Maximum Persons field is empty.</font> 
 <br/>";
    }        

    if(empty($geography)) {
        echo "<font color='red'>Geography field is empty.</font><br/>";
    }

    if(empty($view)) {
        echo "<font color='red'>View field is empty.</font><br/>";
    }        

    if(empty($forestcover)) {
        echo "<font color='red'>Forest Cover field is empty.</font><br/>";
    }

    if(empty($waterfront)) {
        echo "<font color='red'>Waterfront Access field is empty.</font> 
 <br/>";
    }  

    if(empty($firepit)) {
        echo "<font color='red'>Firepit field is empty.</font><br/>";
    }        

    if(empty($description)) {
        echo "<font color='red'>Description field is empty.</font><br/>";
    }

    if(empty($reslink)) {
        echo "<font color='red'>Reservation Link Access field is empty. 
 </font><br/>";
    }       
} else {    
    //updating the table
    $result = mysqli_query($mysqli, "UPDATE sites SET 
 sitenumber='$sitenumber',videolink='$videolink',daynight='$daynight',
     maxtents='$maxtents',maxpersons='$maxpersons',geography='$geography', 
     view='$view',forestcover='$forestcover',waterfront='$waterfront', 
     firepit='$firepit',description='$description',reslink='$reslink' WHERE 
 sitenumber=$sitenumber");

    //redirectig to the display page. In our case, it is index.php
    //header("Location: index.php");
}
}
            echo mysqli_error($mysqli);
 ?>
<?php
//getting id from url
$sitenumber = $_GET['site'];

//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM sites WHERE 
sitenumber=$sitenumber");

while($res = mysqli_fetch_array($result))
{
$sitenumber = $res['sitenumber'];
$videolink = $res['videolink'];
$daynight = $res['daynight'];
$maxtents = $res['maxtents'];
$maxpersons = $res['maxpersons'];
$geography = $res['geography'];
$view = $res['view'];
$forestcover = $res['forestcover'];
$waterfront = $res['waterfront'];
$firepit = $res['firepit'];
$description = $res['description'];
$reslink = $res['reslink'];
}
            echo mysqli_error($mysqli);
?>
<html>
<head>    
<title>Edit Data</title>
</head>

<body>
<a href="index.php">Home</a>
<br/><br/>

<form name="form1" method="post" action="edit.php">
    <table border="0">
    <tr> 
            <td>Site Number</td>
            <td><input type="number" name="sitenumber" value="<?php echo 
$sitenumber;?>"></td>
        </tr>
        <tr> 
            <td>YouTube Link</td>
            <td><input type="url" name="videolink" value="<?php echo 
$videolink;?>"></td>
        </tr>
        <tr> 
            <td>Day or Overnight</td>
            <td><select name="daynight" value="<?php echo $daynight;?>">
            <option value="Day">Day</option>
            <option value="Overnight">Overnight</option></td>

        </tr>
                    <tr> 
            <td>Maximum Tents</td>
            <td><input type="number" name="maxtents" value="<?php echo 
$maxtents;?>"></td>
        </tr>
                    <tr> 
            <td>Maximum Persons</td>
            <td><input type="number" name="maxpersons" value="<?php echo 
$maxpersons;?>"></td>
        </tr>
                    <tr> 
            <td>Geography</td>
            <td><input type="text" name="geography" value="<?php echo 
$geography;?>"></td>
        </tr>
                    <tr> 
            <td>View</td>
            <td><input type="text" name="view" value="<?php echo $view;?>"> 
 </td>
        </tr>
                    <tr> 
            <td>Forest Cover</td>
            <td><input type="text" name="forestcover" value="<?php echo 
  $forestcover;?>"></td>
        </tr
                    <tr> 
            <td>Waterfront Access</td>
            <td><select name="waterfront" value="<?php echo $waterfront;?>">
            <option value="Yes">Yes</option>
            <option value="No">No</option></td>
        </tr>
                    <tr> 
            <td>Firepit Availability</td>
            <td><select name="firepit" value="<?php echo $firepit;?>">
            <option value="Yes">Yes</option>
            <option value="No">No</option></td>
        </tr>
                    <tr> 
            <td>Site Description</td>
            <td><input type="text" name="description" value="<?php echo 
$description;?>"></td>
        </tr>
                    <tr> 
            <td>Reservation Link</td>
           <td><input type="url" name="reslink" value="<?php echo $reslink;? 
>"></td>
        </tr>
            <td><input type="hidden" name="site" value="<?php echo 
$_GET['site'];?>"></td>
            <td><input type="submit" name="update" value="Update"></td>
        </tr>
    </table>
</form>
</body>
</html>

编辑数据


站点号 更正此错误:

$result = mysqli_query($mysqli, "SELECT * FROM sites WHERE sitenumber='".$sitenumber."' ");
这是:

$result = mysqli_query($mysqli, "UPDATE sites SET 
sitenumber='$sitenumber',videolink='$videolink',daynight='$daynight',
maxtents='$maxtents',maxpersons='$maxpersons',geography='$geography', 
view='$view',forestcover='$forestcover',waterfront='$waterfront', 
firepit='$firepit',description='$description',reslink='$reslink' WHERE 
sitenumber='$sitenumber'");
更正此错误:

$result = mysqli_query($mysqli, "SELECT * FROM sites WHERE sitenumber='".$sitenumber."' ");
这是:

$result = mysqli_query($mysqli, "UPDATE sites SET 
sitenumber='$sitenumber',videolink='$videolink',daynight='$daynight',
maxtents='$maxtents',maxpersons='$maxpersons',geography='$geography', 
view='$view',forestcover='$forestcover',waterfront='$waterfront', 
firepit='$firepit',description='$description',reslink='$reslink' WHERE 
sitenumber='$sitenumber'");

SQL查询看起来不错,但问题可能来自变量的值

由于查询没有正确转义(和),我建议您在执行之前调试查询。 通过这种方式,您将能够理解将在数据库中执行什么

如果不使用,可以将查询放入变量中,然后使用
var\u dump
转储它

然后,打开phpmyadmin(我假设您至少有权访问它),并将变量的值(即您的查询)粘贴到SQL编辑器中。然后执行它,应该会有一条消息解释错误在哪里。 它将帮助您了解为什么使用prepared语句很重要,方法是查看哪个变量的值是错误的(例如,它包括


我希望这会对您的SQL查询有所帮助,但问题可能来自变量的值

由于查询没有正确转义(和),我建议您在执行之前调试查询。 通过这种方式,您将能够理解将在数据库中执行什么

如果不使用,可以将查询放入变量中,然后使用
var\u dump
转储它

然后,打开phpmyadmin(我假设您至少有权访问它),并将变量的值(即您的查询)粘贴到SQL编辑器中。然后执行它,您应该会收到一条消息,解释错误的位置。 它将帮助您了解为什么使用prepared语句很重要,方法是查看哪个变量的值是错误的(例如,它包括


我希望这会有所帮助

您的代码容易受到SQL注入的攻击,即使改用SQL,也会受到黑客攻击。检查:在哪个查询中出现语法错误??。因为我查看了您的所有查询,但没有错误。它似乎来自$result=mysqli_查询($mysqli,“选择*来自sitenumber=$sitenumber的站点”);我将研究准备好的声明。正如我所说,我对编码还是相当陌生。@DamienBorden您得到的查询是错误的、更新的还是选择的??您的代码容易受到SQL注入的攻击,即使改用它,也会受到黑客攻击。检查:在哪个查询中出现语法错误??。因为我查看了您的所有查询,但没有错误。它似乎来自$result=mysqli_查询($mysqli,“选择*来自sitenumber=$sitenumber的站点”);我将研究准备好的声明。正如我所说,我对编码还是相当陌生。@DamienBorden您得到的查询是错误、更新还是选择??这解决了我的语法错误。现在我只需要弄清楚为什么我所有的变量都没有定义。。。谢谢。您必须在“include_once”(“config.php”);之后声明所有变量如下:“$sitenumber=”“$videolink=“;”。。。。。。直到最后一个变量。这解决了我的语法错误。现在我只需要弄清楚为什么我所有的变量都没有定义。。。谢谢。您必须在“include_once”(“config.php”);之后声明所有变量如下:“$sitenumber=”“$videolink=“;”。。。。。。直到最后一个变量。