Everything is Working Fine No Error. But I don't Know Why data is not posted on my database.
I'm new TO flutter so I need these Code Using tutorials. Get Method is Working Fine but Post method not working.
IN localhost code was working Now I hosted this project database on the server
This is my database File. Create.php
<?php
include "db.php";
$name = isset($_POST['name']) ? $_POST['name'] : '';
$desciption = isset($_POST['desciption']) ? $_POST['desciption'] : '';
$addr = isset($_POST['addr']) ? $_POST['addr'] : '';
$image_url = isset($_POST['image_url']) ? $_POST['image_url'] : '';
$price = isset($_POST['price']) ? $_POST['price'] : '';
$stmt = $db->prepare("INSERT INTO house (name, desciption,addr,image_url,price) VALUES (?, ?,?,?, ?)");
$result = $stmt->execute([$name, $desciption,$addr,$image_url,$price]);
echo json_encode($result);
When I'm Clicking on Submit Button on error get is this
API response
I need your support. I am posting the question again because it's not been solved past 8 days
Thanks in Advance
$stmt = $db->prepare("INSERT INTO house (name, desciption,addr,image_url,price) VALUES (?, ?,?,?, ?)");
$result = $stmt->execute([$name, $desciption,$addr,$image_url,$price]);
in this section should also bind the parameters because you are using prepared statements.
$stmt = $db->prepare("INSERT INTO house (name, desciption,addr,image_url,price) VALUES (?, ?,?,?, ?)");
$stmt->bind_param("sssss", $name, $desciption, $addr, $image_url, $price);
// s means string so if the price is not string you can change it to integer (i) or double (d)
$result = $stmt->execute();
If this does not work please change bind_param part to:
$stmt->bindParam(1, $name, PDO::PARAM_STR);
$stmt->bindParam(2, $desciption, PDO::PARAM_STR);
$stmt->bindParam(3, $addr, PDO::PARAM_STR);
$stmt->bindParam(4, $image_url, PDO::PARAM_STR);
$stmt->bindParam(5, $name, PDO::PARAM_STR);
And you have typo in desciption, it should be "description"
Related
I created PHP script in a file and I am unable to get data from PHP
Below is my PHP script file :
function storeUser($name, $email, $password ,$phone) {
$stmt = $this->conn->prepare("INSERT INTO users(name, email, phone ,password, created_at) VALUES(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $name, $email, $phone, $password);
$result = $stmt->execute();
$stmt->close();
}
My Android code is perfectly correct, and I send the parameters. But I can not put the parameter with function into the localhost
Please tell me if you know my problem. Thanks.
Please pay attention to the code you write
In the bind_param section you must delete one of the "s"
Only the number of received parameters from the Android side should be use s.
change
$stmt->bind_param("sssss", $name, $email, $phone, $password);
to
$stmt->bind_param("ssss", $name, $email, $phone, $password);
I'm trying to insert some value into my database, but I got no result, but the code got no error, and the result label said it is succeed. My database connection working. How to check the issue here, I confused.
My Code Here
// insert new data to menu table
$sql_query = "INSERT INTO tbl_jadwal (Nama_Lokasi, Category_ID, Longitude, Latitude, Phone, Email, Menu_image, Description)
VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
$upload_image = 'upload/images/' . $menu_image;
$stmt = $connect->stmt_init();
if ($stmt->prepare($sql_query))
{
// Bind your variables to replace the ?s
$stmt->bind_param('sssssss',
$nama_lokasi,
$category_ID,
$longitude,
$latitude,
$phone,
$email,
$upload_image,
$description
);
// Execute query
$stmt->execute();
// store result
$result = $stmt->store_result();
$stmt->close();
}
This should do, you were missing one s in the param string
$stmt->bind_param('ssssssss',
$nama_lokasi,
$category_ID,
$longitude,
$latitude,
$phone,
$email,
$upload_image,
$description
And you have way too much code. Only a very little part of it is relevant
Im new to PDO, heard that this is the better method to do web applications, and im developing small billing application.
Having one dobut, can i do coding like below?
<?php
require_once '../../classes/PDO_connection.php';
$type = 'initial_stock';
$item_code = $_POST["item_code"];
$category = $_POST["category"];
$variety = $_POST["variety"];
$quantity = $_POST["quantity"];
$price = $_POST["price"];
$f_price = number_format($price, '2', '.', '');
$total = $quantity * $price;
$full_name = $item_code.':'.$category.':'.$variety.':'.$f_price;
$in_stock = $quantity;
$prev_stock = '';
//inserting data from initial stock page
$stmt = $pdo->prepare("INSERT INTO silk (type, item_code, category, variety, quantity, price, full_name, total, in_stock, sale_date, entered_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now())");
$stmt->bindParam(1, $type);
$stmt->bindParam(2, $item_code);
$stmt->bindParam(3, $category);
$stmt->bindParam(4, $variety);
$stmt->bindParam(5, $quantity);
$stmt->bindParam(6, $price);
$stmt->bindParam(7, $full_name);
$stmt->bindParam(8, $total);
$stmt->bindParam(9, $in_stock);
$stmt->execute();
//getting all initial stock for dispaling
$stmt = $pdo->prepare("SELECT * FROM silk WHERE type='initial_stock'");
$stmt->execute();
$rows = $stmt->fetchAll();
foreach($rows as $stock){
echo "<tr class='active'>
<td>".$stock['item_code']."</td>
<td>".$stock['category']."</td>
<td>".$stock['variety']."</td>
<td>".$stock['price']."</td>
<td>".$stock['quantity']."</td>
<td><a id='initial_stock_silk_delete' id_to_delete=".$stock['id'].">Delete</a></td>
</tr>";
}
In mysql, i call the function that has query and return the value, but i thought PDO no need that? am i correct? expecting proffesionals advice.... thanks.
It's entirely up to you. If you want to use a function you already accustomed with - nobody forbids you from creating one.
The only thing you MUST take into account - such a function should accept at least TWO arguments - a query with placeholders and an array with data to bind
this is code not working. ERROR:Parse error: syntax error, unexpected '$age' (T_VARIABLE) in... directory.
$name = trim($_POST['name']);
$age = trim($_POST['age']);
$insert= $db->prepare("INSERT INTO data (age, name, created) VALUES (?, ?, NOW())");
$insert->bind_param('is' $age, $name);
if($insert->execute()){
echo 'record added';
die();
}
I believe you are not following PDO rules to prepare and execute any query.
Refer following Url,
PDO::prepare
I hope this will help you.
you are missing , after Type specification chars( is) :
$insert->bind_param('is' $age, $name);
should be :
$insert->bind_param('is',$age, $name);
All parameters should be separated by ,
You are mixing mysqli code with PDO
$name = trim($_POST['name']);
$age = trim($_POST['age']);
$insert= $db->prepare("INSERT INTO data (age, name, created) VALUES (?, ?, NOW())");
$insert->->bindParam(1, $age, PDO::PARAM_INT);
$insert->->bindParam(2, $name, PDO::PARAM_STR, 12);//Change 12 to suit
if($insert->execute()){
I have been ripping my hair for days over this problem so any helpful advice would be appreciated. Calling the following function returns nothing. The POST values are set (They print with echo) and the database let me update and extract with other functions. What am i missing?
Oh yea, all the values are strings.
$stmt = $db->prepare("INSERT INTO content_page (name, layout, page_id) VALUES (?,?,?)");
$stmt->bind_param("sss", $_POST['name'], $_POST['layout'], $_POST['page_id']);
$stmt->execute();
$stmt->close();
At glance, there is nothing wrong with this code (in case you are indeed using mysqli). So, the only way to get to know what is going wrong is to get the error message.
Add this line before connect
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
and make sure you can see PHP errors
Try this
$sql = "INSERT INTO content_page (name, layout, page_id) VALUES (?,?,?)";
if (!$stmt = $db->prepare($sql)) {
die($db->error);
}
$stmt->bind_param("ssi", $_POST['name'], $_POST['layout'], $_POST['page_id']);
if (!$stmt->execute()) {
die($stmt->error);
}
$stmt->close();
Or, if, as you said, all your values are strings (given, they are as well defined as varchars/something similar in your database), you can still bind_param("sss"...
Aren't page_id's integers ? Since the asker first tagged the question as PDO, here is the PDO version :
$stmt = $db->prepare("INSERT INTO content_page (name, layout, page_id) VALUES (:name,:layout,:pid)");
$sth->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
$sth->bindParam(':layout', $_POST['layout'], PDO::PARAM_STR);
$sth->bindParam(':pid', $_POST['page_id'], PDO::PARAM_INT);
$stmt->execute();
Or (MySQLi):
$stmt = $db->prepare("INSERT INTO content_page (name, layout, page_id) VALUES (?,?,?)");
$stmt->bind_param("ssi", $_POST['name'], $_POST['layout'], $_POST['page_id']);
$stmt->execute();
Or (PDO) :
$stmt = $db->prepare("INSERT INTO content_page (name, layout, page_id) VALUES (?,?,?)");
$stmt->execute(array($_POST['name'], $_POST['layout'], $_POST['page_id']));
Here you are:
$name = $_POST['layout'];
$layout = $_POST['layout'];
$page_id= $_POST['page_id'];
$stmt = $db->prepare("INSERT INTO content_page (name, layout, page_id) VALUES ('".$name."','".$layout."','".$page_id."')");