无法将数据插入mysql数据库

时间:2019-11-05 02:39:52

标签: php mysql

我下面的代码假设是将一些信息插入到mysql数据库中。出于某种原因,每次我测试它时,都会收到错误消息,表明它无法执行。一切看起来都应该对我有用。我在这里缺少什么吗?

<?php
      include("phpconnect.php");

      $name = $_GET["name"];
      $date = $_GET["date"];
      echo $name;
      echo $date;

      $sql = "INSERT INTO main (name, visits, visitDate, lastVisit)
      VALUES ('$name', '1', '$date', '$date')";


      if (mysqli_query($conn, $sql))
    {
        echo "Records added successfully.";
    }
    else
    {
        echo "ERROR: Could not execute $sql. "
            .mysqli_error($conn);
    }

    mysqli_close($conn);
    ?>

3 个答案:

答案 0 :(得分:0)

也许,您应该构建稍微不同的SQL语句。您总是可以抛出错误消息,更适合概览-

$sql = "INSERT INTO main (name, visits, visitDate, lastVisit)
  VALUES (?, 1, ?, ?)";

if($stmt = $mysqli->prepare($sql)){
    $stmt->bind_param('sss', $name, $date, $date);

    if (!$stmt->execute()) {
        return false;
// or print error message
    } else {
        return true;
} else {
    return false;
}

或者检查一下-MySQL INSERT INTO with PHP $variable

答案 1 :(得分:-1)

尝试类似这样的方法。该函数可以准确地插入到我的数据库中,也可以抓取SQL注入。

function addRestaurant() {
    if(isset($_POST['submit'])) {
        global $connection;
        $name = $_POST['name'];
        $address = $_POST['address'];
        $city = $_POST['city'];
        $state = $_POST['state'];       
        $zipcode = $_POST['zipcode'];
        $googlemapslink = $_POST['googlemapslink'];
        $restauranttype = $_POST['restauranttype'];
        $website = $_POST['website'];
        $logo = $_POST['logo'];
        $sitelink = $_POST['sitelink'];

        if ($googlemapslink == "") {
            $googlemapslink = "https://youtu.be/dQw4w9WgXcQ";
        }

        if ($website == "") {
            $website = "https://youtu.be/dQw4w9WgXcQ";
        }

        if ($logo == "") {
            $logo = "https://youtu.be/dQw4w9WgXcQ";
        }

        $name = mysqli_real_escape_string($connection, $name);
        $address = mysqli_real_escape_string($connection, $address);
        $city = mysqli_real_escape_string($connection, $city);
        $state = mysqli_real_escape_string($connection, $state);
        $zipcode = mysqli_real_escape_string($connection, $zipcode);
        $googlemapslink = mysqli_real_escape_string($connection, $googlemapslink);
        $restauranttype = mysqli_real_escape_string($connection, $restauranttype);
        $website = mysqli_real_escape_string($connection, $website);
        $logo = mysqli_real_escape_string($connection, $logo);
        $sitelink = mysqli_real_escape_string($connection, $sitelink);

        $query = "INSERT INTO `restaurants` (Name, Address, City, State, ZipCode, GoogleMapsLink, Website, RestaurantType, RestaurantLogo, SiteLink) ";
        $query .= "VALUES (";
        $query .= "'$name', ";
        $query .= "'$address', ";
        $query .= "'$city', ";
        $query .= "'$state', ";
        $query .= "'$zipcode', ";
        $query .= "'$googlemapslink', ";    
        $query .= "'$website', ";
        $query .= "'$restauranttype', ";
        $query .= "'$logo', ";
        $query .= "'$sitelink'); ";

        $filesite = "restaurants/" . $sitelink;
        $file = "restaurants/menu.php";
        $contents = file_get_contents($file);

        file_put_contents($filesite, $contents);

        $result = mysqli_query($connection, $query);
            if(!$result) {
                die("Query failed." . mysqli_error($connection));
            } else {
                echo "Record updated!";
            }
    }
}

答案 2 :(得分:-1)

  • 首先检查您的datbase连接
  • 第二次检查您的表单方法GET或POST,然后应用
  • 检查表列名称

include(“ phpconnect.php”);

if(isset($ _ POST ['submit'])){

$name = $_POST["name"];
$date = $_POST["date"]; 

$sql = "INSERT INTO main (name, visits, visitDate, lastVisit) VALUES ('$name', '1', '$date', '$date')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}