i am displaying my table data in html page but i am getting blank page.....
i am new to php coding please help me.....
this is my html code
<!DOCTYPE html>
<html>
<head>
<script>
function validate(){
var phonenumber=document.myform.phonenumber.value;
if(phonenumber.length>=10){
alert("Phonenumber must be at least 10 characters");
return false;
}
}
</script>
</head>
<body font-color="red">
<form name ="myform" action="dis.php" method="post" submit="return validate()">
<table border='0' width='480px' cellpadding='0' cellspacing='0' align='center'>
<center><tr>
<td><h1><marquee> Employee Details</marquee></h1></td>
</tr><center>
<table border='0' width='480px' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td align='center'>Name:</td>
<td><input type='text' name='name' required></td>
</tr>
<tr> <td> </td> </tr>
<tr>
<td align='center'>id:</td>
<td><input type='text' name='id'></td>
</tr>
<tr> <td> </td> </tr>
<tr>
<td align='center'>roll number:</td>
<td><input type='text' name='rollnumber'></td>
</tr>
<tr> <td> </td> </tr>
<tr>
<td align='center'>Address:</td>
<td><input type='text' name='address'></td>
</tr>
<tr> <td> </td> </tr>
<tr>
<td align='center'>Phonenumber:</td>
<td><input type='text' name='phonenumber'></td>
</tr>
<tr> <td> </td> </tr>
<table border='0' cellpadding='0' cellspacing='0' width='480px' align='center'>
<tr>
<td align='center'><input type='submit' name='insert' value="insert"></td>
</tr>
<tr>
<td align='center'><input type='submit' name='update' value="update"></td>
</tr>
</table>
</table>
</table>
</form>
</body>
</html>
this is my php code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "emp",$connection);
if (!$select_db)
{
die("Database Selection Failed" . mysql_error());
}
else
{
session_start();
error_reporting(0);
$name = $_POST['name'];
$id = $_POST['id'];
$rollnumber = $_POST['rollnumber'];
$address = $_POST['address'];
$phonenumber = $_POST['phonenumber'];
if(isset($_POST['insert']))
{
$sql = mysql_query("SELECT * FROM venu ");
$result = mysql_query($sql) or die(mysql_error());
$n=mysql_num_rows($result);
if($n > 0)
{
echo "<table>";
echo "<th>name</th>";
echo "<th>id</th>";
echo "<th>rollnumber</th>";
echo "<th>address</th>";
echo "<th>phonenumber</th>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['rollnumber'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No records matching your query were found.";
}
}
else
{
echo "ERROR: Could not able to execute.";
}
}
mysql_close($connection);
?>
</body>
</html>
i am getting error
only blank page displaying...
please help me....
You are using mysql_query() twice. Change this:
$sql = mysql_query("SELECT * FROM venu ");
$result = mysql_query($sql) or die(mysql_error());
Into:
$sql = "SELECT * FROM venu ";
$result = mysql_query($sql) or die(mysql_error());
Related
If I write something in search box and press search , it should only return matched rows and hide other rows.
Here is my code, it works perfects only issue is it gives me searched record + all record list of table.
What can I do to show only searched data in table.?
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page=5;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $num_rec_per_page;
$result= mysql_query("SELECT * FROM products LIMIT $start_from, $num_rec_per_page");
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%".$term."%' or price LIKE '%".$term."' or details LIKE '%".$term."'";
$r_query = mysql_query($sql);
if($r_query>1)
{
while ($row = mysql_fetch_array($r_query)){
echo "<tr bgcolor='red'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
}
else{
echo "Nothing should be displayed";
}
}
?>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr class='danger'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
?>
</table>
Just keep single while loop and run the query and search query as shown below it will solve the issue:
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1) {
while ($row = mysql_fetch_array($r_query)) {
echo "<tr bgcolor='red'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['details'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subcategory'] . "</td>";
echo "<td>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
} else {
echo "Nothing should be displayed";
}
?>
</table>
I have a quick question, I'm trying to use a function to delete a certain movie from a database using checkboxes. I can add stuff to the database just fine, but can't seem to delete using the checkboxes. Thanks in advance for your help!
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<?
$db = new PDO("mysql:host=localhost;dbname=armstrongpz", "armstrongpz", "12345");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
<?
function deleteFromDatabase($db)
{
$deleteQuery = $db->prepare("SELECT * FROM movie");
$deleteQuery->execute();
foreach($deleteQuery->fetchAll() as $row)
{
if(array_key_exists($row['id'], $_POST))
{
$deleteQuery = $db->prepare('DELETE FROM movie WHERE movie.id='. $row['id']);
$deleteQuery->excute();
}
}
}
?>
<h2 style='text-align: center';>
Movie Collection Database
</h2>
<?
if($_SERVER['REQUEST_METHOD'] === "POST")
{
deleteFromDatabase($db);
if(isset($_POST['add']))
{
$TitleOfMovie = $_POST['TitleMovie'];
$StudioOfMovie = $_POST['StudioMovie'];
$RatingOfMovie = $_POST['mRating'];
$PubYearOfMovie = $_POST['PubYear'];
$IMDBRating = floatval($_POST['imdbRating']);
$RunTimeOfMovie = $_POST['RunTime'];
$addQuery = "INSERT INTO movie (id,title,studio,rating,pub_year,imdb_rating,run_time) VALUES(".'NULL'.", '$TitleOfMovie','$StudioOfMovie','$RatingOfMovie','$PubYearOfMovie', '$IMDBRating', '$RunTimeOfMovie')";
$statement = $db->prepare($addQuery);
$statement->execute();
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<table align='center' border='0' cellpadding='5'>
<tbody>
<tr>
<td style='border-style: none'>
<input name='TitleMovie' size='50' type='text' width='35' value= 'Title'/>
</td>
<td style='border-style: none'>
<input name='StudioMovie' size='25' type='text' width='35' value= 'Studio Name'/>
</td>
<td>
<?
$rating = array(1=>'G', 'PG', 'PG-13', 'R', 'NC-17', 'Not Rated');
echo "<select size='1' selected='movieRating' name='mRating'>";
foreach($rating as $key=>$value)
{
echo "<option value=\"$key\">$value</option>\n";
}
echo "</select>\n";
?>
</td>
<td style='border-style: none'>
<input name='PubYear' size='10' type='text' width='35' value='Pub Year'/>
</td>
<td style='border-style: none'>
<input name='imdbRating' size='10' type='text' width='35' value='IMDB rating'/>
</td>
<td>
<input name='RunTime' size='10' type='text' width='35' value='Run time'/>
</td>
<td>
<input type="checkbox" name="add" value="Add" align="top"/>Add
</td>
</tr>
<div style="text-align:center">
<input type='submit' name='submit' value='Update Database'>
</div>
</tbody>
</table>
</form>
<table border ='1' align='center'>
<?
$arrayOfFieldNames = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run time(min)");
echo "<tr>";
echo "<td>". $arrayOfFieldNames[0] . "</td>";
echo "<td>". $arrayOfFieldNames[1] . "</td>";
echo "<td>". $arrayOfFieldNames[2] . "</td>";
echo "<td>". $arrayOfFieldNames[3] . "</td>";
echo "<td>". $arrayOfFieldNames[4] . "</td>";
echo "<td>". $arrayOfFieldNames[5] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
?>
<?
$query = "SELECT * FROM movie";
$stmt = $db->prepare($query);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>" .$row['title']. "</td>";
echo "<td>" .$row['studio']. "</td>";
echo "<td>" .$row['rating']. "</td>";
echo "<td>" .$row['pub_year']. "</td>";
echo "<td>" .$row['imdb_rating']. "</td>";
echo "<td>" .$row['run_time']. "</td>";
echo "<td><input type='checkbox' name='". $row['id'] . "' value='" . $row['id'] . "'>Delete</td>";
echo "</tr>";
}
?>
</table>
<?
if(isset($db))
{
$db= null;
}
?>
</body>
Please forgive me for the terribly structured code. I'm attempting to update different customers on each line with an individual submit button. The delete function works great. I'm trying to pass the ID of the row when the submit button is clicked for each row. However, I'm getting the ID for the last row, no matter which row I click. I've attached a screenshot as an example. Any help is appreciated!
<form action="" method="post">
<table class="table table-striped table-bordered table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Product</th>
<th>Firmware Version</th>
<th>Purchase Date</th>
<th>Delete</th>
</tr>
</thead>
<?php
$pdo = new PDO("mysql:host=localhost;dbname=project", $username, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query = $pdo->prepare("select * from customers");
$query->execute();
while($customers = $query->fetch()){
$ID = $customers['ID'];
echo '<tr><td><input type="text" name="name" value="';
echo $customers['name'];
echo '"></td>';
echo "<td>" . $customers['email'] . "</td>";
echo "<td>" . $customers['phone'] . "</td>";
echo "<td>" . $customers['address'] . "</td>";
echo "<td>" . $customers['product'] . "</td>";
echo "<td>" . $customers['firmware'] . "</td>";
echo "<td>" . $customers['purchase_date'] . "</td>";
echo '<td align="center"><input type="hidden" name="id" value="';
echo $ID;
echo '"><input type="submit" name="delete" value="X"> </td></tr>';
echo '<tr><td colspan="8"><input type="hidden" name="id_update" value="';
echo $ID;
echo '"><input type="submit" name="update" value="Update">';
echo $ID . '<--This is the ID for each row';
echo '</td></tr>';
}
// Delete customer
if(isset($_POST['delete'])) {
try{
$ID = $_POST['id'];
$query = $pdo->prepare("delete from customers where ID = :ID");
$query->bindParam(':ID', $ID);
$query->execute(array(
':ID' => $ID
));
echo "Customer successfully deleted.";
echo '<META http-equiv="refresh" content="1;URL=view_edit.php">';
}catch(PDOException $e){
echo "Failed to delete the MySQL database table ... :".$e->getMessage();
} //end of try
} //end of isset delete
// Edit customer
if(isset($_POST['update'])) {
echo "Update " . $_POST['id_update'] . '<-- This is the result of clicking update for each row';
} //end of isset update
?>
</table>
</form>
Have you tried doing a form per row as opposed to one form that wraps all the rows (I know you are asking about doing it in 1 form, so you may be just wanting to do it that way only)? It may or may not be what you are looking for. I do this when I need multi-rows with per-row updates.
<?php
function UserForm($customers = array())
{
ob_start(); ?>
<form action="" method="post"><?php
$ID = $customers['ID']; ?>
<tr>
<td><input type="text" name="name" value="<?php echo $customers['name']; ?>"></td>
<td><?php echo $customers['email']; ?></td>
<td><?php echo $customers['phone']; ?></td>
<td><?php echo $customers['address']; ?></td>
<td><?PHP echo $customers['product']; ?></td>
<td><?php echo $customers['firmware']; ?></td>
<td><?php echo $customers['purchase_date']; ?></td>
<td align="center">
<input type="hidden" name="id" value="<?php echo $ID; ?>">
<input type="submit" name="delete" value="X">
</td>
</tr>
<tr>
<td colspan="8">
<input type="hidden" name="id_update" value="<?php echo $ID; ?>" />
<input type="submit" name="update" value="Update" />
<?php echo $ID; ?><--This is the ID for each row -->
</td>
</tr>
</form>
<?php
$data = ob_get_contents();
ob_end_clean();
return $data;
} ?>
<table class="table table-striped table-bordered table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Product</th>
<th>Firmware Version</th>
<th>Purchase Date</th>
<th>Delete</th>
</tr>
</thead>
<?php
$pdo = new PDO("mysql:host=localhost;dbname=project", $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$query = $pdo->prepare("select * from customers");
$query->execute();
while($customers = $query->fetch()){
echo UserForm($customers);
}
// Delete customer
if(isset($_POST['delete'])) {
try{
$ID = $_POST['id'];
$query = $pdo->prepare("delete from customers where ID = :ID");
$query->bindParam(':ID', $ID);
$query->execute(array(':ID' => $ID));
echo "Customer successfully deleted.";
echo '<META http-equiv="refresh" content="1;URL=view_edit.php">';
}catch(PDOException $e){
echo "Failed to delete the MySQL database table ... :".$e->getMessage();
} //end of try
} //end of isset delete
// Edit customer
if(isset($_POST['update'])) {
echo "Update " . $_POST['id_update'] . '<-- This is the result of clicking update for each row';
} //end of isset update
?>
</table>
I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
First off you can just implode() all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway
my html form ->
<form action="where.php" method="post">
<h2 align="center" style="color: white;"> Search Challan </h2>
<table border="1" bgcolor="grey" align="center">
<tr>
<td align="center"> Search a Challan Details . Enter the Challan no below :</td>
</tr>
<tr>
<td align="center">
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="Search"align="middle" ></td>
<tr>
<td>
<select name="squery" style="width:142px;" >
<option value="challan_no">Challan no </option>
<option value="product_name">Product Name</option>
<option value="Buyer">buyer</option>
<option value="Employee Responsible">Employee</option>
</td>
</tr>
<tr>
<td>
<input type="text" name="search" >
</td>
</tr>
</tr>
</form>
where.php ->
<?php
$dbhost='localhost';
$dbusername='root';
$dbuserpass='';
$dbname='inventory';
//connect to the mysql database server.
$con = mysql_connect ($dbhost, $dbusername, $dbuserpass);
if (!$con ) die ("unable to connect : ". mysql_error());
mysql_selectdb("$dbname",$con ) ;
$user_req = $_REQUEST['squery'] ; //colomn name
$req_id = $_REQUEST['search'] ; //
$query = "SELECT * FROM challan WHERE '$user_req' = $req_id ";
$result = mysql_query($query);
if (!$result) die ("DAtabase acces faild bc : ". mysql_error());
$rows = mysql_numrows($result);
for ($j=0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo "<TABLE border=1 bgcolor=grey align=center width=500px float=left>" ;
echo "<tr>";
echo "<td align=center>Challan no : </td>";
echo " <td> $row[0] </td>";
echo " </tr>";
echo "<tr>";
echo "<td align=center>Challan Date : </td>";
echo " <td> $row[1] </td>";
echo "<tr>";
echo "<td align=center>Product Name : </td> ";
echo " <td> $row[2] </td>";
echo "<tr>";
echo "<td align=center>Product qty : </td> " ;
echo " <td> $row[3] </td>";
echo "<tr>";
echo "<td align=center> Buyer : </td> " ;
echo " <td> $row[4] </td>";
echo "<tr>";
echo "<td align=center>Employee Responsible : </td> " ;
echo " <td> $row[5] </td>";
echo "</tr>";
}
?>
Its output is just blank. I'm unable to identify where I am going wrong.
blank screen indicates that has occurred a fatal error and php was unable to continue script execution, you can run this in the top of your scripts to errors should be printed to the screen as part of the output.
<?php
ini_set('display_errors', 1);