【问题标题】:could not inserting data to mysql table using php无法使用 php 将数据插入 mysql 表
【发布时间】:2014-08-26 02:02:55
【问题描述】:

我在 html 中有以下表单

<form method="post" enctype="multipart/form-data" />
                <fieldset>
                <legend>Activate Scheme</legend>
                <p>Date Of Draw</p>
                <p><input type="text" name="dateOfDraw" class="textBox" style="width:150px" /></p>

                <p>Time Of Draw</p>
                <p><input type="text" class="textBox" name="timeOfDraw" style="width:150px" /></p>

                <p>Enter scheme name</p>
                <p><input type="text" class="textBox" name="schemeName" style="width:150px" />
                </p>

                <p>Upload Image</p>
                <p><input type="file" name="image" id="image" /></p>

                <p><input name="scheme_button" type="submit" class="button1" value="Submit"></p>
                </fieldset>
        </form>

问题是当我执行以下查询时它们没有错误

<?php 
if(isset($_POST['submit_button']) && count($_POST)>0) {
    print_r($_POST);

    $dateOfDraw = $_POST['dateOfDraw'];
    $timeOfDraw = $_POST['timeOfDraw'];
    $schemeName = $_POST['schemeName'];
    $imageName = $_FILES['image']['name'];

    $destination = '../images/'.$imageName;
    $source = $_FILES['image']['tmp_name'];
    if(move_uploaded_file($source, $destination)) {
        echo 'file uploaded';
    } else {
        echo ' file not uploaded';
    }

    $sSQL = "INSERT INTO landing_page(dateOfDraw, timeOfDraw, schemeName, image) VALUES('$dateOfDraw','$timeOfDraw','$schemeName','$imageName')";
    echo $sSQL;
    if(!$sSQL){
        die(mysqli_query($con));
        }
    mysqli_query($con,$sSQL);

    //header("location: list_products.php");
}
                ?>

但是当我在 mysql 数据库中检查我的数据时,行仍然是空的。请检查我是否遗漏了什么或 php 代码错误。 注意:我使用的是 php 版本 5.5.16

【问题讨论】:

  • 有什么问题@JohnConde 请指出我
  • 您的代码不包含 SQL 转义或错误检查 (mysqli_error)。你至少可以说出print_r 所说的话。
  • @TinyCoder 你把它连接到数据库
  • 在您打开&lt;?php标签error_reporting(E_ALL); ini_set('display_errors', 1);后立即将错误报告添加到文件顶部,看看它是否会产生任何结果。
  • @Utkarsh 是的,我正在连接它,我的所有表格都在工作,接受这个

标签: php html mysql mysqli


【解决方案1】:

您的提交按钮的名称是scheme_button,这正是您需要$_POST

 if(isset($_POST['scheme_button']) && count($_POST) > 0)

你的表单没有动作,也不知道去哪里。所以加一个。

<form method="post" action="index.php" enctype="multipart/form-data" />

【讨论】:

  • 我不想要任何操作,我只想提交我的表单并留在那个页面上
  • @TinyCoder 可能是因为您没有澄清这一点,或者您仍然可以添加 action="" 以留在同一页面上。但是,这部分是正确的对吧? if(isset($_POST['scheme_button']) && count($_POST) > 0)。毕竟,我们很乐意为您提供帮助
  • 请不要生气,这是我的错,我检查你的答案晚了,请不要介意。请让我知道如果不使用 action="" 这有什么缺点。
  • @TinyCoder 看到这个stackoverflow.com/questions/3620340/…。为了额外的学习。还有这个sitepoint.com/forums/…
【解决方案2】:

首先你必须命名提交按钮 if(isset($_POST['scheme_button']) && count($_POST) > 0)

其次,如果在同一页面中插入代码,那么

如果在另一个页面中插入代码然后 动作 = “文件名”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多