MySQL Query not working with PHP Variable? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why is this working:
$sql_query = "SELECT * FROM Content WHERE id IN (1,5,7,9)";
But this isn't:
$array_values = "1,5,7,9";
$sql_query = "SELECT * FROM Content WHERE id IN ('$array_values')";
I want to select data from the database based on the integers in the $array_values string.
How can I do it?

because there are ` s in your code here
$sql_query = "SELECT * FROM Content WHERE id IN ('$array_values')";
use :
$sql_query = "SELECT * FROM Content WHERE id IN ( $array_values )";
or
$sql_query = "SELECT * FROM Content WHERE id IN (".$array_values.")";

Related

SQL not providing proper result. (Using PHP) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So here I have a table of Products which contains ID, Name, Type & Price
Now I'm running a function to give me the minimal price of specific product types.
The datatype of Price is Price int(5,2)
function MinPrice($connection) {
echo "Minimum price: <br>";
$sql = "SELECT Type, min(Price)
FROM Products
GROUP BY Type;";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_assoc($result)) {
//Printing the result
echo $row['Type'].' || '.$row['Price'].'<br>';
}
}
The code works but not properly. I get the names of the product types but for the price, it gives me an error.
Undefined index: Price
Can anyone please help me out with this?
Try this:
$sql = "SELECT Type, min(Price) as Price
FROM Products
GROUP BY Type;";

Why isn't `INSERT ROW` working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have an SQL piece of code that every time a user inserts a name creates a new column with the name (and this part works fine). I also want it to add a row to that column with the respective comment given by the user. I don't see why this add row statement won't work.
$value = mysql_real_escape_string($_POST['name']);
$comment = mysql_real_escape_string($_POST['comment']);
$add = mysql_query("ALTER TABLE Names ADD $value Text NOT NULL");
$sql = mysql_query("INSERT INTO Names VALUES $comment");
Try with
$sql = mysql_query("INSERT INTO Names (`$value`) VALUES ('$comment')") or die(mysql_error());
You need to specify the column list.
Ideally you should start using PDO or an ORM to be able to work and debug easier
Try this:
$value = mysqli_real_escape_string($db, $_POST['name']);
$comment = mysqli_real_escape_string($db, $_POST['comment']);
$add = mysqli_query($db, "ALTER TABLE Names ADD '{$value}' Text NOT NULL");
$sql = mysqli_query($db, "INSERT INTO Names({$value}) VALUES('{$comment}')");

PDO multiple queries in php /sql [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the following code. I am grabbing values from a form, and using those values to try to update "customers" and "workorder". The "name" value will be duplicate across the customers and workorder table. I keep on getting the error "number of bound variables does not match number of tokens". I am totally new to using PDO, and am unsure on how to proceed. Any ideas?
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE customers set name = ?, email = ?, mobile =? WHERE id = ?; UPDATE workorder set name = ?;";
$q = $pdo->prepare($sql);
$q->execute(array($name,$email,$mobile,$id));
Database::disconnect();
header("Location: index.php");
You have 4 variables here:
$q->execute(array($name,$email,$mobile,$id));
Should be 5:
$q->execute(array($name,$email,$mobile,$id, $name));

How to store json string in mysql using php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to store my JSON string into database but when i give json string it is not working but when i give simple value it work
this is what i am doing
// $data contain json string
// info is a TEXT type in mysql
$q = "Update user set info = $data where userid = $id";
$sql= $this->db->query($q);
You should really check your error log or enable display_errors to see where the query is failing, but my guess is you probably just need to wrap the data field in quotes. Try this:
// $data contain json string
// info is a TEXT type in mysql
$q = "Update user set info = '$data' where userid = $id";
$sql= $this->db->query($q)
You are missing the quotes around $data and $id. And you need to close the string before concatenating variables, like this:
$q = "Update user set info = '". $data."' where userid = '". $id."'";
$sql= $this->db->query($q);

selcet from mysql using php variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
How can I use a variable inside a mysql select from statment? like so:
$db = $_POST['db'];
$query = "SELECT * FROM $db..";
To literally answer the question:
$db = $_POST['db'];
$query = "SELECT * FROM {$db}..";
OR
$query = "SELECT * FROM {$_POST['db']}..";
OR
$query = "SELECT * FROM ".$_POST['db']."..";
As others have said, accepting unsanitized input from the POST is a very bad idea indeed
at the very least you should do the following
$db = $mysqli->real_escape_string($_POST['db']);
which will atleast ensure that other commands will not be inserted such as INSERTS, UPDATES, or GRANT

Categories