I'm looking to insert the text input provided by user on a webpage in a text input field(comment box) I created using php, to a mongo database, how do I do that?
First of all you have to connect with Mongo Database then create collection and then insert the desire value in json format.
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$inputTextValue= array(
"key" => "value",
);
$collection->insert($inputTextValue);
echo "inputTextValue inserted successfully";
?>
Try this hope this code useful for you
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$document = array(
"field_name" => "Your text input field "
);
$collection->insert($document);
echo "Document inserted successfully";
?>
Related
I created a function to output a table row with data from a JS file. But I'd like to retrieve data from the MySQL database I made instead. Is it possible?
Function:
function outputCartRow(name, id, email, proj_title){
document.write('<tr>');
document.write('<td>'+name+'</td>');
document.write('<td>'+id+'</td>');
document.write('<td>'+email+'</td>');
document.write('<td>'+proj_title+'</td>');
document.write('</tr>');
}
I created a separate form that submits all the textfield input to the database, for reference:
PHP text:
<?php
$conn = mysqli_connect('127.0.0.1:3307', '', '');
if(!$conn){
echo 'Missing server';
}
if(!mysqli_select_db($conn,'fyp')){
echo 'Missing database';
}
$stud_name = $_POST['stud_name'];
$stud_id = $_POST['stud_id'];
$stud_email = $_POST['stud_email'];
$proj_title = $_POST['proj_title'];
$sql = "INSERT INTO student_assignment(stud_name, stud_id, stud_email, proj_title) values ('$stud_name', '$stud_id', '$stud_email', '$proj_title')";
if(!mysqli_query($conn, $sql)){
echo 'Update error';
}
else{
echo 'Update successful';
}
header("refresh:2; url=supervisor_pa.html");
?>
Is there a way to retrieve all the data and refer it to the function?
I wan to insert data to mysql table from another database which is connected via ODBC.But I cannot enter into the while loop, here is my code -
N.B: For security I dont provide db name, user and pass.
ODBC connection declared as 'connStr'
<?php
$connStr = odbc_connect("database","user","pass");
$conn = mysqli_connect("server","user","pass","database");
//$result_set=mysqli_query($conn,$datequery);
//$row=mysqli_fetch_array($result_set);
echo "<br>";
echo "<br>";
$query="select cardnumber, peoplename, creditlimit, ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
where cardnumber not like '5127%'" ;
$rs=odbc_exec($connStr,$query);
$i = 1;
while(odbc_fetch_row($rs))
{ //echo "Test while";
$cardnumber=odbc_result($rs, "cardnumber");
$peoplename=odbc_result($rs, "peoplename");
$creditlimit=odbc_result($rs, "creditlimit");
$cbalance=odbc_result($rs, "cbalance");
$minpay=odbc_result($rs, "minpay");
$conn = mysqli_connect("server","user","pass","database");
$sql= "INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay) VALUES ('cardnumber', 'peoplename', 'creditlimit', 'cbalance', 'minpay') ";
if(!(mysqli_query($conn,$sql))){
//echo "Data Not Found";
echo "<br>";
}
else{
echo "Data Inserted";
echo "<br>";
}
echo $i++ ;
}
echo "<br>";
odbc_close($connStr);
?>
How can I solve this?
If you really want to understand why you can't enter while() {...}, you need to consider the following.
First, your call to odbc_connect(), which expects database source name, username and password for first, second and third parameter. It should be something like this (DSN-less connection):
<?php
...
$connStr = odbc_connect("Driver={MySQL ODBC 8.0 Driver};Server=server;Database=database;", "user", "pass");
if (!$connStr) {
echo 'Connection error';
exit;
}
...
?>
Second, check for errors after odbc_exec():
<?php
...
$rs = odbc_exec($connStr, $query);
if (!$rs) {
echo 'Exec error';
exit;
}
...
?>
you can do it with just SQL check it here, like:
INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay)
SELECT cardnumber, peoplename, creditlimit,
ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
where cardnumber not like '5127%'
I just wanted to create PDO connection rather than old connection but I can't get form fields to be inserted into MYSQL DB. I am sure I am making a stupid mistake however I can't figure it out what exactly.
Here is my database.php file
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'dbaname';
try{
$conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
die( "Connection failed: " . $e->getMessage());
}
and after submit action is taking place at my process.php
<?php
if (!isset($_SESSION)) session_start();
if(!$_POST) exit;
require 'database.php';
include dirname(__FILE__).'/settings/settings.php';
include dirname(__FILE__).'/functions/emailValidation.php';
$TechName = strip_tags(trim($_POST["TechName"]));
$Date = strip_tags(trim($_POST["Date"]));
$ToolSerial = strip_tags(trim($_POST["ToolSerial"]));
$CartridgeSerial = strip_tags(trim($_POST["CartridgeSerial"]));
$TorqueSerial = strip_tags(trim($_POST["TorqueSerial"]));
$LastCalibration = strip_tags(trim($_POST["LastCalibration"]));
$ThreadCond = strip_tags(trim($_POST["ThreadCond"]));
$HardfacingCond = strip_tags(trim($_POST["HardfacingCond"]));
$PocketCond = strip_tags(trim($_POST["PocketCond"]));
//$emailaddress = strip_tags(trim($_POST["emailaddress"]));
/*------------------ STEP 2 ------------------*/
$TorquedOEM = strip_tags(trim($_POST["TorquedOEM"]));
$FullAssembly = strip_tags(trim($_POST["FullAssembly"]));
//$file_url = strip_tags(trim($_POST["file_url"]));
$Notes = strip_tags(trim($_POST["Notes"]));
/*------------------ STEP 3 ------------------*/
//$Signature = strip_tags(trim($_POST["Signature"]));
$SignedDate = strip_tags(trim($_POST["SignedDate"]));
try {
$q = "INSERT INTO tportal (TechName, Date, ToolSerial, CartridgeSerial, TorqueSerial, LastCalibration, ThreadCond, HardfacingCond, PocketCond, TorquedOEM, FullAssembly, Notes, SignedDate)
VALUES (:TechName, :Date, :ToolSerial, :CartridgeSerial, :TorqueSerial, :LastCalibration, :ThreadCond, :HardfacingCond, :PocketCond, :TorquedOEM, :FullAssembly, :Notes, :SignedDate)";
$query = $conn -> prepare($q);
$results = $query -> execute(array(
":TechName" => $TechName,
":Date" => $Date,
":ToolSerial" => $ToolSerial,
":CartridgeSerial" => $CartridgeSerial,
":TorqueSerial" => $TorqueSerial,
":LastCalibration" => $LastCalibration,
":ThreadCond" => $ThreadCond,
":HardfacingCond" => $HardfacingCond,
":PocketCond" => $PocketCond,
":TorquedOEM" => $TorquedOEM,
":TorqueSerial" => $TorqueSerial,
":FullAssembly" => $FullAssembly,
":Notes" => $Notes,
":SignedDate" => $SignedDate,
));
if ($conn->query($q)) {
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
}
else{
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted. $PocketCond');</script>";
}
$conn = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
After I click on submit button I get a "Data not successfully inserted pass" pass: is variable result from $PocketCondvariable I placed to test the data.
Thank you for your time.
Apparently after refreshing the database I realized my entries were added to the database however my error scripts were creating problem.
if ($conn->query($q)) {
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
}
else{
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted. $PocketCond');</script>";
}
I want to remove a document in mongodb-php. I am accepting an id from the user and using that I want to delete the document but it gives me an error
"Deprecated: MongoCollection::remove(): Passing scalar values for the
options parameter is deprecated and will be removed in the near future
in C:\wamp\www..process.php on line 12".
here is my code
<?php
$m = new mongo();
echo "Connection to database successfully";
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycollection;
echo "Collection selected succsessfully";
$collection->remove(array("Team_ID"=>$_POST['team_id']),false);
echo "Documents deleted successfully";
$cursor = $collection->find();
// iterate cursor to display team_id of documents
echo "Updated document";
foreach ($cursor as $document) {
echo $document["Team_ID"] . "\n";
}
?>
As the error states, it doesn't accept scalar values as a second parameter. Instead, use an array with options (http://php.net/manual/en/mongocollection.remove.php).
$collection->remove(array('Team_ID' => $_POST['team_id']), array('justOne' => false));
As "justOne" is false by default, you can omit the second parameter.
$collection->remove(array('Team_ID' => $_POST['team_id']));
This question already has answers here:
Error: file is encrypted or is not a database
(7 answers)
Closed 5 years ago.
I have an SQLite database and am trying to connect to it with PHP. This is what I'm using:
<?php
$dbconn = sqlite_open('combadd.sqlite');
if ($dbconn) {
$result = sqlite_query($dbconn, "SELECT * FROM combo_calcs WHERE options='easy'");
var_dump(sqlite_fetch_array($result, SQLITE_ASSOC));
} else {
print "Connection to database failed!\n";
}
?>
However, I get this error:
Warning: sqlite_open() [function.sqlite-open]: file is encrypted or is not a database in C:\xampp\htdocs\deepthi\combadd\combadd_db.php on line 4
Connection to database failed!
What's wrong and how can I fix it?
Try to use PDO instead of sqlite_open:
$dir = 'sqlite:/[YOUR-PATH]/combadd.sqlite';
$dbh = new PDO($dir) or die("cannot open the database");
$query = "SELECT * FROM combo_calcs WHERE options='easy'";
foreach ($dbh->query($query) as $row)
{
echo $row[0];
}
$dbh = null; //This is how you close a PDO connection
Connecting To Database
Following PHP code shows how to connect to an existing database. If database does not exist, then it will be created and finally a database object will be returned.
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('combadd.sqlite');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
?>
Now let's run above program to create our database test.db in the current directory. You can change your path as per your requirement. If database is successfully created then it will give following message:
Open database successfully
SELECT Operation
Following PHP program shows how we can fetch and display records
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('combadd.sqlite');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
SELECT * FROM combo_calcs WHERE options='easy';
EOF;
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
echo "ID = ". $row['ID'] . "\n";
}
echo "Operation done successfully\n";
$db->close();
?>
<?php
if ($db = sqlite_open('sampleDB', 0666, $sqliteerror) ) {
$result = sqlite_query($db, 'select bar from foo');
var_dump(sqlite_fetch_array($result) );
} else {
die($sqliteerror);
}
?>
Make sure sqlite support is enable, check phpinfo()
One another solution to your problem is:
Using sqlite3 module instead
class DB extends SQLite3
{
function __construct( $file )
{
$this->open( $file );
}
}
$db = new DB( 'sampleDB.sqlite' );