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);
Related
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"
The code I have for the function trying to insert the data into the table is
function registerDiet(){
global $connect, $meat, $seafood, $salad, $name, $username, $age, $email, $password, $hash;
$statement = mysqli_prepare($connect, "SELECT user_id FROM User WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $colUserID);
$statement = mysqli_prepare($connect, "INSERT INTO Diet (user_id, meat, seafood, salad) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "iiii", $colUserID, $meat, $seafood, $salad);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
}
The function is called towards the end of the code.
user_id is the primary key in the User table and a foreign key in the Diet table, the relationship has been configured and it works fine when using phpMyAdmin. Meat, seafood and salad field types are all boolean (tinyint) in the database table.
For example when I use
INSERT INTO `Diet` (`user_id`, `meat`, `seafood`, `salad`) VALUES ('46', '0', '0', '0');
in phpMyAdmin it works, anyone able to advise?
You have to close the first statement before opening another one.
function registerDiet(){
global $connect, $meat, $seafood, $salad, $name, $username, $age, $email, $password, $hash;
$statement = mysqli_prepare($connect, "SELECT user_id FROM User WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $colUserID);
mysqli_stmt_close($statement);
$statement = mysqli_prepare($connect, "INSERT INTO Diet (user_id, meat, seafood, salad) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "iiii", $colUserID, $meat, $seafood, $salad);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
}
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
This is my current statement. Everything was working fine until I added the key
Key is just a generated hash for the user to activate the account.
$stmt = $mysqli->prepare("INSERT INTO Account (accountUsername,accountPassword,accountEmail,accountActivate,accountKey) VALUES (?, ?, ?,?,?)");
$stmt->bind_param('sssiss', $username, $newPassword, $email,0,$key,time());
When I'm doing this code I'm getting an error.
Cannot pass parameter 5 by reference
Do you know what could be the issue?
Thanks!
Edit Code:
$stmt = $mysqli->prepare("INSERT INTO Account (accountUsername,accountPassword,accountEmail,accountActivate,accountKey,accountCreated) VALUES (?, ?, ?,?,?,?)");
$stmt->bind_param('sssisi', $username, $newPassword, $email,0,$key,$time);
http://i.stack.imgur.com/Th5tl.png
If you use bind_param that 0 needs to be in a variable since bind_param passes by reference.
$somevar=0;
$stmt = $mysqli->prepare("INSERT INTO Account (accountUsername,accountPassword,accountEmail,accountActivate,accountKey) VALUES (?, ?, ?, ?,?,?)");
$stmt->bind_param('sssiss', $username, $newPassword, $email,$somevar,$key,$time);
I've been sitting on the same small problem now for over 10 hours, so it's time to ask stackoverflow! I'm connected to the database but when calling mysqli_stmt_bind_param I get "invalid object or resource".
I've tried the insert statement in the console and it works fine..
<?php
$con=mysqli_connect("127.0.0.1:3306", "myUsername", "password");
mysqli_select_db($con, "webshop");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query= mysqli_stmt_init($con);
mysqli_stmt_prepare($query, "INSERT INTO user (name, email, hash, address, tel) VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($query, "ssssi", $name, $email, $hash, $address, $tel);
if(mysqli_stmt_execute($query))
{
mysqli_close($con);
}
?>
Thankful for any help at all!
You have to use the statement object returned by mysqli_stmt_prepare()
$stmt = mysqli_stmt_prepare($con, "INSERT INTO user (name, email, hash, address, tel) VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, "ssssi", $name, $email, $hash, $address, $tel);
if(mysqli_stmt_execute($stmt))
Also, the mysqli_stmt_init($con) call is not needed (I think).
mysqli_stmt_init is needed as you are accessing mysqli using the procedural style.
This returns an object of type mysqli_stmt, which then acts as a container for the query you are building. As such, you should pass this as the first parameter to mysqli_stmt_prepare, mysqli_stmt_bind_param and mysqli_stmt_execute.
So your code would look like:
<?php
$con=mysqli_connect("127.0.0.1:3306", "myUsername", "password");
mysqli_select_db($con, "webshop");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$stmt = mysqli_stmt_init($con);
$query = "INSERT INTO user (name, email, hash, address, tel) VALUES (?, ?, ?, ?, ?)";
mysqli_stmt_prepare($stmt, $query);
mysqli_stmt_bind_param($stmt, "ssssi", $name, $email, $hash, $address, $tel);
if(mysqli_stmt_execute($stmt))
{
mysqli_close($stmt);
}
?>
One, unrelated point - you appear to be requiring that your tel field (which I presume to be a telephone number) is an integer. This might be a bad idea if you have to handle telephone numbers starting with 0 (common in the UK for example) at any point.