php form with list box is not working ? - php

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 = "...

Related

Updating a mysqli table with a php form

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>

simple attendance system with radio button

I am making a simple attendance class system
I have this php code with radio buttons here.. now i put it in a table
I can only choose one radio button on the entire table instead of one radio button per registered account
<?php
session_start();
if(!isset($_SESSION["in"]))
{
header("Location: log.php");
}
?>
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Attendance</title>
</head>
<body>
<center>
<br />
<?php
echo "Today is " . date("m/d/Y") . "<br><br><br><br>";
?>
<?php
$conn= new mysqli("localhost", "root", "", "dbform");
$sql = "SELECT * FROM tblusers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table width='1300' border='6'>
<tr>
<th width='100'>ID</th>
<th width='100'>Lastname</th>
<th width='100'>Firstname</th>
<th width='100'>Sex</th>
<th width='100'>Attendance</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr> ";
echo "<td align='center'>2016" . $row['id'] . "</td>";
echo "<td align='center'>" . $row['lastname'] . "</td>";
echo "<td align='center'>" . $row['firstname'] . "</td>";
echo "<td align='center'>" . $row['sex'] . "</td>";
?>
<form method="post" action="Succes_Submit_Attendace.php" name="submit_attendance">
attendance =
<td align='center'> <label><input type="radio" name="attendance" value="present">Present</label>
 <label><input type="radio" name="attendance" value="absent">Absent</label><br /><br />
<?php
$row['attendance'] ;
?> </td>
<?php
echo " </tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
$conn->close();
?>
<br>
<br>
<input type="reset"> <input type="submit" value="Submit Attendance"></h4>
</form>
<div align="right">
<h3>Back</h2>
</div>
All the radio buttons are of same name so you need to group them by row id. Change the input type to below :
<label><input type="radio" name="attendance[<?php echo $row['id']; ?>]" value="present">Present</label>
 
<label><input type="radio" name="attendance[<?php echo $row['id']; ?>]" value="absent">Absent</label><br /> <br />
Also there are HTML errors in your code. Please fix them

Update table in phpmyadmin using input

I have been trying to make an online gateway for my college, where staff can nominate students for particular post or prizes. I used php with css for front end and phpmyadmin for back end. But once i give the input(regno) to nominate a student, it doesn't update in the table. Can anyone help? This is my code:
<!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 http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>NOMINATE ENTRIES</title>
<meta author="" content="">
<link rel="stylesheet" type="text/css" href="view.css" media="all">
</head>
<body id="main_body" >
<img id="top" src="top.png" alt="">
<div id="form_container">
<h1><a>Nominate Entries</a></h1>
<form name="form5" class="appnitro" method="post" action="test.php">
<div class="form_description">
<center><h2>Students Database</h2></center>
<p><center><font size='3'>
<?php
$con=mysqli_connect("localhost","staff","12345","mop");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `student` WHERE `Nominated` = 0");
echo "<table border='1'>
<tr>
<th>Register No</th>
<th>Department &nbsp </th>
<th>Name &nbsp &nbsp &nbsp </th>
<th>Class &nbsp </th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['RegNo'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Department'] . "</td>";
echo "<td>" . $row['Class'] . "</td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['submit']))
{
$regno = $_POST['regno'];
$reason = $_POST['reason'];
$sql = "UPDATE `mop`.`student` SET `Nominated` = \'1\' WHERE `student`.`RegNo` = 1106103;";}
mysqli_close($con);
?>
</center></font>
</p>
</div>
<b>Enter Register Number <font color='red'>*</font> </b> <input type="text" id="regno" name="regno"><br>
<b>Enter Reason <font color='red'>*</font> </b> <input type="text" id="reason" name="reason"><br>
<ul >
<center><li class="buttons">
<input type="hidden" name="form_id" value="768845" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" /></center>
</li>
</ul>
</form>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>
</html>
You didn't actually send your query.
$result = $connection -> query($sql);
// Or, since it is only an update
$connection -> query($sql);
Where $connection is the connection to your DB
i have changed the whole file like this sir. thanks all for ur input wanted to share it for others:
here is my form.php file:
<!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 http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>NOMINATE ENTRIES</title>
<meta author="" content="">
<link rel="stylesheet" type="text/css" href="view.css" media="all">
</head>
<body id="main_body" >
<img id="top" src="top.png" alt="">
<div id="form_container">
<h1><a>Nominate Entries</a></h1>
<form name="form" class="appnitro" method="post" action="test.php">
<div class="form_description">
<center><h2>Students Database</h2></center>
<p><center><font size='3'>
<?php
$con=mysqli_connect("localhost","staff","123456","mop");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM student");
echo "<table border='1'>
<tr>
<th>Register No</th>
<th>Name &nbsp &nbsp &nbsp </th>
<th>Department &nbsp </th>
<th>Class &nbsp </th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['RegNo'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Department'] . "</td>";
echo "<td>" . $row['Class'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</center></font>
</p>
</div>
<b>Enter Register Number <font color='red'>*</font> </b> <input type="text" name="regno"><p>
<b>Enter Reason <font color='red'>*</font> </b> <input type="text" name="reason"><p>
<ul >
<center><li class="buttons">
<input type="hidden" name="form_id" value="768845" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" /></center>
</li>
</ul>
</form>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>
</html>
it refers to the test.php file and here is that file too:
<?php
$con=mysqli_connect("localhost","staff","123456","mop");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="UPDATE student SET Reason = '$_POST[reason]' WHERE RegNo ='$_POST[regno]'";
if (!mysqli_query($con,$sql1))
{
die('Error: ' . mysqli_error($con));
}
else
{
$sql2="INSERT INTO nominated select * from student where regno = '$_POST[regno]'";
if (!mysqli_query($con,$sql2))
{
die('Error: ' . mysqli_error($con));
}
else
{
$sql3="DELETE from student where regno = ".intval($_POST["regno"]);
if (!mysqli_query($con,$sql3))
{
die('Error: ' . mysqli_error($con));
}
}
}
header("location:form5_1.php");
mysqli_close($con);
?>

display dynamic table after clicking submit button

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
}

how can i Select data from database using input type box and print on same page

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");

Categories