将空字符串保存到DB

时间:2016-02-18 14:52:15

标签: php mysql mysqli

我正在使用此脚本将表单中的数据保存到远程数据库中,但出于某些奇怪的原因,它将其保存为空:

Empty data

脚本是:

   apply plugin: 'com.android.application'
     android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
    applicationId "com.netvariant.heinz"
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
    }
}}dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:23.1.1'
compile 'com.google.android.gms:play-services:8.4.0'}

当我第一次用var style = [ new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(255, 255, 255, 0.2)' }), stroke: new ol.style.Stroke({ color: 'red', width: 2 }) }), new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'green', width: 2 }), geometry: function(feature) { var geom = feature.getGeometry(); var extent = geom.getExtent(); var bottomLeft = ol.extent.getBottomLeft(extent); var bottomRight = ol.extent.getBottomRight(extent); // return a linestring with the second style return new ol.geom.LineString([bottomLeft, bottomRight]); } }) ]; 而不是#FORM SECTION $choice; $email; $checkbox; $success = FALSE; #SQL SECTION $host = '*******'; $user = '*******'; $pass = '*******'; $db = '********'; $connection = new mysqli($host, $user, $pass, $db); $sql = "INSERT INTO clients_and_choices (email,choice) VALUES ('$email','$choice')"; $query = mysqli_query($connection, $sql); if ($_SERVER["REQUEST_METHOD"] == "POST"){ $email = $_POST['email']; $choice = $_POST['choice']; $email = $connection->real_escape_string($email); $choice = $connection->real_escape_string($choice); if (mysqli_connect_errno()) trigger_error(mysqli_connect_errno()); if (!filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($choice)){ http_response_code(400); echo "Houston we got a problem!"; exit; } if ($query){ $success = TRUE; http_response_code(200); echo 'Thank you :D'; } else{ http_response_code(500); echo "Bummer! It seems something went wrong :< If you would be so kind to try again :D"; } mysqli_close($connection); if (isset($_GET["ajax"])) echo $success ? "success" : "error"; } 写它时,它起作用了......也许我做错了什么但是我无法说出来并且我不会做错。知道如何在PHP中调试。

2 个答案:

答案 0 :(得分:2)

就像我在评论中所说:

$sql = "INSERT INTO clients_and_choices (email,choice) VALUES ('$email','$choice')";属于 之后验证了输入并确保它们不为空。还要确保表单确实使用post方法,并且输入带有匹配的名称属性。

您可能也在同一个文件中使用HTML表单和PHP / SQL,这会导致在加载页面后立即在数据库中插入空值。

您还需要删除这些内容,因为没有必要使用它们:

$choice;
$email;
$checkbox;

然后检查输入的空白​​度。

请务必检查错误。

我确实注意到了if (isset($_GET["ajax"]))可能会使用Ajax。

如果你在那里设置了关于要传递给数据库的数据的任何内容,那么这也会导致数据输入过早。

答案 1 :(得分:0)

在声明$ email和$ choice应具有的值之前插入。 因为你有空字符串。现在,在声明和验证变量之后,您的查询和插入位于正确的位置。

#FORM SECTION
$choice;
$email;
$checkbox;
$success = FALSE;

#SQL SECTION
$host = '*******';
$user = '*******';
$pass = '*******';
$db = '********';

$connection = new mysqli($host, $user, $pass, $db);

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $email = $_POST['email'];
    $choice = $_POST['choice'];

    $email = $connection->real_escape_string($email);
    $choice = $connection->real_escape_string($choice);

    if (mysqli_connect_errno())
        trigger_error(mysqli_connect_errno());

    if (!filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($choice)){
        http_response_code(400);
        echo "Houston we got a problem!";
        exit;
    }

    $sql = "INSERT INTO clients_and_choices (email,choice) VALUES ('$email','$choice')";
    $query = mysqli_query($connection, $sql);

    if ($query){
        $success = TRUE;
        http_response_code(200);
        echo 'Thank you :D';
    } else {
        http_response_code(500);
        echo "Bummer! It seems something went wrong :< If you would be so kind to try again :D";
    }

    mysqli_close($connection);

    if (isset($_GET["ajax"]))
        echo $success ? "success" : "error";
}