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   </th>
<th>Name       </th>
<th>Class   </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       </th>
<th>Department   </th>
<th>Class   </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);
?>
Related
The code below searches employees on basis of employee number entered in textbox or first name but I have the first name in Arabic and it is not matching. Image is attached which shows priperly arabic on broser but not it is searched by textbox I dont know why
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Contacts</title>
<link rel="stylesheet" href="css/insert.css" />
<link rel="stylesheet" href="css/navcss.css" />
</head>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<div class="maindiv">
<?php include("includes/head.php");?>
<?php include("menu.php");?>
<!--HTML form -->
<div class="form_div">
<h3>Search Records:</h3>
<p>You may search by Employee Number</p>
<form method="post" action="search.php?go" id="searchform">
<input type="text" class="input" name="emp_number">
<input type="submit" class="input" name="submit" value="Search">
</form>
<?php
include 'connect-db.php';
//do something here in code
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[a-zA-Z0-9,]+$/", $_POST['emp_number'])){
//if(preg_match("/\p{Arabic}/u", $_POST['first_name'])){
$emp_number=$_POST['emp_number'];
//$first_name=$_POST['first_name'];
//connect to the database
//$db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
//$mydb=mysql_select_db("mydb");
//-query the database table
//mysqli_query($connection,"SET CHARACTER SET utf8");
$sql="SELECT * FROM employees WHERE emp_number LIKE '%" . $emp_number . "%' OR first_name LIKE '%" . $emp_number."%'";
//-run the query against the mysql query function . $name .
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$FirstName =$row['first_name'];
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>First Name</th> <th>Last Name</th><th>Employee Number</th><th>Department</th><th>Email</th><th>Total Printers</th><th>Total Scanners</th><th>Total PCs</th><th>Total Telephones</th></tr>";
//-display the result of the array
echo "<tr>";
echo '<td>' . $row['first_name'] . '</td>';
echo '<td>' . $row['last_name'] . '</td>';
echo '<td>' . $row['emp_number'] . '</td>';
echo '<td>' . $row['department'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['total_printers'] . '</td>';
echo '<td>' . $row['total_scanners'] . '</td>';
echo '<td>' . $row['total_pc'] . '</td>';
echo '<td>' . $row['total_phones'] . '</td>';
echo '<td class="forscreen">View</td>';
echo '<td class="forscreen">Edit</td>';
echo '<td class="forscreen">Delete</td>';
echo '<td class="forscreen">View Accessories</td>';
echo '<td class="forscreen">Add Accessories</td>';
//echo '<button class="btnPrint" type="submit" value="Submit" onclick="window.print()">Print</button>';
//echo '<button class="btnPrevious" type="submit" value="Submit" onclick="history.back(); return false;">Return to previous page</button>';
/*echo "<ul>\n";
echo "<li>" . "" .$FirstName . "</li>\n";
echo "</ul>"; */
echo "</tr>";
}
// close table>
echo "</table>";
}
else{
echo "<p>Please enter a search query</p>";
}
}
}
?>
<div class="forscreen">
<br />
<button class="btnPrint" type="submit" value="Submit" onclick="window.print()">Print</button>
<button class="btnPrevious" type="submit" value="Submit" onclick="history.back(); return false;">Return to previous page</button>
</div>
</div>
</div>
</body>
</html>
You should check your database tables default collation. I often work with korean charachters and I always make sure everything is set tu utf8. (HTML / PHP headers and DB tables collation).
This may not be required in modern versions of PHP, but on the very first line of your file, add the following:
<?php
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
?>
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
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");
Can someone take a look at this and maybe see why this is not updating my database?
It is a form that pulls data from my database and when I click the update button it SHOULD update the database, but it dosn't.
Is there a way to show if it is returning a error?
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Beerlist Admin</title>
</head>
<body>
<?php include '../beerlist/config.php'; ?>
<table border="0" width="95%" align="center" cellspacing="0" cellpadding="5">
<tr>
<?php
#///////////////////////////////////
#//////////// ALL BEERS/////////////
#///////////////////////////////////
$sql = "SELECT * FROM bottles ORDER BY name";
$mydata = mysql_query($sql,$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE bottles SET new='$_POST[new]', name='$_POST[name], style='$_POST[style], location='$_POST[location], size='$_POST[size], abv='$_POST[abv], number='$_POST[number], price='$_POST[price]' WHERE name='$_POST[hidden]'";
mysql_query ($UpdateQuery, $con);
};
while($record = mysql_fetch_array($mydata)){
echo "<form action=beerlist_admin.php method=post>";
echo "<td><input size=7 type=text name=new value=\"" . $record['new'] . "\" > ";
echo "<input type=text size=50 name=name value=\"" . $record['name'] . "\" >";
echo "<input type=text size=20 name=style value=\"" . $record['style'] . "\" >";
echo "<input type=text size=20 name=location value=\"" . $record['location'] . "\" >";
echo "<input type=text size=7 name=size value=\"" . $record['size'] . "\" >";
echo "<input type=text size=5 name=abv value=\"" . $record['abv'] . "\" >";
echo "<input type=text size=5 name=number value=\"" . $record['number'] . "\" >";
echo "<input type=text size=7 name=price value=\"" .$record['price'] . " \" >";
echo "<input type=hidden name=hidden value=" . $record['number'] . "\"> <input type=submit name=update value=update></td></tr>";
echo "</form>";
}
echo "</table>"
?>
</body>
</html>
First and foremost, mysql_* has been deprecated and you should consider migrating to MySQLi or PDO and be prepared for when mysql_* gets removed so your site does not stop working.
Your code is wide open to SQL Injection you should use mysql_real_escape_string($variable) to prevent that.
With a combination of sprintf you can further define what kind of variable you are expecting, for example:
%s - is meant to be used for string data.
%d - is meant to be used for numbers, and presented as a (signed) decimal number.
On your the below example I've used only %s as I am unsure of the data you're trying to insert and your database table field types.
Your UPDATE query is poorly written and may cause issues in different cases depending on the data you provide it with so you should always be using the mysql_error at least during the development process so you can debug this sort of errors and once you move to a live site make those errors get logged silently to file instead.
You don't need to always use echo to print every little piece of HTML, see how I've changed your WHILE code.
Also the way you're using the form within the table while it may work its not the correct way.
Since you have not posted the contents of config.php I can't tell if there is any issues in there as well.
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Beerlist Admin</title>
</head>
<body>
<?php
$con = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$con)
{
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('beerlist', $con);
if (!$db_selected)
{
die ('Can\'t use foo : ' . mysql_error());
}
?>
<table border="0" width="95%" align="center" cellspacing="0" cellpadding="5">
<?php
if (isset($_POST['update']))
{
$UpdateQuery = sprintf("UPDATE bottles
SET new='%s',
name='%s',
style='%s',
location='%s',
size='%s',
abv='%s',
number='%s',
price='%s'
WHERE name='%s'",
mysql_real_escape_string($_POST['new']),
mysql_real_escape_string($_POST['name']),
mysql_real_escape_string($_POST['style']),
mysql_real_escape_string($_POST['location']),
mysql_real_escape_string($_POST['size']),
mysql_real_escape_string($_POST['abv']),
mysql_real_escape_string($_POST['number']),
mysql_real_escape_string($_POST['price']),
mysql_real_escape_string($_POST['hidden']));
if (!mysql_query($UpdateQuery))
{
die('Invalid query: ' . mysql_error());
}
}
$sql = "SELECT *
FROM bottles
ORDER BY name";
$mydata = mysql_query($sql);
if (!$mydata)
{
die('Invalid query: ' . mysql_error());
}
while($record = mysql_fetch_array($mydata))
{
?>
<form action="beerlist_admin.php" method="post">
<tr><td>
<input size="7" type="text" name="new" value="<?php echo $record['new']; ?>">
<input type="text" size="50" name="name" value="<?php echo $record['name']; ?>">
<input type="text" size="20" name="style" value="<?php echo $record['style']; ?>">
<input type="text" size="20" name="location" value="<?php echo $record['location']; ?>">
<input type="text" size="7" name="size" value="<?php echo $record['size']; ?>">
<input type="text" size="5" name="abv" value="<?php echo $record['abv']; ?>">
<input type="text" size="5" name="number" value="<?php echo $record['number']; ?>">
<input type="text" size="7" name="price" value="<?php echo $record['price']; ?>">
<input type="hidden" name="hidden" value="<?php echo $record['number']; ?>">
<input type="submit" name="update" value="update">
</td></tr>
</form>
<?php
}
?>
</table>
</body>
</html>
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 = "...