简体   繁体   中英

PHP database SQL insert

I am trying to write data form mine app to a external database. I just get no response form my PHP page. When I look at the variables that I send to the PHP page, they are received good and nothing goes wrong at that moment. But when I do an INSERT with SQL it goes wrong. (I think). When I go to mine PHPadmin page and I do next SQL command, it works:

INSERT INTO images (FBid,Datum,Lat,Longi,Image) 
VALUES ('1846465164',
'2016-08-25 14:14:15',10.5,5.69,'/9j/
 4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE
 BAQEBQBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB')

So i have next database;

ID(PRIMARY KEY AUTOINCREMENT), 
FBid (varchar(255)), 
Datum (datetime), 
Lat (Double), 
Longi(Double), 
Image(Blob).

And this is my php page:

<?php



 if($_SERVER['REQUEST_METHOD']=='POST'){

   define('HOST','localhost');
 define('USER','XXXXXXXXX');
 define('PASS','XXXXXXXXX');
 define('DB','database2');

 $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

 $image = $_POST['image'];
 $FBid = $_POST['FBid'];
 $date = $_POST['Date'];
 $long = $_POST['long'];
 $lat = $_POST['lat'];

 $stmt = $con->prepare(
                "INSERT INTO images (FBid,Datum,Lat,Longi,Image) 
                 VALUES (:Fbid,:date,:lat,:long,:image)"); 
        $stmt->bindParam(":Fbid",$FBid);
        $stmt->bindParam(":date", $date);
        $stmt->bindParam(":lat", $lat);
        $stmt->bindParam(":long", $long);
        $stmt->bindParam(":image","s",$image);
        $stmt->execute();

$check = mysqli_stmt_affected_rows($stmt);

 if($check == 1){
 echo "Image Uploaded Successfully";
 }else{
 echo "Error Uploading Image";
 }
 mysqli_close($con);
 }else{
 echo "Error";
 }

Thank you guys!

Regards, Stijn

Looking at the database connection, you are using mysqli prepare wrongly. In the INSERT statement, it looks like a PDO version. If you want to use PDO version, have a look at this link . You can't mix PDO and mysqli. The procedural style for mysqli_prepare is like below:

$stmt = mysqli_prepare($con, "INSERT INTO images VALUES (?, ?, ?, ?, ?)");
if ( !$stmt ) {
   die('mysqli error: '.mysqli_error($con);
}
mysqli_stmt_bind_param($stmt, 'ssddb', $FBid,$date,$lat,$long,$image);
if ( !mysqli_stmt_execute($stmt)) {
   die( 'stmt error: '.mysqli_stmt_error($stmt) );
}

$check = mysqli_stmt_affected_rows($stmt);
if($check == 1){
 echo 'Image successfully uploaded';
}else{
 echo 'Error uploading image';
}
mysqli_stmt_close($stmt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM