php不要将数据保存在mysql数据库中

时间:2018-10-15 07:49:00

标签: php mysql

我是android的新手,我把代码告诉我我错了 php不要将mysql数据库中的任何数据保存在本地主机(wamp服务器)中

这是connect.php代码:

<?php

$server="127.0.0.1";
$user="root";
$pass="";
$dbname="mydb";
$dsn="mysql:host=$server;port=3306;dbname=$dbname";
try{
$connect=new PDO($dsn,$user,$pass);
$connect->exec("set character_set_connection='utf8'");
$connect->exec("set name='utf8'");

if($connect){
echo "the connection is fine";
}
}catch(PDOException $error){

echo"unable to connect".$error->getmessage();

}

这是插入表的代码

<?php
include "connect.php";

if (isset($_POST["add"])){

$name=$_POST["name"];
$des=$_POST["des"];
$price=$_POST["price"];
$cat=$_POST["cat"];
$image=$_POST["image"];


$query_insert="insert into cell (name,des,price,cat,image) values (:name,:des,:price,:cat,:image);";
$result=$connect->prepare($query_insert);
$result->bindparam(":name",$name);
$result->bindparam(":des",$des);
$result->bindparam(":price",$price);
$result->bindparam(":cat",$cat);
$result->bindparam(":image",$image);

$result->execute();
echo mysql_error();
echo"yes";
}

?>

2 个答案:

答案 0 :(得分:-1)

connect.php代码

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn_status = "Connected successfully"; 
    }
catch(PDOException $e)
    {
    $conn_status = "Connection failed: " . $e->getMessage();
    }

然后插入页面代码

<?php
include "connect.php";

//clean user input (https://stackoverflow.com/a/51955492/4236404)
function clean_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
} // end of clean function

if(isset($_POST["add"])) {
  //clean the user input
  $name = clean_input($_POST["name"]);
  $des = clean_input($_POST["des"]);
  $price = clean_input($_POST["price"]);
  $cat = clean_input($_POST["cat"]);
  $image = clean_input($_POST["image"]);
  //prepare sql and bind parameters
  $stmt = $conn->prepare("INSERT INTO yourTable (name, des, price, cat, image) VALUES (:name, :des, :price, :cat, :image)");
  $stmt->bindParam(':name', $name);
  $stmt->bindParam(':des', $des);
  $stmt->bindParam(':price', $price);
  $stmt->bindParam(':cat', $cat);
  $stmt->bindParam(':image', $image);
  //insert the data
  if($stmt->execute()) {
    echo "Inserted Successfully." 
  } // end of insert

} // end of Post

如果插入不成功,则可能是因为您的表单$_POST[add]尚未设置。

答案 1 :(得分:-2)

尝试运行以下脚本。我创建的 PDO try/catch Gist是为了帮助解决此类问题。它不会解决您的问题,但是会告诉您问题是什么。使用return语句回复此评论,我们可以从那里获取它。

if (isset($_POST["add"])){

try {
    $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // Add PDO query below here.
    $name=$_POST["name"];
    $des=$_POST["des"];
    $price=$_POST["price"];
    $cat=$_POST["cat"];
    $image=$_POST["image"];


    $query_insert="insert into cell (name,des,price,cat,image) values (:name,:des,:price,:cat,:image);";
    $result=$connect->prepare($query_insert);
    $result->bindparam(":name",$name);
    $result->bindparam(":des",$des);
    $result->bindparam(":price",$price);
    $result->bindparam(":cat",$cat);
    $result->bindparam(":image",$image);

    $result->execute();

        if($result->execute()){
            $arr = $result->errorInfo();
            print_r($arr);
        }

}
catch (PDOException $e) {
    print_r($e);
}

}