Sir,
I have a form which contains 3 text fields and a submit button.(basically a search form) I made a query set through which i dynamically display a table on the same page. So inputting a specific data in any of the text field and hitting the submit button the table shows the relevant result for that query.
Now when the page loads it shows the empty table also. I want when user enters some txt inputs and hits submit button then only the table below should shows up. Please help me. I am totally new to coding. I uses dreamweaver.
Try this:
<?php
if(isset(POST['submit']))
{
?>
Set table structure here ....
<?php
}
?>
-
Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<title>Test</title>
<head>
</head>
<body>
<form action="" name="myForm" enctype="multipart/form-data" method="post">
<table width="100%" align="center">
<tr><td>First Name</td><TD><input type="text" name="fname"></TD></tr>
<tr><td></td><TD><input type="text" name="mname"></TD></tr>
<tr><td>Last Name</td><TD><input type="text" name="lname"> </TD></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit" onmouseover="this.style.cursor=\'pointer\';" OnMouseOut="this.style.cursor=\'default\';">
</td></tr></table></form>
<?PHP
if(isset(POST['submit']))
{
$con = mysql_connect("$host","$username","$pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name", $con); //database name
mysql_query("insert into db_table set first_name='".$POST['fname']."',middle_name='".$POST['mname']."',last_name='".$POST['lname']."'");
$result = mysql_query("SELECT * FROM $db_table"); //table
echo "<table cellpadding='0' cellspacing='0'>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['middle_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>
</body>
</html>
Since you have not provide any code, I guess you can do something like this :-
If your search result return null then don't show the table :-
if(mysqli_num_rows($result) <= 0)
{
//don't show your table
}
else
{
// result table
}
Related
I am trying to update the rank column in the users table in my database by presenting data in a PHP form and using a button to submit. However once i edit the data in my PHP form and press submit, the data in the database remains unchanged. I'm adding a (link to the) picture of the webpage, and the code is posted below.
Webpage image
<!DOCTYPE HTML>
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
Displays all data from 'users' table
*/
// connect to the database
include('../db/connect.php');
// get results from database
$result = $MySQLi_CON->query("SELECT * FROM users")
or die(mysql_error());
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Username</th> <th>Email</th> <th>Rank</th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = $result->fetch_array()) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_id'] . '</td>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td><input type="hidden" name="user_id[]" id="newrank" width="20px" min="0" max="100" value="' . $row['user_id'] . '"></td>';
echo '<td><form method="POST" action=""><input type="number" name="newrank[]" id="newrank" width="20px" min="0" max="100" value="' . $row['rank'] . '"></form></td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
if(isset($_POST['btn-update'])) {
for($i = 0; count($_POST["user_id"]); $i++) {
$_POST['newrank'][$i] = $MySQLi_CON->real_escape_string($_POST['newrank'][$i]); # If this function exists either, if not comment or remove this line!
$_POST['user_id'][$i] = $MySQLi_CON->real_escape_string($_POST['user_id'][$i]); # If this function exists either, if not comment or remove this line!
$MySQLi_CON->query('UPDATE users SET rank=' . $_POST['newrank'][$i] . ' WHERE user_id=' . $row['user_id'][$i] . '');
}
echo "Updated the rows.";
}
?>
<br>
<button type="submit" class="btn btn-default" name="btn-update" id="btn-update">Update</button></a>
<p>Add a new record</p>
</body>
</html>
Seems there is an error in your query statement
Modify this : if ($$MySQLi_CON->query($sql) === TRUE) {
with if ($MySQLi_CON->query($sql) === TRUE) {
You need to parse the id you wish to modify to the $_POST. Also, you need to use <form action="" method="POST"> in your code.
Didn't tested it, but the following should work:
<!DOCTYPE HTML>
<html>
<head>
<title>View Records</title>
</head>
<body>
<table border='1' cellpadding='10'>
<thead> <th>ID</th> <th>Username</th> <th>Email</th> <th>Rank</th> <th colspan="3"></th></thead>
<tbody>
<?php
//Displays all data from 'users' table
// connect to the database
include('../db/connect.php');
// get results from database
$result = $MySQLi_CON->query("SELECT * FROM users")
or die(mysql_error());
// loop through results of database query, displaying them in the table
while($row = $result->fetch_assoc()) {
// echo out the contents of each row into a table
?>
<form action="" method="POST">
<tr>
<td><?php echo $row['user_id']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email'];?></td>
<td><input type="number" name="newrank" id="newrank" value="<?php echo $row['rank']; ?>"></td>
<td><input type="hidden" name="id" value="<?php echo $row['user_id']; ?>"><button type="submit" class="btn btn-default" name="btn-update" id="btn-update">Update</button></td>
<td>Delete</td>
</tr>
</form>
<?php
}
?>
</tbody>
</table>
<?php
if(isset($_POST['btn-update']))
{
$sql = 'UPDATE users SET rank=' . $_POST['newrank'] . ' WHERE user_id=' . $_GET['id'] . '';
if ($MySQLi_CON->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $MySQLi_CON->error;
}
}
?>
<p>Add a new record</p>
</body>
</html>
I realise I'm probably being the biggest noob there has ever been, but this has driven me mad all day...
So basically I've got 1 table called 'clients' with basic client details in... I've managed a search.php, view.php and edit.php to show and edit the client search results, but I want an 'Add Project' button under each search result which links to newproject.php, where the ID of the client I am currently viewing is populated into the form, so I can 'create a new project' with the client's details without having to put them in again - I've tried different things all day to absolutely no avail, I'm at the point now where I'm clearly missing the most basic solution!
Any help would be so hugely appreciated!
// connect to the database
include('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM clients")
or die(mysql_error());
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Title'] . '</td>';
echo '<td>' . $row['LastName'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
Is the view.php
$sql="INSERT INTO projects (projectname, budget)
VALUES
('$_POST[projectname]','$_POST[budget]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
Is the insertproject.php
<form action="insertproject.php" method="post">
Project Name: <input type="text" name="projectname" /><br><br>
Budget: <input type="text" name="budget" /><br><br>
Is newproject.php
For some reason I've got the idea in my head that I want a textbox pre-populated with GET ID from the URL to go straight into the project form...
If it sounds more simple to you than it does to me then you deserve a Nobel Prize!
Pass the client_id to newproject.php and collect the client_id in the newproject.php and put the value in a hidden field inside the form.
In Search Results Page Add
Add Project
In newproject.php Page
<?php
$clientId = $_REQUEST['client_id']
?>
<form method="post" action="newproject.php">
<input type="hidden" name="client_id" id="client_id" value="<?php echo $clientId;?>"/>
-----------------
Your Project Form
-----------------
</form>
THIS IS MY CODE HERE I AM GETTING PROBLEM THIS IS A INPUT TYPE TAG AND NOT SELECTED DATA FROM DATABASE USING PHP.
here is first part of this code is html formatted ad second part is php
i want select data from database to data slect to same page as we see on the sopping cart sites
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpSelect</title>
</head>
<body>
Insert Age for search
<form action="#" method="post" >
<input type="text" id="val" name="resValue" />
<input type="submit" value="submit" /></form>
<?php
if(isset($_POST['submit']))
{
$res=$_POST['resValue'];
echo $res;
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons where Age=25");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Try changing the SQL to this
SELECT * FROM Persons where Age='".mysql_escape_string($formValue['val'])."'
First issue I found on your code is here:
<input type="submit" value="submit" />
it should be:
<input type="submit" value="submit" name="submit" />
To be able to get the results. Below are the codes:
<?php
$query = "";
if(isset($_POST['submit']))
{
$res=$_POST['resValue'];
$query = " where Age='$res'"
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons $query");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Change your select query to this one.
SELECT * FROM Persons where Age='".mysql_escape_string($_POST['val'])."'
In your problem the all value of form get by the name of all fields of form.\
so here should be
<input type="submit" name="submit" value="submit"/>
Because in $_POST['submit'] submit is same as button's name.
$result = mysqli_query($con,"SELECT * FROM Persons where Age="25");
In the below code the list box is getting populated with the values but when I select the corresponding list value and click search the table is not getting displayed. I have used the SELF_PHP for the form action here also the idea of this form is that the list will be having and id's populated while the loading of the page. When I select a list value and click on the search button it should display the table with the values of only that ambulance ID or id which has been selected in the list box. Please help me.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>TPS Login Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Ambulance Activity Log</h2>
<hr>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
View activity of ambulance:
<Select NAME="field">
<?php
include 'con.php';
$query = "SELECT amb_id FROM ambulance;";
$result = mysql_query($query) or die("Unable to retreive amb_id :".mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
echo"<option id=\"ambid\" value=$row[amb_id]>$row[amb_id]</option>";
}
}
?>
</Select>
<input class="btn btn-info" type="submit" name="search" value="Search" />
<input type="hidden" name="searching" value="yes" />
</form>
<?php
if(isset($_POST['submit']))
{
$id=getElementById('ambid') ;
$query = "SELECT * FROM log WHERE amb_id='$id' ORDER BY l_time DESC;";
$result = mysql_query($query) or die(mysql_error());
print "
<table border=\"5\" cellpadding=\"5\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#808080\" width=\"800\" text-align=\"center\" id=\"AutoNumber2\" bgcolor=\"#C0C0C0\">
<tr>
<td width=\"30\">Amb ID</td>
<td width=\"120\">Event Time</td>
<td width=\"50\">Description</td>
<td width=\"30\">Signal ID</td>
<td width=\"30\">Road No</td>
<td width=\"30\">Priority</td>
</tr>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
print "<tr>";
print "<td>" . $row['amb_id'] . "</td>";
print "<td>" . $row['l_time'] . "</td>";
print "<td>" . $row['l_event'] . "</td>";
print "<td>" . $row['t_sigid'] . "</td>";
print "<td>" . $row['l_roadno'] . "</td>";
print "<td>" . $row['e_priority'] . "</td>";
print "</tr>";
}
print "</table>";
print "</center>";
}
?>
</body>
</html>
Please help me.
Thanks in advance :)
Give the <select> Tag a better name . And quote your "$row[amb_id]". value=$row[amb_id] ! is wrong.
<select name="ambid" size="10">
....
{
echo"<option value=\"$row[amb_id]\">$row[amb_id]</option>";
}
</select>
After post you can get it with :
if(isset($_POST['submit']))
{
$id=$_POST['ambid'];
$query = "...
I have this html code:
<tr>
<td><label><input type="text" name="id" class="DEPENDS ON info BEING student" id="example">ID</label></td>
</tr>
<tr>
<td>
<label> <input type="checkbox" name="yr" class="DEPENDS ON info BEING student"> Year</label>
</td>
</tr>
But I don't have any idea on how do I check this checkboxes if they are checked using php, and then output the corresponding data based on the values that are checked.
Please help, I'm thinking of something like this. But of course it won't work, because I don't know how to equate checkboxes in php if they are checked:
<?php
$con = mysql_connect("localhost","root","nitoryolai123$%^");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("school", $con);
$id = mysql_real_escape_string($_POST['idnum']);
if($_POST['id'] == checked & $_POST['yr'] ==checked ){
$result2 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
echo "<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['IDNO'] . "</td>";
echo "<td>" . $row['YEAR'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
You must give your checkboxes a value. This value gets send to the server, in case the checkbox is checked.
if ( $_POST['checkboxname'] == 'checkboxvalue' ) {
}
Since I see no form:
To send the data to the server, you need a form around your input elements:
<form method="POST" action="myphpscript.php">
YOUR CONTENT HERE
</form>
try the following:
if (isset($_POST['yr'])) { ... }
$_POST['yr'] == checked
should be:
$_POST['yr'] == 'on'
The default for firefox is 'on', maybe different in other browsers. (Thanks to David)
If you include a hidden field, with the same name and the failure value that you want to show up in the post data, then when the checkbox does not return a value (it is unchecked), the hidden control on the form will.
echo '<form method="post"><input type="hidden" name="checkdata" value="0">\
<input type="checkbox" name="checkdata" value="1">\
<input name="submitbutton" type="submit"></form>\
</body></html>';
if ($_POST['submitbutton']) {
echo "Value:|".$_POST['checkdata']."|";
}