My aim to is to Update Value in Database By using Update Query . On my first page i have just displayed database table in webpage. Then by using hyperlink i have to click on Edit to second page "edit.php".While on first page i have to get the value of id and send it to second page. Where a input form is displayed which gets Value casually but Id through hidden tag. On third page getting the values query is implented but the value of id is missing.
First Page
<html>
<head>
<title>Assignment</title>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","");
// Check connection
if (!mysql_connect()) {
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$db=mysql_select_db("assignment",$con);
$result = mysql_query("SELECT * FROM teacher ",$con);
?><table cellpadding="2px" border="2px"><?php
while($row = mysql_fetch_array($result)) {
?> <tr>
<td><a href="edit.php?id=<?php
echo $row['id']; ?>">Edit</a > Delete
</td><td>
<?php
echo $row['id']; ?></td><td> <?php echo $row['name'];?></td><td><?php echo $row['program']; ?></td>
<?php }
?></table><?php
mysql_close($con);
?>
</body>
</html>
Secnod Page edit.php
<html>
<head>
<title>Assignment Edit</title>
</head>
<body>
<?php
$id = $_GET['id'];
?>
<form action="update.php" method="get">
Address <input type="text" name="program"><br>
<input type="hidden" name="id" value='<?php $id?>'>
<input type="submit" name="submit">
</form>
</body>
</html>
Third Page update.php
<html>
<head>
<title>Update Page</title>
</head>
<body>
<?php
$add=$_GET['program'];
$id=$_GET['id'];
$con=mysql_connect("localhost","root","");
// Check connection
if (!mysql_connect()) {
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$db=mysql_select_db("assignment",$con);
$query = "UPDATE teacher SET program='$add' WHERE id =".$id;
echo $query;
$result = mysql_query($query,$con);
/* while($row = mysql_fetch_array($result)) {
echo $row['id'] ." " . $row['name']." ". $row['address']."<br>";
}
mysql_close($con);
*/
?>
</body>
</html>
output
UPDATE teacher SET program='openSource' WHERE id =
you need to change this
<input type="hidden" name="id" value='<?php $id?>'>
to
<input type="hidden" name="id" value='<?php echo $id?>'>
(or)
<input type="hidden" name="id" value='<?=$id?>'>
Related
This is my db : link link
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM org_insert");
echo "<!doctype html>
<html lang=\"en\">
<head>
<!-- Bootstrap CSS -->
<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css\">
<title>Hello, world!</title>
</head>
<body>
<table border='1'>
<tr>
<th>below_whom</th>
<th>name</th>
</tr>";
$row = mysqli_fetch_array($result);
#echo '<pre>'; print_r($row); echo '</pre>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['below_whom'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<div class="form-group">
<label for="usr">below_whom:</label>
<input type="text" name ="below_whom" id="below_whom" class="form-control">
</div>
<div class="form-group">
<label for="usr">name:</label>
<input type="text" name ="name" id="name" class="form-control">
</div>
<form method="post">
<input type="button" name="submit" id="submit" class="btn btn-primary" value="submit"/>
</form>
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit']))
{
if($below_whom !=''||$name !=''){
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
mysqli_close($con);
echo"</body>
</html>";
?>
The text under p tag isn't getting executed, ie, the program is not going inside the if statement itself. I have rechecked the syntax, what is the problem? Is the syntax incorrect? I am pretty sure the connection with sql is correct. I have also refereed to some articles, still I am stuck here.
use post variables before if loop as shown below
if(isset($_POST['submit']))
{
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
if($below_whom !=''||$name !=''){
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
and in HTML Code add type as submit and start form tag before div as
<form method="post" action=""> and closes after input tag
<input type="submit" name="submit" id="submit" class="btn btn-primary" value="submit"/>
One of the issues that I am able to see is that Your query should be:
$query=mysqli_query("INSERT INTO `org_insert`(`below_whom`,`name`) VALUES ('$below_whom','$name')");
Hope this helps.
Change the mysqli_fetch_array to mysqli_fetch_assoc or add a parameter too
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
I displayed a set of questions.
On selecting one of those questions for which I required the answers to be posted on the same page, but the problem is I am not getting id parameter from url which I am passing on selectiong the question.
Now when I am trying to answer the question which is selected, I will need the id of question to post the answer of that perticular question which I already have in url as a parameter for Example: id=1.
Here is the body section of html page:
<?php
include("menu/menu.php");
$sqli = "SELECT * FROM forum_question where id='$id'";
$result=mysqli_query($conn,$sqli);
?>
<form action="submit_answer.php" method="post" name="answers">
<br> <br> <br>
<?php
while($row = mysqli_fetch_array($result))
echo "Q".$row['detail'];
?>
<br>
answers:<br>
<textarea class="tinymce" name="answers"></textarea>
<input type="hidden" name="id" value="<?php echo $id;?>">
<br> <br>
<input type="submit" value="submit" name="submit">
After submit the page "submit_answer.php", Code is:
<?php
include'config.php';
if($conn){
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$answers = $_REQUEST['answers'];
$id= $_GET ['id'];
}
$sqli= "INSERT INTO answers (answers)
VALUES ('$answers')";
if (mysqli_query( $conn,$sqli))
{
echo "New record created successfully";
header("location:answer.php?id='$id'");
} else {
echo "Error: " . $sqli . "<br>" . $conn->error;
}
}else{
}
mysqli_close($conn);
?>
Basically I am very much fresher in Php I just want to know how should I get the id of question and submit it to the "submit_answer.php" with the answer content.
just take a hidden field below the answer field and get the url parameter to that hidden field on page load, as said by user buivankim2020 and submit submit_answer.php,
after submit get the value of that field in variable like what you do for getting the answer..
You should change it (add hidden input for id in markup html)
<?php
include'config.php';
//session_start();
$id= $_GET ['id'];
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="tinymce/js/jquery.min.js"></script>
<script type="text/javascript" src="tinymce/plugin/tinymce/tinymce.min.js"></script>
<script type="text/javascript" src="tinymce/plugin/tinymce/init-tinymce.js"></script>
</head>
<body>
<div id="container">
<div id="main">
<?php
include("menu/menu.php");
$sqli = "SELECT * FROM forum_question where id='$id'";
$result=mysqli_query($conn,$sqli);
?>
<form action="submit_answer.php" method="post" name="answers">
<br> <br> <br>
<?php
while($row = mysqli_fetch_array($result))
echo "Q".$row['detail'];
?>
<br>answers:<br>
<textarea class="tinymce" name="answers"></textarea>
<input type="hidden" name="id" value="<?php echo $id;?>">
<br> <br>
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
submit_answer.php
<?php
include'config.php';
if($conn){
if (isset($_POST['answers']) && isset($_POST['id'])) {
$answers = $_POST['answers'];
$id= $_POST['id'];
$sqli= "INSERT INTO answers (answers) VALUES ('$answers')";
if (mysqli_query( $conn,$sqli))
{
echo "New record created successfully";
header("location:answer.php?id='$id'");
} else {
echo "Error: " . $sqli . "<br>" . $conn->error;
}
}
mysqli_close($conn);
}
?>
I am trying to make a html or php page (for some own learning process) which can input 3 selection and then display there results in next page.
1- input Start Date
2- input End Date
3- Show Service Name in drop down menu via mysql query to get services names from the table
So far I have managed to get the start and end table and drop down menu which successfully query the services table and shows the name, but the problem is that when i click submit i can see the results of start and end date but i am unable to see how can i add services selection in the posting.
This is my code.
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#start_datepicker").datepicker();
$("#end_datepicker").datepicker();
});
</script>
</head>
<body style="font-size:62.5%;">
<form action="test.php" method="post">
Start Date: <input type="text" name="startdate" id="start_datepicker"> <br />
End Date: <input type="text" name="enddate" id="end_datepicker"><br />
<select name="srvname">
<?php
$conn = new mysqli('localhost', 'root', 'SQLPASS', 'radius')
or die ('Cannot connect to db');
$result = $conn->query("select srvname name from rm_services");
while ($row = $result->fetch_assoc()) {
echo "<option value=\"" . $row["id"] . "\">" . $row["name"] . "</option>";
}
?>
</select>
<input type="submit" value="Submit:">
</form>
</body>
</html>
and this is test.php which will per form action shows the date
<?php
$STARTDATE = $_POST['startdate'];
$ENDDATE = $_POST['enddate'];
$SRVNAME = $_POST['srvname']; //gets the value -> $row["id"]
echo "<h2>You have entered the following information:</h2>";
echo "<pre>$STARTDATE</pre> ";
echo "<pre>$ENDDATE</pre>";
echo "<pre>$SRVNAME</pre>";
?>
This one should work, and for the future: if you post something on stackoverflow, please post formatted code, it's way easier to edit and especially to read it...
your index.php or whatever...
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#start_datepicker").datepicker();
$("#end_datepicker").datepicker();
});
</script>
</head>
<body style="font-size:62.5%;">
<form action="test.php" method="post">
Start Date: <input type="text" name="startdate" id="start_datepicker"> <br />
End Date: <input type="text" name="enddate" id="end_datepicker"><br />
<select name="srvname">
<?php
$conn = new mysqli('localhost', 'root', 'SQLPASS', 'radius')
or die ('Cannot connect to db');
$result = $conn->query("select id, name from rm_services");
while ($row = $result->fetch_assoc()) {
echo "<option value=\"" . $row["id"] . "\">" . $row["name"] . "</option>";
}
?>
</select>
<input type="submit" value="Submit:">
</form>
</body>
</html>
your test.php
<?php
$STARTDATE = $_POST['startdate'];
$ENDDATE = $_POST['enddate'];
$SRVNAME = $_POST['srvname']; //gets the value -> $row["id"]
echo "<h2>You have entered the following information:</h2>";
echo "<pre>$STARTDATE</pre> ";
echo "<pre>$ENDDATE</pre>";
echo "<pre>$SRVNAME</pre>";
?>
I didn't test it, but actually it should work...
I have a form on one page linking to a PHP file (action), now the PHP result is being displayed in this PHP file/page. But I want the result to be displayed on the page with the form. I have searched thoroughly and couldn't find it anywhere. Perhaps any of you can help?
Code: /citizens.php (main page)
<form method="post" action="/infoct.php">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="submit">
</form>
Code: /infoct.php
<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="refresh" content="0; url=/citizens.php" /> -->
</head>
<body>
<?php {
$ID2 = isset($_POST['ID']) ? $_POST['ID'] : false;
}
$connect = mysql_connect('localhost', 'root', 'passwd');
mysql_select_db ('inhabitants');
$sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID2";
$res = mysql_query($sql);
echo "<P1><b>Citizen Identification number is</b> $ID2 </p1>";
while($row = mysql_fetch_array($res))
{
echo "<br><p1><b>First Name: </b></b>", $row['Name'], "</p1>";
echo "<br><p1><b>Surname: </b></b></b>", $row['Surname'], "</p1>";
echo "<br><p1><b>Date of birth: </b></b></b></b>", $row['DOB'], "</p1>";
echo "<br><p1><b>Address: </b></b></b></b></b>", $row['Address'], "</p1>";
echo "<br><p1><b>Background information: </b><br>", $row['RPS'], "</p1>";
}
mysql_close ($connect);
?>
</body>
</html>
My fixed code thanks to Marc B
<form method="post">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="submit">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$ID = isset($_POST['ID']) ? $_POST['ID'] : false;
$connect = mysql_connect('fdb13.biz.nf:3306', '1858208_inhabit', '12345demien12345');
mysql_select_db ('1858208_inhabit');
$sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID";
$res = mysql_query($sql);
if ($ID > 0) {
echo "<p><b>Citizen Identification number is</b> </p>";
while($row = mysql_fetch_array($res))
echo "<br><p><b>Surname: </b></b></b>", $row['Surname'], "</p>";
echo "<br><p><b>First Name: </b></b>", $row['Name'], "</p>";
echo "<br><p><b>Date of birth: </b></b></b></b>", $row['DOB'], "</p>";
echo "<br><p><b>Address: </b></b></b></b></b>", $row['Address'], "</p>";
echo "<br><p><b>Background information: </b><br>", $row['RPS'], "</p>";
mysql_close ($connect);
}
else {
echo "<p>Enter a citizen ID above</p>";
}
}
?>
DB Snap
A single-page form+submit handler is pretty basic:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... form was submitted, process it ...
... display results ...
... whatever else ...
}
?>
<html>
<body>
<form method="post"> ... </form>
</body>
</html>
That's really all there is.
Use code on the same page (citizens.php)
<?php
if (isset($_POST)) {
Do manipulation
}
?>
Else use ajax and remove action method from form.
<form method="post" id="contactForm">
<input type="text" name="ID" placeholder="ID">
<input name="set" type="buttom" id="submitId">
</form>
<script>
$("#submitId").click(function(){
var Serialized = $("#contactForm").serialize();
$.ajax({
type: "POST",
url: "infoct.php",
data: Serialized,
success: function(data) {
//var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this
// do what ever you want with the server response
},
error: function(){
alert('error handing here');
}
});
});
</script>
And in your infact.php in the end Echo the data so that ajax will have the data in return.
You could just put everything in infoct.php, like this:
<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="refresh" content="0; url=/infoct.php" /> -->
</head>
<body>
<form method="post" action="/infoct.php">
<input type="text" name="ID" placeholder="ID" value="<?php isset($_POST['ID']) ? $_POST['ID'] : '' ?>">
<input name="set" type="submit">
</form>
<?php
if (isset($_POST['ID'])) {
$ID2 = $_POST['ID']; // DO NOT FORGET ABOUT STRING SANITIZATION
$connect = mysql_connect('localhost', 'root', 'usbw');
mysql_select_db ('inhabitants');
$sql = "SELECT `Name`, `Surname`, `DOB`, `RPS`, `Address` FROM `citizens` WHERE ID = $ID2";
$res = mysql_query($sql);
echo "<P1><b>Citizen Identification number is</b> $ID2 </p1>";
while($row = mysql_fetch_array($res))
{
echo "<br><p1><b>First Name: </b></b>", $row['Name'], "</p1>";
echo "<br><p1><b>Surname: </b></b></b>", $row['Surname'], "</p1>";
echo "<br><p1><b>Date of birth: </b></b></b></b>", $row['DOB'], "</p1>";
echo "<br><p1><b>Address: </b></b></b></b></b>", $row['Address'], "</p1>";
echo "<br><p1><b>Background information: </b><br>", $row['RPS'], "</p1>";
}
mysql_close ($connect);
}
?>
</body>
</html>
Do not forget about string sanitization !
I have found the solutions to the folowing problems:
Display results on same page
Thanks to Marc B
A single-page form+submit handler is pretty basic:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... form was submitted, process it ...
... display results ...
... whatever else ...
}
?>
<html>
<body>
<form method="post"> ... </form>
</body>
</html>
That's really all there is.
Only first value is showing
I resolved this problem by adding this to my code:
while($row = mysql_fetch_array($res)) {
$surname=$row['Surname'];
$name=$row['Name'];
$dob=$row['DOB'];
$address=$row['Address'];
$RPS=$row['RPS'];
Now all the values are being displayed instead of only the first one.
Display results on same page
Well I've stumbled upon this with the same problem
and I found out you can simply require the other file.
include_once("PATH_TO_FILE")'.
in /citizens.php
<?php include_once="infoct.php" ?>
<form> ... </form>
<div>
<?php $yourdata ?>
</div>
$yourdata should be html.
Do not forget about string sanitization !
Make sure to remove action from the form
Better than having all logic and Html in one file.
I have a simple dropdown menu which posts the result to itself but when i choose one of the options in the drop down menu it does not echo back the result as expected.
I'm sure i've just missed out something simple but can't spot it. Any ideas? The form posts but does not echo back $user_settings.
<?php
include "functions.php";
connect();
$sql="SELECT user_id, user_realname FROM users ORDER BY user_realname ASC";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$name=$row['user_realname'];
$options.="<OPTION VALUE=>".$name.'</option>';
}
if(isset($_POST['submit'])){
$user_realname = $_POST['username_select'];
$user_select = mysql_query("SELECT user_id, user_realname FROM users WHERE user_realname = '$user_realname'")
or die ("Could not get user data");
while($row = mysql_fetch_array($user_select)){
$user_settings = $row['user_id'];
echo $user_settings;
}
}
?>
<html>
<head>
<body>
<form action="<?php echo $PHP_SELF;?>" method="POST">
<tr><label>Choose User to Edit</tr>
<tr><SELECT NAME="username_select"><OPTION VALUE=""></option>User's Name<?php echo $options;?></SELECT></label></tr>
<tr><input type="submit" value="submit" name="submit"></tr>
</form>
<?php echo $user_settings;?>
<br/>
Go Back
</body>
</head>
</html>
User $_SERVER['PHP_SELF'] instead of $PHP_SELF