我无法将数据添加到我的数据库

时间:2016-09-22 04:10:08

标签: php

Godd之夜。我有这个代码用于PHP将数据添加到我的数据库,但我没有成功。

 <?php
require("config.inc.php");
if (!empty($_POST)) {
$user = $_POST['User'];
$mail = $_POST['Mail'];
$token = $_POST['Token'];
$pass = $_POST['Pass'];
$result = mysqli_query($con,"SELECT 1 FROM Proteos where
        User='$user'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data==0){
    echo $data;
    echo "Hey, un grato saludo mister ".$user."!\n";
    $query = "INSERT INTO Proteos (User, Mail ) VALUES ($user,$mail ) ";
}
mysqli_close($con)

这是我的config.inc

<?php 
define('DB_SERVER','mysql.smartfreehosting.net'); 
define('DB_NAME','u178665800_prote'); 
define('DB_USER','u178665800_carin'); 
define('DB_PASS','xxxxxx'); 
$con = mysql_connect(DB_SERVER,DB_USER,DB_PASS); 
mysql_select_db(DB_NAME,$con); 
?>

4 个答案:

答案 0 :(得分:0)

必须在引号中传递字符串值。也执行查询。 调试使用mysqli_error

$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS, DB_NAME);

连接mysqli(ref)更改config.inc

{{1}}

答案 1 :(得分:0)

将您的价值放在单引号而不是执行查询

$query = "INSERT INTO Proteos (User, Mail ) VALUES ($user,$mail ) ";

$query = "INSERT INTO Proteos (User, Mail ) VALUES ('$user','$mail') ";

之后$query传递给mysqli_query

mysqli_query($con,$query);

答案 2 :(得分:0)

<?php
require("config.inc.php"); 


  if (!empty($_POST)) {
    $user = $_POST['User'];
    $mail = $_POST['Mail'];
    $token = $_POST['Token'];
    $pass = $_POST['Pass'];
$result = mysqli_query($con,"SELECT 1 FROM Proteos where
        User='$user'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data==0){
    echo $data;
    echo "Hey, un grato saludo mister ".$user."!\n";
    mysqli_query($con,"INSERT INTO Proteos (User, Mail ) VALUES ('$user','$mail' ) ") or die(mysqli_error());
}
mysqli_close($con);

检查上面的代码,使用'var'插入值,如'$user','$mail' 查询中显示的任何错误都有助于查找代码or die(mysqli_error())

答案 3 :(得分:0)

您正在将VARCHAR (string)数据插入数据库而不使用单引号。

没有单引号的数据被视为表/字段名称或整数或关键字。

您输入的数据不会导致错误。

更正了SQL:

$query = "INSERT INTO Proteos (User, Mail ) VALUES ('$user','$mail') ";