I want to insert data which returns from the php code, into an existing html table (name = "userDetails").
Number of rows in the html table would be different according to the results receiving from the mysql database. How can I do that?
Code is as follows,
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
$userDetailsArray = array();
if(mysqli_num_rows($result) > 0){
$index = 0;
while ($row = mysqli_fetch_assoc($result)) {
$userDataArray[$index] = $row;
$row = $userDataArray[$index];
$userID = $row['id'];
$firstName = $row['firstname'];
$email = $row['email'];
$index++;
}
}
?>
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
</table>
</body>
<html/>
Looks like you're looking for something like this:
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td>'. $row['id'] .'</td>';
echo '<td>'. $row['firstname'] .'</td>';
echo '<td>'. $row['email'] .'</td>';
echo '</tr>';
}
}
?>
</table>
</body>
<html/>
You can simply generate HTML in your while loop. Not the best possible strategy, but it certainly gives results.
using same page means it will work. try this
while ($row = mysqli_fetch_assoc($result))
{
$userDataArray[$index] = $row;
$row = $userDataArray[$index];
echo "<tr><td>".$row['id']."</td>";
echo "<td>".$row['firstname']."</td>"";
echo "<td>".$row['email']."</td></tr>"";
$index++;
}
what are the requirements of these lines in your code actualy?
$userDetailsArray = array();
$userDataArray[$index] = $row;
$row = $userDataArray[$index];
??and even what is the requirement of $index variable?
i am adding changes to you program here.
<?php
$connection = mysqli_connect('localhost', 'root', '', 'users');
?>
<html>
<head></head>
<body>
<table name = "userDetails">
<tr>
<th>User ID</th>
<th>First Name</th>
<th>email</th>
</tr>
<?php
$sql = "SELECT id, firstname,email FROM usertable";
$result = mysqli_query($connection, $sql);
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td>$row['id'];</td>
<td>$row['firstname'];</td>
<td>$row['email'];</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan=3>Data is not available</td>
</tr>
<?php
}
?>
</table>
</body>
<html/>
Related
I have a Database contain 3 Tables, the Tables are: (products, Directive and Standard) Each device is manufactured according to a Directive and Standards, I want to creat a webpage with a Dropdown list, where I can select a Device to see according to which directive and Standards made. I alreadz done these, but I can select only one Device.
how can I show a list of all devices when I press at Select all ??
<?php
$con = mysqli_connect("test", "test", "test", "test");
$sql = "SELECT DISTINCT product_name from products";
$res = mysqli_query($con, $sql);
?>
<!DOCTYP html>
<html>
<title></title>
<script type="text/javascript" src="fetchnDisp.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<style type="text/css">
table{
border: 1px solid;
border-collapse: collapse;
padding: 10px;
}
th, td, tr{
border: 1px solid;
}
tr:nth-child(even) {
background-color: #D6EEEE;
}
</style>
</head>
<body>
select Device :
<select id="products" onchange="selectDevice()">
<option value="">Please Choose...</option>
<option value="all">Select All...</option>
<?php while( $rows = mysqli_fetch_array($res)){
?>
<option value="<?php echo $rows['product_name']; ?> " > <?php echo $rows['product_name']; ?> </option>
<?php
}
?>
<select>
<table>
<thead>
<th style="width: 10%"> Article Number </th>
<th style="width: 10%"> Directive </th>
<th style="width: 10%"> Note </th>
<th style="width: 10%"> Standard </th>
</thead>
<tbody id="ans">
</tbody>
</table>
</body>
</html>
here is the Javascript file
function selectDevice(){
var x = document.getElementById("products").value;
$.ajax({
url:"showDevice.php",
method: "POST",
data:{
id : x
},
success:function(data){
$("#ans").html(data);
}
})
}
here is the PHP file
<?php
$k = $_POST['id'];
$k = trim($k);
$con = mysqli_connect("localhost", "test", "test", "woehler");
$sql = "SELECT art_nr, product_name, directive, products.note, standards.standard_name FROM woehler.products left join standards on products.standard_id=standards.id WHERE product_name='{$k}'";
$res = mysqli_query($con, $sql);
while($rows = mysqli_fetch_array($res)){
?>
<tr>
<td><?php echo $rows['art_nr']; ?> </td>
<td><?php echo $rows['directive']; ?> </td>
<td><?php echo $rows['note']; ?> </td>
<td><?php echo $rows['standard_name']; ?> </td>
</tr>
<?php
}
echo $sql;
?>
in your PHP file
rewove WHERE product_name='{$k}' if value of $k equal to "all"
$sql = "SELECT art_nr, product_name, directive, products.note, standards.standard_name FROM woehler.products left join standards on products.standard_id=standards.id";
if($k != 'all'){
$sql = "SELECT art_nr, product_name, directive, products.note, standards.standard_name FROM woehler.products left join standards on products.standard_id=standards.id WHERE product_name='{$k}'";
}
I used if elseif, and that's how it works
if ($k !="") {
$sql = "SELECT art_nr, product_name, directive, products.note, standards.standard_name FROM woehler.products left join standards on products.standard_id=standards.id WHERE product_name='{$k}'";}
elseif ($k != "all") {
$sql = "SELECT art_nr, product_name, directive, products.note, standards.standard_name FROM woehler.products left join standards on products.standard_id=standards.id";}
<?php
$k = $_POST['id'];
$k = trim($k);
$con = mysqli_connect("localhost", "test", "test", "woehler");
$sql = "SELECT art_nr, product_name, directive, products.note, standards.standard_name FROM woehler.products left join standards on products.standard_id=standards.id WHERE product_name='{$k}'";
$res = mysqli_query($con, $sql);
$html = '';
while($rows = mysqli_fetch_array($res)){
$html .= '<tr>
<td>'.$rows['art_nr'].'</td>
<td>'.$rows['directive'].'</td>
<td>'.$rows['note'].'</td>
<td>'.$rows['standard_name'].'</td>
</tr>';
}
echo $html;
?>
Happy Coding!
hi guys how can i create a validation so that when i import a csv file it will not give me a error offset(intentionally reduced the csv file column)
i got the code https://www.webslesson.info/2016/10/import-csv-file-data-into-mysql-database-using-php-ajax.html
here is the code in index.php
<?php
$connect = mysqli_connect("localhost", "root", "", "db");
$query = "SELECT * FROM tbl_employee ORDER BY id desc";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | Import CSV File Data into MySQL Database using PHP & Ajax</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:900px;">
<h2 align="center">Import CSV File Data into MySQL Database using PHP & Ajax</h2>
<h3 align="center">Employee Data</h3><br />
<form id="upload_csv" method="post" enctype="multipart/form-data">
<div class="col-md-3">
<br />
<label>Add More Data</label>
</div>
<div class="col-md-4">
<input type="file" name="employee_file" style="margin-top:15px;" accept=".csv"/>
</div>
<div class="col-md-5">
<input type="submit" name="upload" id="upload" value="Upload" style="margin-top:10px;" class="btn btn-info" />
</div>
<div style="clear:both"></div>
</form>
<br /><br /><br />
<div class="table-responsive" id="employee_table">
<table class="table table-bordered">
<tr>
<th width="5%">ID</th>
<th width="25%">Name</th>
<th width="35%">Address</th>
<th width="10%">Gender</th>
<th width="20%">Designation</th>
<th width="5%">Age</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["gender"]; ?></td>
<td><?php echo $row["designation"]; ?></td>
<td><?php echo $row["age"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#upload_csv').on("submit", function(e){
e.preventDefault(); //form will not submitted
$.ajax({
url:"import.php",
method:"POST",
data:new FormData(this),
contentType:false, // The content type used when sending data to the server.
cache:false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data){
if(data=='Error1')
{
alert("Invalid File");
}
else if(data == "Error2")
{
alert("Please Select File");
}
else if(data != 'Error1' && data != 'Error2')
{
$('#employee_table').html(data);
alert("Data Import Successfully");
}
}
})
});
});
</script>
and here is for import.php
<?php
if(!empty($_FILES["employee_file"]["name"]))
{
$connect = mysqli_connect("localhost", "root", "", "db");
$output = '';
$allowed_ext = array("csv");
$tmp = explode(".", $_FILES["employee_file"]["name"]);
$extension = end($tmp);
if(in_array($extension, $allowed_ext))
{
$file_data = fopen($_FILES["employee_file"]["tmp_name"], 'r');
fgetcsv($file_data);
while($row = fgetcsv($file_data))
{
$id = mysqli_real_escape_string($connect, $row[0]);
$name = mysqli_real_escape_string($connect, $row[1]);
$address = mysqli_real_escape_string($connect, $row[2]);
$gender = mysqli_real_escape_string($connect, $row[3]);
$designation = mysqli_real_escape_string($connect, $row[4]);
$age = mysqli_real_escape_string($connect, $row[5]);
$query = "
INSERT INTO tbl_employee
(id,name, address, gender, designation, age)
VALUES ('$id','$name', '$address', '$gender', '$designation', '$age')
";
mysqli_query($connect, $query);
}
$select = "SELECT * FROM tbl_employee ORDER BY id DESC";
$result = mysqli_query($connect, $select);
$output .= '
<table class="table table-bordered">
<tr>
<th width="5%">ID</th>
<th width="25%">Name</th>
<th width="35%">Address</th>
<th width="10%">Gender</th>
<th width="20%">Designation</th>
<th width="5%">Age</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["id"].'</td>
<td>'.$row["name"].'</td>
<td>'.$row["address"].'</td>
<td>'.$row["gender"].'</td>
<td>'.$row["designation"].'</td>
<td>'.$row["age"].'</td>
</tr>
';
}
$output .= '</table>';
echo $output;
}
else
{
echo 'Error1';
}
}
else
{
echo "Error2";
}
?>
i put all the code so that every one can try it too.
Thanks for answer
I want to create delete feature using function and jquery
My jquery works and show messages but nothing happen "Nothing Deleted"
Jquery Code
<script type="text/javascript">
$(".remove").click(function(){
var id = $(this).parents("tr").attr("id");
if(confirm('Are you sure to remove this record?'))
{
$.ajax({
url: 'delete.php',
type: 'GET',
data: {id: id},
error: function() {
alert('Something is wrong');
},
success: function(data) {
$("#"+id).remove();
alert("Record removed successfully");
}
});
}
});
PHP Function Code
function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}
Delete.php Code
include ('function.php');
$id = $_GET['id'];
$table = 'msg';
delete($table,$id);
HTML Code
<table class="table table-striped" style="background-color: #ffffff;">
<tr>
<th>ID</th>
<th>From</th>
<th>Title</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php
$i = '1';
$username = $user_data['username'];
$query = "SELECT * FROM msg WHERE `go_to` = '$username' Order by id";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row['come_from']; ?></td>
<td>
<a href="read_message/<?php echo $row['id']; ?>"><?php if(count_msg_not_opened($username, $row['id']) > '0')
{
echo $row['title'];
}
else
{
echo '<b>' . $row['title'] . '</b>';
} ?></a></td>
<td><?php echo $row['date']; ?></td>
<td>
<button class="btn btn-danger btn-sm remove">Delete</button>
</td>
</tr>
<?php } ?>
</table>
I also include "jquery.min.js"
When I press "Delete" bottom this message appears "Are you sure to remove this record?"
I pressed "Yes" then this message appears "Record removed successfully", but nothing was deleted.
I don't know where the problem is.
You forgot to add the id attribute to the <tr>
<tr id="<?php echo $row['id']; ?>">
You should also add error checking and prepared statements to your PHP code.
Are you sure that you have connected your PHP-code to your SQL-database?
function delete($table,$id) {
global $connect;
mysqli_query($connect, "DELETE FROM `$table` WHERE `id` = $id ");
}
The code above is relying on a connection already existing within your PHP-file. See this to find out how to apply a connection.
I have comma based string which is separated by comma, i need the staring to be converted in each single row and add against to the id ,how should i rotate the inner loop for code after explode function
<?php $conn = new mysqli("localhost","root","","test"); if($conn->connect_error){
die("Connection Failed<br>".$conn->connect_error); }
$sql = 'SELECT id,mobile,code,createdon FROM tset';
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$mobile = $row['mobile'];
$code = $row['code'];
$date = $row['date'];
echo "<tr><td>".$id."</td><td>".$mobile."</td>
<td>".$code."</td><td>".$date."</td></tr>";
}
mysqli_close($conn); ?>
Sample Data
id:10542
Mobile: 1234567890
Code: H5413910102,H5413910201,H5413910301,H5413910701,H5413910802,
dateL:2017-08-30 16:57:00
I need to get the data in this format by using the sample data
<table>
<tr>
<th>ID</th>
<th>MOBILE</th>
<th>CODE</th>
<th>DATE</th>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910102</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910201</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910301</td><td>9/4/2017</td>
</tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910701</td><td>9/4/2017</td>
<tr>
<tr>
<td>10541</td><td>1234567890</td><td>H5413910802</td><td>9/4/2017</td>
</tr>
</table>
$myString = "H5413910102,H5413910201,H5413910301,H5413910701,H5413910802,";
$myArray = explode(',', $myString);
foreach($myArray as $my_Array){
echo $my_Array.'<br>';
}
From the database's perspective, it's not recommended and certainly not a good practice to have data as comma separated list. You should consider normalizing your database. Having said that, you should follow the below procedure to achieve the desired result as of now(or for the time being).
<?php
$conn = new mysqli("localhost","root","","test");
if($conn->connect_error){
die("Connection Failed<br>".$conn->connect_error);
}
$sql = 'SELECT id,mobile,code,createdon FROM tset';
$result = $conn->query($sql);
?>
<table>
<tr>
<th>ID</th>
<th>MOBILE</th>
<th>CODE</th>
<th>DATE</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)){
$codes = explode(",", $row['code']);
foreach($codes as $code){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><?php echo $code; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
<?php
}
}
?>
</table>
<?php
mysqli_close($conn);
?>
Hope this code helps you :
$sql = 'SELECT id,mobile,code,createdon FROM tset';
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$mobile = $row['mobile'];
$code = $row['code'];
$date = $row['date'];
$code = explode(",",$code);
foreach($code as $key=>$value){
echo "<tr><td>".$id."</td><td>".$mobile."</td><td>".$value."</td><td>".$date."</td></tr>";
}
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I want alphabetical pagination in php. I have a code, but some problem is there. When I click any alphabet, in address bar selected alphabet shows, but data starting from selected alphabet not shows.
I have database testing
table name: tbl_student
atrributes are: student_id, student_name, student_phone
and my code is:
<?php
$connect = mysqli_connect('localhost', 'root', '', 'testing');
$char = '';
if(isset($_GET["char"]))
{
$char = $_GET["char"];
$char = preg_replace('#[^a-z]#i', '', $char);
$query = "SELECT * FROM tbl_student WHERE student_name LIKE '$char%'";
}
else
{
$query = "SELECT * FROM tbl_student ORDER BY student_id";
}
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title> Alphabetic Pagination</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:1100px;">
<div class="table-responsive">
<div align="center">
<?php
$character = range('A', 'Z');
echo '<ul class="pagination">';
foreach($character as $alphabet)
{
echo '<li>'.$alphabet.'</li>';
}
echo '</ul>';
?>
</div>
<table class="table table-bordered">
<tr>
<th width="20%">ID</th>
<th width="50%">Student Name</th>
<th width="30%">Student Phone</th>
</tr>
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["student_id"]; ?></td>
<td><?php echo $row["student_name"]; ?></td>
<td><?php echo $row["student_phone"]; ?></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3" align="center">Data not Found</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
The problem is that you have used character as variable names in url and char in $_GET[] array.
Either
Change $_GET["char"] to $_GET["character"] `
Or
Change
<li>'.$alphabet.'</li>
to
<li>'.$alphabet.'</li>