Any ideas on Exporting HTML5 Form data to CSV please?
I can only use HTML5 or Javascript or both.
Thanks
You could just display the form data in CSV format in the browser (maybe in a popup) and have the user do a File -> Save Page (assuming it doesn't have to be fully automated).
OK, this is what I'm going to have to do: 1. If there IS internet connection ... then save the data to MySQL Table. 2. Using PHP run a script on the MySQL DB to export the data to CSV.
I'll use something like this for example:
<?php
$host = 'localhost';
$user = 'mysqlUser';
$pass = 'myUserPass';
$db = 'myDatabase';
$table = 'products_info';
$file = 'export';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
Related
I am creating a database to make my 'PHP' website but I couldn't do this. My website is cruzapp that is related to rideshare companies and changing it in to php to get details about our users. But I can't connect MYSQL by using the following PHP code:
?php
$username = "name";
$password = "password";
$hostname = "host";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Not connected to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("examples",$dbhandle)
or die("Could not select examples");
//execute the SQL query and return records
$result = mysql_query("SELECT id, model,year FROM cars");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:"$row{'id'}." Name:".$row{'model'}."Year: ". //display the results
$row{'year'}.<br>";
}
//close the connection
mysql_close($dbhandle)
?>
Can anyone help me to debug this code?
I will be very thankful to you.
Try this one out. It uses MySQLi with error echoing.
<?php
$username = "name";
$password = "password";
$hostname = "host";
$database = "examples";
$con = mysqli_connect($hostname, $username, $password, $database);
if (!$con) {
exit("Connection failed: " . mysqli_connect_error());
}
$result = mysqli_query($con, "SELECT id, model,year FROM cars");
if (mysqli_error($con)) {
exit("Error: " . mysqli_error($con));
}
while ($row = mysqli_fetch_array($result)) {
echo "ID:" . $row['id'] . " Name:" . $row['model'] . "Year: " . $row['year'] . "<br>";
}
mysqli_close($con);
First of all you should not use mysql because with PHP 7 mysql extension does not work anymore. so you must consider to change it to mysqli or PDO. PDO is recommended. Any how for a quick fix $selected = mysql_select_db($dbhandle,"examples") do this and also check all your values like hostname database name table name and make sure there are no mistakes.
i want to fetch data from my database which is linked to the website,i have created a search box,whenever i enter a particular value in search box,it should display all the related content from the database.i have done the following code,it is not fetching the data from the database,it just shows a blank screen.
<?php
define('db_name','njgh');
define('db_user','root');
define('db_password','');
define('db_host','localhost');
session_start();
$link=mysql_connect(db_host,db_user,db_password);
if(!$link)
{
die('couldnot connetc:'.mysql_error());
}
$db_selected=mysql_select_db(db_name,$link);
if(!$db_selected)
{
die('cant connect to db');
}
function clean($str) {
$str = #trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$value = clean($_POST['searchtext']);
$qry = "SELECT * FROM database WHERE Site_ID = '$value'";
$result = mysql_query($qry);
print_r($result);
mysql_query function return resource ID not your data , use mysql_fetch_array, mysql_fetch_object to get data
$value = ($_POST['searchtext']);
$result = mysql_query("SELECT * FROM database WHERE Site_ID = '$value'");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
mysql_* has been removed entirely as of PHP 7.0. Prevent SQL injection and use the mysqli statement class.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "njgh";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$value = $_POST['searchtext'];
/* create a prepared statement */
if($stmt = $conn->prepare("SELECT * FROM database WHERE Site_ID =?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $value);
/* execute query */
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo $row['db_table_field_name'];
}
//or
$row = $result->fetch_assoc();
print_r($row);
} else {
}
/* close statement */
$stmt->close();
}
/* close connection */
$con->close();
?>
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.
Hope this will work for you......
<?php
define('db_name','sports');
define('db_user','root');
define('db_password','');
define('db_host','localhost');
$link=mysqli_connect(db_host,db_user,db_password,db_name);
if(!$link)
{
die('couldnot connect:'.mysql_error());
}
function clean($str) {
$link=mysqli_connect(db_host,db_user,db_password,db_name);
$str = #trim($str);
if(get_magic_quotes_gpc())
$str = stripslashes($str);
return mysqli_real_escape_string($link, $str);
}
if(isset($_POST['searchtext'])){
$value = clean($_POST['searchtext']);
$qry = "SELECT * FROM cricket WHERE id = '$value'";
$qrydb = mysqli_query($link, $qry);
while($row=mysqli_fetch_array($qrydb)){
$name = $row['name'];
}
echo $name ;
}
?>
<body>
<form method="post">
<input type="text" name="searchtext">
<input type="submit" name="submit">
</form>
</body>
I am trying to write a function that will fetch one column from mysql & return it. some how I am not able to write get_hash() function. Further is it safe to use $GLOBALS?
<?php
//Create Database Connection
$dbhost = "localhost";
$dbuser = "developer";
$dbpass = "abc";
$dbname = "abc";
$connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
//Test Connection`enter code here`
if(mysqli_connect_errno()) {
die("Connection Failed" .
mysqli_connect_error() .
"(" . mysqli_connect_errorno() . ")"
);
}
// Query
$query = "SELECT tweet FROM twc_hashtag";
// Fetch Data
function get_hash() {
if ($result=mysqli_query($GLOBALS['connection'], $GLOBALS['query'])){
while ($row=mysqli_fetch_row($result)){
//echo "<tr>";
//echo "<td>" . $row[0] . "</td>";
//echo "</tr>";
return $row[0];
}
}
//Test Query Error
if(!$result){
die("Database Query Failed" . mysqli_error($GLOBALS['connection']));
}
}
//Close The Connection
mysqli_close($connection);
?>
I need to fetch the data in a different php file & then pass that data to a javascript function. Hence I am going to call get_hash(); from a different php file.
Hi I try to load a JSON data from a php link in a js file whith this
$.getJSON('link to rempl.php', function(data) {
cn =data.nom;
});
this is the code of rempl.php
<?php
header('Content-Type: text/plain; charset=utf-8');
require_once ('connectDB.php');
if ($_POST['nom']== 0){
header("Location: http://localhost/geopp/editer.php");
}else{
$id = $_POST['nom'];
}
$sql = "SELECT id , id_archi , nom , archithect , adresse , date_construction , dateconst , resume FROM patri where id=".$id.";";
$req = mysqli_query ($link,$sql);
$feature = array();
//echo "lon\tlat\ttitle\tdescription\ticon";
while ($row = mysqli_fetch_assoc($req)) {
$res['id_archi'] = $row['id_archi'];
$res['id'] = $row['id'];
$res['date'] = $row['dateconst'];
$res['nom'] = $row['nom'];
$res['archithect'] = $row['archithect'];
$res['adresse'] = $row['adresse'];
$res['date_construction'] = $row['date_construction'];
$res['resume'] = $row['resume'];
$feature[] = json_encode($res);
}
echo implode(', ',$feature);
header("Location: http://localhost/geopp/empl.php");
?>
but when I alert the variable cn it show me undefined and thanks for helping me.
I currently have PHP code that fetches some html from the database, it then passes this as JSON to jquery which parses the JSON. Until that moment all is good. However, if you then change some styles in TinyMCE it attaches this as style to the element. (E.g. <h1 style="font-weight:bold">)
Next time the script tries to retrieve this, the JSON doesn't parse, because of the double apostrophes. Is there any way to make TinyMCE not use double apostrophes?
EDIT WITH SOME ACTUAL CODE
PHP Storer:
$conn = mysql_connect($row['ipdb'],$row['usernamedb'], $row['wwdb']) or die("err");
$db = mysql_select_db($row['usernamedb']) or die("err");
$id = $_POST['id'];
$column = $_POST['column'];
$page = $_POST['page'];
$value = $_POST['value'];
$qry = "UPDATE ".$page." SET ".$column."='$value' WHERE id='$id'";
$result = mysql_query($qry) or die("An error occurred ".mysql_error());
PHP Fetcher:
$conn = mysql_connect($row['ipdb'],$row['usernamedb'], $row['wwdb']) or die("err");
$db = mysql_select_db($row['usernamedb']) or die("err");
$identifier = $_POST['identifier'];
$page = $_POST['page'];
$qry = "SELECT id, textnl, texten FROM ".$page." WHERE identifier='$identifier'";
$result = mysql_query($qry) or die("An error occurred ".mysql_error());
$obj = mysql_fetch_object($result);
$textnl = $obj->textnl;
$texten = $obj->texten;
$id = $obj->id;
echo '{ "textnl" : "' . $textnl . '", "texten" : "' . $texten . '", "id" : "' . $id . '" }';
Try to use stripslashes http://www.php.net/manual/es/function.stripslashes.php. Maybe can help you.