-4

i'm new in android i'll put my code tell me please what i am wrong php dont save any data im mysql database in localhost(wamp server)

here is connect.php code:

<?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();

}

here is the code for insert into table

<?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";
}

?>
Mitra
  • 3
  • 6

2 Answers2

-2

Try running the below script. It's a PDO try/catch Gist I've created to help catch such issues. It won't fix your problem but it'll tell you what the problem is. Reply to this comment with the return statement and we can take it from there.

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);
}

}

Niall Lonergan
  • 840
  • 1
  • 7
  • 27
  • 2
    `It won't fix your problem` - then this should not be posted as answer, but you should link to your gist in comments or so – Marcin Orlowski Oct 15 '18 at 08:18
  • For the purposes of making it easy for the poster I edited the Gist and put it in as an answer. It may very well point out the cause and help in fixing the issue. Don't get caught up on the use of "It won't fix your problem". I only say that to indicate that it is not a fix but a step in fixing. Just as important. – Niall Lonergan Oct 15 '18 at 08:22
  • 1
    [Answer guide](https://stackoverflow.com/help/how-to-answer) - This clearly states that `Any answer that gets the asker going in the right direction is helpful`, it does meet the requirements of an answer but maybe could've been put as a comment instead as it doesn't directly solve the asker's problem. Anywho, this doesn't violate the rules. – Sanguinary Oct 15 '18 at 08:29
  • its my errors PDOException Object ( [message:protected] => SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens [string:Exception:private] => [code:protected] => HY093 [file:protected] => C:\wamp64\www\test\insert.php [line:protected] => 23 [trace:Exception:private] => Array ( [0] => Array ( [file] => C:\wamp64\www\test\insert.php [line] => 23 [function] => execute [class] => PDOStatement [type] => -> [args] => Array ( ) ) ) [previous:Exception:private – Mitra Oct 15 '18 at 08:31
  • Ok so now we know that the cause of your issue the number of bound variables doesn't match. Try printing each variable. It's likely one is empty. – Niall Lonergan Oct 15 '18 at 08:37
  • After you've tried printing the variables ($name, $des) etc, check your database parameters to see if entries are Not NULL and amend accordingly. – Niall Lonergan Oct 15 '18 at 08:43
  • this is the last error Array ( [0] => 00000 [1] => [2] => ) – Mitra Oct 15 '18 at 09:06
  • Ok, that means that the query was successful. Have you checked the DB? Also, I'd recommend using bindvalue instead of bindparam. You'll run in to less issues. There's lots of documentation on the difference. – Niall Lonergan Oct 15 '18 at 09:11
  • only 2 fields save in database and if i bind the other fields it gets error field one and two – Mitra Oct 15 '18 at 17:14
  • Well at least they're going in now and you know the remaining issue is likely the data types accepted by DB. – Niall Lonergan Oct 15 '18 at 23:22
-2

connect.php code

<?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();
    }

And then the insert page code

<?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

If the insert is not successful then the problem will lie with your form $_POST[add] is probably not set.

Erik Thiart
  • 356
  • 5
  • 16