Tuesday, 15 October 2019

PHP Code to upload csv file into a MySQL table

Hi All,
Use this code

Create a database SNITDB and table import(tid,name,tweet,label) to run this program.
----------------------------------------------------------------------------------
<form method='POST' enctype='multipart/form-data'>
<center>
Upload CSV FILE: <input type='file' name='csv_info' />
<input type='submit' name='submit' value='Upload' style="height: 50px; width: 150px; left: 250; top: 250;"/>
</center>
</form>
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("SNITDB",$con);
if(isset($_POST['submit'])){
if($_FILES['csv_info']['name']){
$arrFileName = explode('.',$_FILES['csv_info']['name']);
if($arrFileName[1] == 'csv'){
$handle = fopen($_FILES['csv_info']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$item1 = mysqli_real_escape_string($con,$data[0]);
$item2 = mysqli_real_escape_string($con,$data[1]);
$item3= mysqli_real_escape_string($con,$data[2]);
$item4= mysqli_real_escape_string($con,$data[3]);
$sql="INSERT into import(tid,name,tweet,label) values('$item1','$item2','$item3','$item4')";
                                    mysqli_query($con,$sql);
                                   }
                        fclose($handle);
}
}
}
?>

PHP code to create CSV file from MySQL table

Hi All,
Use this code..
For this program to run create database SNITDB  and create a table tweet(tid,sname,tweet)
----------------------------------------------------------
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("SNITDB",$con);
header('Content-Type: text/csv; charset=utf-8');
      header('Content-Disposition: attachment; filename=da.csv');
       $output = fopen("php://output", "w");
    fputcsv($output, array('tid','sname', 'tweet'));
        $query = "select distinct * from tweet where label=0";
        $result = mysql_query($query, $con);
        while($row = mysql_fetch_array($result))
        {
             fputcsv($output,array($row['tid'],$row['sname'],$row['tweet']));
             $sql="update tweet set label=1 where tid='$row[tid]'";
             mysql_query($sql, $con);
        }
      fclose($output);

?>

Monday, 14 October 2019

Load Combo Box from DB

Hi All,

Use this code
Here the function showoptions takes table name as first argument, id as second argument and value to be displayed in the screen as third argument.
------------------------------------------------------------------------------------------------------
Showdata.php

<html>
<body>

<?php
include('dbconnect.php');
?>

<form  action=# method=post>
<pre>
Project Name <input>
Project Members <select name=projmembers multiple> <?php showoptions("reg_st","id","name"); ?></select>


</pre>
</form>
</body>
</html>


-------------------------------------------------------------------------------------
dbconnect.php

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("snitdb",$con);

function showoptions($table,$value,$display)
{
$qr="select  * from  $table ";
echo $qr;

$data=mysql_query($qr);
while($row=mysql_fetch_array($data))
{
echo "<option value='".$row[$value]."'>".$row[$display]."</option>";

}

}


?>