当前位置: 首页 > 知识库问答 >
问题:

mysqli_query(插入不工作[重复]

郑旭
2023-03-14

我有一个问题,这个代码没有插入到数据库中的值。。。

<html>
<head>
<title>Add Data</title>
</head>

<body>
<?php 
//including the database connection file
include("config.php");

if(isset($_POST['Submit'])) {    
$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 field is empty.</font><br/>";
    }
    //link to the previous page
    echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
    // if all the fields are filled (not empty)             
    //insert data to database
    $result = mysqli_query($mysqli, "INSERT INTO sites (sitenumber,videolink,daynight,maxtents,maxpersons,geography,view,forestcover,waterfront,firepit,description) 
    VALUES ('{$sitenumber}','{$videolink}','{$daynight}','{$maxtents}','{$maxpersons}','{$geography}','{$view}','{$forestcover}','{$waterfront}','{$firepit}','{$description}')");

    //display success message
    echo "<font color='green'>Data added successfully.";
    echo "<br/><a href='index.php'>View Result</a>";
} 
}
?>
</body>
</html>

我想不出来。。。我已经确保这些值是从HTML表单页面传递过来的。我已经能够让删除页面正常工作了。“mysqli_查询(insert-into)”只是没有将值插入数据库。

共有2个答案

谷越
2023-03-14

我只是想回答,多亏了echo mysqli_错误($mysqli),我才明白了这一点,这是一个非常好的derp。我只是没有在insert中加入reslink,因为我没有为它指定默认值,它就停在那里了。我一把它添加到查询中,它就工作了。

谭翔
2023-03-14

如果你的ELSE工作,你的MYSQL连接工作比查询将。

mysqli_query($mysqli, "INSERT INTO sites(
    `sitenumber`,
    `videolink`,
    `daynight`,
    `maxtents`,
    `maxpersons`,
    `geography`,
    `VIEW`,
    `forestcover`,
    `waterfront`,
    `firepit`,
    `description`
)
VALUES(
    '$sitenumber',
    '$videolink',
    '$daynight',
    '$maxtents',
    '$maxpersons',
    '$geography',
    '$view',
    '$forestcover',
    '$waterfront',
    '$firepit',
    '$description'
)";
 类似资料:
  • 我必须向MySQL数据库发送一个INSERT查询,假设所有变量都设置正确,我使用以下代码: 变量被正确设置为result,正如我在使用时看到的那样,但现在在数据库中插入了行。 奇怪的是,如果调用MySQL错误日志,可以清楚地看到insert被正确地发送到数据库,如下所示: 但它没有出现在表格中。 有什么建议吗?谢谢

  • 在插入数据库后,我一直在尝试重定向,但它不起作用。插入工作正常,但没有重定向。这是我的代码

  • 我刚刚完成了一门关于python for Data Science的在线课程。我想在Pycharm edu中编写一些自己的代码。我以前也这么做过,一切都很顺利。我在Linux Mint中通过Pycharm和控制台下载了“numpy”和“matplotlib”。当我导入numpy并导入matplotlib(并且不编写其他内容)时,我的程序运行正常,但是当我编写“import matplotlib.p

  • 我试图一次将多行插入数据库。 用户最多可以添加5个新行,以便将血液结果插入数据库。 有一个添加更多按钮允许他们这样做。 我有一个循环来遍历每个条目,但是当我单击submit时,只会提交一个条目。第一,我不确定我哪里出了问题,任何帮助都将不胜感激。 循环在这个代码块的底部,但我不确定这就是问题所在。

  • 我正在使用hibernate jpa执行批处理更新。 更新:我得到了解决方案:问题是我正在刷新已经刷新的事务,因此它没有给我任何事务正在进行中的错误以及我上面发布的错误,所以我只是删除了`getem().flush();和getEm().clear();从finally块开始工作:)

  • 我写了这个触发器,但它不工作,我执行了这个错误: 11:24:11创建触发器check_venduti在INSERT ON venduti之后,对每一行开始SELECT count(*)作为num_rows FROM prodotto(其中COD_PRODOTTO=New.COD_PRODOTO如果numrows<0,则发出“Error”信号,结束错误代码:1064)。您的SQL语法有错误;检查与