douyunhuan9886 2018-12-03 19:25
浏览 139
已采纳

PHP INSERT INTO MySQL无法正常工作,没有给出错误

I am currently trying to execute an INSERT INTO when a search button is clicked. Eventually I'd like to submit the search term into the database, however I'm currently just trying to submit a simple string to ensure the initial INSERT INTO works correctly, which it currently doesn't.

I have the following:

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="search" name="search" id="results" placeholder="Enter search">
    <input type="submit" name="submit" class="frstBtn" value="searchterm"><br>
</form>

<?php
        $searchterm = "";
    ?>
<?php
        if(isset($_POST['submit']))
        {
            $sql = "INSERT INTO tableName (variableName) VALUES ('test')";
            $results = mysqli_query($conn,$sql) or die(mysqli_error());
        }
        ?>

The latter php function, search, works perfectly fine, but the former function including the INSERT INTO does not.

Here is the table itself in phpmyadmin.

I have successfully manually copied and pasted the actual INSERT INTO statement into the actual database and it has worked fine, so I know the issue isn't with the statement. Any help is much appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • dpqmu84646 2018-12-03 20:01
    关注

    Since the searchTerm column is defined as PRIMARY, I’m ready to bet that you are trying to insert the same value twice and you don't have enabled the error_reporting. To find out exactly what the problem is, first of all enable all errors by adding the following at the beginning of your script:

    ini_set('error_reporting', -1);
    ini_set('display_errors', 1);
    

    Also, note that you must use mysqli_error($conn) instead of mysqli_error().

    By the way, to ignore errors when inserting unique values, use:

    INSERT IGNORE INTO searchHistory(searchTerm) VALUES ('test')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同