In cf7, I am using a sql data action to read from the database. The instructions say I can use php code. I would like to use php to get a value from a form and use it in the sql statements. How can I do this?
For example, I would like to do something like this:
SELECT * FROM Table
WHERE Column = "value retrieved from php".
How do I run the php code inside the sql?
For example, I would like to do something like this:
SELECT * FROM Table
WHERE Column = "value retrieved from php".
How do I run the php code inside the sql?
Add a SQLData in the action section of the form or on the submit page.
Here you write the code by setting the variables that you read from the form fields
For example, in the form I have set two fields named dateFrom and dateTo
You can also use the PHP block (in this case you don't need the php start and end tags), as shown below, but you have to manage the connection to the DB manually
Here you write the code by setting the variables that you read from the form fields
For example, in the form I have set two fields named dateFrom and dateTo
<?php
$from = $this->data("dateFrom");
$to = $this->data("dateTo");
$query = sprintf("SELECT COUNT(*) AS `total` FROM `Cars` WHERE `data_registrazione` BETWEEN '%s' AND '%s';", $from, $to);
echo $query;
?>
You can also use the PHP block (in this case you don't need the php start and end tags), as shown below, but you have to manage the connection to the DB manually
$from = $this->data("dateFrom");
$to = $this->data("dateTo");
$conn = mysqli_connect('localhost:3306', 'mydb', 'usermydb', 'passwordmydb');
if(! $conn) {
die('Could not connect: ' . mysqli_error());
}
$sql = "SELECT COUNT(*) AS total FROM Cars WHERE `data_registrazione` BETWEEN '{$from}' AND '{$to}';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_object($result) ;
// echo $row->total;
// echo '<br>';
// echo $sql;
// echo '<br>';
mysqli_close($conn);
return $row->total;
Thank you. Does your 2nd solution let me connect the 'php' action to the 'table list' view?
Yes, it is possible.
In the DataSources field of the TableList, you must set the name of the PHP module you have created {var:MODULE-PHP-NAME},
for example {var:phpListCars}.
Additionally, in the PHP module, you need to return the entire recordset.
Remember: In the TableList, you must set the Table Columns manually by inserting the aliases you used in the query.
For example:
modello,nome, costo ...
In the DataSources field of the TableList, you must set the name of the PHP module you have created {var:MODULE-PHP-NAME},
for example {var:phpListCars}.
Additionally, in the PHP module, you need to return the entire recordset.
$from = $this->data("dateFrom");
$to = $this->data("dateTo");
$conn = mysqli_connect('localhost:3306', 'mydb', 'usermydb', 'passwordmydb');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error) . "(" . mysqli_connect_error() . ")";
}
$sql = "SELECT model AS modello, name AS nome, cost AS costo FROM Cars WHERE `data_registrazione` BETWEEN '{$from}' AND '{$to}';";
$result = mysqli_query($conn, $sql);
// Array to store the recordset
$recordset = array();
// Check if there are any results
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// Add each row to the recordset
$recordset[] = $row;
}
} else {
echo "No data found!";
}
mysqli_free_result($result);
mysqli_close($conn);
return $recordset;
Remember: In the TableList, you must set the Table Columns manually by inserting the aliases you used in the query.
For example:
modello,nome, costo ...
You need to login to be able to post a reply.