4

I am complete beginner I want to display comma separated values from database in tabular form

From the below image link we can see how the data is getting added separated by commas:

Databaseimage

I want to display in tabular form in html/php page just like below:

Table image

This is my html page below:-

<form action="insert.php" method="POST">

<div id="items">
    <input type="text" placeholder="name" name="user_name[]" />
    <input type="text" placeholder="email" name="user_email[]" />   
</div>

<input type="button" value="add entry" id="add"/>
<input type="submit"  value="submit"/>

Html page

Javascript file for adding additional input:-

$(document).ready(function(){

$("#add").click(function (e){
event.preventDefault()
$('#items').append('<div><input type="text" placeholder="Name"   name="user_name[]" ><input type="text" placeholder="email" name="user_email[]">'
+'<input type="button" value="delete" id="delete"/></div>');    

}); 

$('body').on('click','#delete',function(e){
    $(this).parent('div').remove();
});

});

And below is the PHP code for inserting to database:-

 <?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dynamic", $con);

$user_name = $_POST["user_name"];
$value = implode(',', $user_name); 
$user_email = $_POST["user_email"]; 
$valueone = implode(',', $user_email); 

$sql="INSERT INTO dynamicdata (user_name, user_email)
VALUES
('$value','$valueone')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>
Shabarish Shetty
  • 132
  • 2
  • 12
  • 8
    Since you are just starting out, let me gently point out that SQL tables shouldn't be like that. Please find a tutorial on database normalization and read that before writing any more code or trying to get this fixed. – e4c5 Jan 05 '17 at 10:49
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 05 '17 at 10:51
  • Hi , Thanks for quick reply . I need this done urgently because there are thousands of code in our project . Am not in a position to change since we have meet the deadline .On the later stage we can go through database normalization and change the code . Is there any alternate solution for this . – Shabarish Shetty Jan 05 '17 at 10:54
  • You may also want to check your browsers debug console and fix the javascript error as well – RiggsFolly Jan 05 '17 at 10:54

2 Answers2

1

Just fetch the data from the table and explode both username and useremail to make array and just loop it and diplay the values.

Below pasted code may help you.

<?php
$ArrUserName=explode($username,','); //exploding the coma separated username to make array of user names
$ArrUserEmail=explode($username,',');//exploding the coma separated user email to make array of user email
echo '<table><tr><td>Name</td><td>EMAIL</td></tr>';
for($i=0;$i<count($ArrUserName);$i++){
    echo "<tr>";
        echo"<td>".$ArrUserName[$i]."</td>"; //display user name
        echo"<td>".$ArrUserEmail[$i]."</td>"; // diaplay user email
    echo"</tr>";        
}
?>

Please understand this nd try to implement in your code.

Pranav MS
  • 2,235
  • 2
  • 23
  • 50
1

Ideally, you would want the username and email stored in a new row for each user. This would make it a lot simpler for yourself.

However, try something like:

<?php 

$query   = mysql_query('SELECT * FROM dynamicdata LIMIT 1', $con);
$results = mysql_fetch_assoc($query);

$usernames = explode(',', $results['user_name']);
$emails    = explode(',', $results['user_email']);

//Now we should be able to loop over them.
for($row = 0; $row <= count($usernames); $row++) {
    echo $usernames[$row] . ' : ' . $emails[$row];
}

I haven't had chance to test this, but hopefully it works.

Ryan Hipkiss
  • 648
  • 1
  • 7
  • 20