how to display message on html page using php - php

I am doing project on marks management system. In the project i have a task of updating the marks of student.Suppose if the updation is successful i should display a message that it is successful otherwise
unsuccessful should be displayed. I am doing my project using php and html. The code is as follows.
update marks
user_id: <input type="text" name="userid"><br>
Branch<select name="Branch">
<option value="cse">CSE</option>
<option value="eee">EEE</option>
<option value="ece">ECE</option>
</select>
Marks<br><input type="text" name="marks" size="40"></br>
<select name="Subject">
<optgroup label="CSE">
<optgroup label="sem 4">
<option value="dbms">DBMS</option>
</optgroup>
<optgroup label="EEE">
</optgroup>
<optgroup label="ECE">
</optgroup>
</select>
Semester<select name="Semester">
<option value="sem 1">SEM 1</option>
<option value="sem 2">SEM 2</option>
<option value="sem 3">SEM 3</option>
<option value="sem 4">SEM 4</option>
<option value="sem 5">SEM 5</option>
<option value="sem 6">SEM 6</option>
<option value="sem 7">SEM 7</option>
</select>
<input id="button" type="submit" name="submit" value="SUBMIT">
</form>
</body>
</html>
The following one is php code
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
function updatem()
{
session_start();
$marks=$_POST['marks'];
$branch=$_POST['Branch'];
$semester=$_POST['Semester'];
$subject=$_POST['Subject'];
$userid=$_POST['userid'];
if((!empty($_POST['userid']))
&&(!empty($_POST['marks']))
&&(!empty($_POST['Subject']))
&&(!empty($_POST['Semester']))
&&(!empty($_POST['Branch'])))
{
$query=mysql_query("UPDATE marks_list SET marks_obt=$marks
WHERE username_id=$userid
AND branch_id=(SELECT branch_id FROM branch WHERE branch_name='$branch') AND
semester_id=(SELECT semester_id FROM semester WHERE semester_name='$semester')
AND subject_code=(select subject_code FROM subcodes WHERE
branch_id=(SELECT branch_id FROM branch WHERE branch_name='$branch')
AND
semester_id=(SELECT semester_id FROM semester WHERE semester_name='$semester'))") or die("insertion unsuccessful".mysql_error());
header("Location: update_marks.html");
}
}
if(isset($_POST['submit']))
{
updatem();
}
?>
thanks in advance..

Add this code after your update query
if(mysql_affected_rows()>0)
{
$_SESSION['message']='This is your message';
}
Now in the file Where you want to display the value
Add this code
Important Note: Always start session at the top of the page.
session_start();
echo $_SESSION['message'];

Related

How to react on click from the line in list?

Someone can help me to solve this
I have this html select tag, and I have a button,
how to react on the one line from this list and press the button, so this line will be redirected to another page?
<p><strong>Select your favorite species of flamingo.</strong></p>
<select size="6">
<option value="American">American flamingo</option>
<option value="Andean">Andean flamingo</option>
<option value="Chilean">Chilean flamingo</option>
<option value="Greater">Greater flamingo</option>
<option value="James's">James's flamingo</option>
<option value="Lesser">Lesser flamingo</option>
</select>
<input name="submit" type="submit" value=" OK!">
You don't need javascript for this if you have some php in place. It's standard form controls functionality. I'm assuming you have some kind of script reading some parameter. I'm going to call the script photos.php and the parameter species.
<form method="get" action="photos.php">
<select name="species">
<option value="">Select one...</option>
<option value="American">American flamingo</option>
<option value="Andean">Andean flamingo</option>
<option value="Chilean">Chilean flamingo</option>
<option value="Greater">Greater flamingo</option>
<option value="James's">James's flamingo</option>
<option value="Lesser">Lesser flamingo</option>
</select>
<input name="submit" type="submit" value=" OK!">
</form>
Then, in your php, check the value and redirect:
if (isset($_GET['species'])) {
if (in_array($_GET['species'], ['American', 'Andean'])) { // Other species omitted
header('Location: /photos/'. $_GET['species'] . '.html';
exit();
}
} else {
// Output your selection form
}
If I don't misunderstood your question then you can do this way using JS, Also you can do this with simple .php form submit on a particular page and then check which option value is selected. Finally redirect to a flower page based on the selected option value as #msg pointed.
window.location.replace will redirect you to the page as per your selection.
window.onload=function() {
document.getElementById("form1").onsubmit=function() {
let selected_option = document.getElementById('option').value;
window.location.replace(selected_option);
return false;
}
}
<form id="form1">
<p><strong>Select your favorite species of flamingo.</strong></p>
<select id="option" size="6" name="flamigos">
<option value="American.html">American flamingo</option>
<option value="Andean.html">Andean flamingo</option>
<option value="Chilean.html">Chilean flamingo</option>
<option value="Greater.html">Greater flamingo</option>
<option value="James's.html">James's flamingo</option>
<option value="Lesser.html">Lesser flamingo</option>
</select>
<input name="submit" type="submit" value=" OK!">
</form>

Pushing Data to a MySQL Database from a Form

I am trying to send data from an HTML form to a MySQL database in phpmyadmin. I have a database named bhs2018 and a table called game 1. Here are the contents of that table.
Here is my form:
<form name="game" action="insert.php" method="post">
<p> <select id="player" name = 'player'>
<option value="b">B</option>
<option value="n">N</option>
<option value="a">A</option>
<option value="c">C</option>
<option value="m">M</option>
<option value="j">J</option>
<option value="ja">Ja</option>
</select>
<select id="what" name = 'what'>
<option value="shoton">Shot on Cage</option>
<option value="shotoff">Shot off Cage</option>
<option value="goal">Goal</option>
<option value="countergoal">Goal on Counter</option>
<option value="countershot">Shot on Counter</option>
<option value="assist">Assist</option>
<option value="block">Block</option>
<option value="steal">Steal</option>
<option value="turnover">Turnover</option>
<option value="drawn">Ejection Drawn</option>
<option value="ejected">Ejected</option>
</select>
<select id="where" name = 'where'>
<option value="set">Set</option>
<option value="navy">Navy</option>
<option value="leftwing">1/2 side past 5</option>
<option value="rightwing">4/5 side past 5</option>
<option value="point">Point/3</option>
<option value="lefttwo">1/2 side 2 meter</option>
<option value="righttwo">4/5 side 2 meter</option>
<option value="1">6 on 5 1</option>
<option value="2">6 on 5 2</option>
<option value="3">6 on 5 3</option>
<option value="4">6 on 5 4</option>
<option value="5">6 on 5 5</option>
<option value="6">6 on 5 6</option>
</select>
<select id="quarter" name = 'quarter'>
<option value="q1">Quarter 1</option>
<option value="q2">Quarter 2</option>
<option value="q3">Quarter 3</option>
<option value="q4">Quarter 4</option>
</select>
<select id="time" name = 'time'>
<option value="0:30">0:30</option>
<option value="1:00">1:00</option>
<option value="1:30">1:30</option>
<option value="2:00">2:00</option>
<option value="2:30">2:30</option>
<option value="3:00">3:00</option>
<option value="3:30">3:30</option>
<option value="4:00">4:00</option>
<option value="4:30">4:30</option>
<option value="5:00">5:00</option>
<option value="5:30">5:30</option>
<option value="6:00">6:00</option>
<option value="6:30">6:30</option>
<option value="7:00">7:00</option>
</select>
Notes: <input type="text" id = 'notes' name = 'notes'>
<button type="submit" onclick="save()"> Save </button> </p>
</form>
Whenever I click my "Save" button, the insert.php script loads. Instead of echoing something, it just shows the code. Here is insert.php.
<?php
$con = mysqli_connect('127.0.0.1','root','password'(my actual password is here);
if(!$con){
echo 'Not Connected to Server';
}
if (!mysqli_select_db($con,'bhs2018')){
echo 'Not Selected';
}
$Player = $_POST['player'];
$Quarter = $_POST['quarter'];
$Time = $_POST['time'];
$Where = $_POST['where'];
$Notes = $_POST['notes'];
$What = $_POST['what'];
$sql = "INSERT INTO game1 (player,quarter,time1,where1,notes,what) VALUES ('$Player', '$Quarter', '$Time', '$Where','$Notes','$What')";
if(!mysqli_query($con,$sql)){
echo'Not Inserted';
}
else{
echo 'Inserted';
}
header('refresh:2; url=index.html');
?>
What is looking wrong with my code? Why does it not run the php script? Thank you so much!
Check your database. Your PRIMARY KEY player should be an integer like int(30) or bigint(200) but not varchar. Create a new column for PRIMARY KEY something like player_id and shift player to next column. Also when primary key is an integer, there is no manual insertion option for inputting it's value in your form. So make your PRIMARY KEY is set to AUTO_INCREMENT in case you don't want to insert it's value manually.

PHP and SQL - How can I make this ID into a variable?

I am making a PHP website, and I want my user ID to load into the tickets table when you order a ticket. There is a webpage where you can select the ticket, the amount, and the ID is already given as a label.
The first code adds the data to my database
function best(){
global $db, $errors;
// receive all input values from the form
$ticket = e($_POST['tickety']);
$aantall = e($_POST['aantal']);
$usidd = e($_POST['usid']);
// form validation: ensure that the form is correctly filled
if (empty($ticket)) {
array_push($errors, "Ticket is verplicht");
}
if (empty($aantall)) {
array_push($errors, "Aantal is verplicht");
}
// order ticket if there are no errors in the form
if (count($errors) == 0) {
$con = new PDO("mysql: host=localhost; dbname=website", "root", "");
mysqli_query($db, $query);
$_SESSION['success'] = "Ticket besteld";
header('location: index.php');
$query = "INSERT INTO tickety (ticket, aantal, userid)
VALUES('$ticket', '$aantall', '$usidd')";
mysqli_query($db, $query);
}
}
This code defines the input fields
<form method="post" action="ticketpag.php">
<?php echo display_error(); ?>
<div class="input_group">
ID
</br>
<input type='hidden' name='usid'>
<h4><?php
echo $_SESSION['user']['id'];
?></h4>
</input>
Ticket
</br>
<select name='tickety'>
<?php
$query = "SELECT * FROM `stok`";
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<option>".$row['naam']."</option>";
}
?>
</select>
</br>
Aantal
</br>
<select name="aantal">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
</select>
</div>
<div class="input-group">
<button type="submit" class="btn" name="bestel_btn"> Bestellen</button>
Terug
</div>
</form>
Add $_SESSION['user']['id']; to an input field instead of a label and it should work.
Example:
<input type="hidden" name="usid" id="usid" value="<?php echo $_SESSION['user']['id'];?>" />
Hidden fields are not shown to a user.
Addition:
But, as #mickmackusa mentioned, sending existing SESSION data using a POST request is a bad-practise. Instead test and see what happens when you echo $_SESSION['user']['id'] inside of best(). You should not be using POST to deliver existing SESSION data.

How do I use PHP to query my SQL database using SELECT tag in html

Basically I'd like to use the choices which the user has selected from a different select tags, and in essence store these as variables which I can then use to query my database in SQL.
My HTML code is here:
<div id ="search_elements">
<img src="UniSelect.jpeg">
<select>
<option selected disabled>Select a university</option>
<option value="ucl">UCL</option>
<option value="kings">Kings College</option>
<option value="imperial">Imperial College</option>
<option value="lse">London School of Economics</option>
</select>
<img src="PriceSelect.jpeg">
<select>
<option selected disabled>Select a weekly rent price</option>
<option value="50">0-£50</option>
<option value="100"> £100-£150</option>
<option value="150">£150-200</option>
<option value="200"> £200+</option>
</select>
</div>
And the type of php i would be looking to use would be:
//$con=mysqli_connect("localhost","adam","YjM3ZTYwOTQ5OWRmYWZh","adam_...");
//if (mysqli_connect_errno())
// {
// echo "Failed to connect to MySQL: " . mysqli_connect_error();
// }
// Perform queries
//$sql=mysqli_query($con,"SELECT CONTENT FROM Blog WHERE ID = 01");
//$result=mysqli_fetch_assoc($sql);
//echo $result['CONTENT'];
//mysqli_close($con);
To make it clear once more, I want to use the different values which the user selects, upon clicking a search button, have these query results shown in a table.
This solution a little differs from yours because you have no provided your form, submit button, etc. but in general, after a few changes, it should work too:
<form method="post" action="">
<img src="UniSelect.jpeg">
<select name="university">
<option selected disabled>Select a university</option>
<option value="ucl">UCL</option>
<option value="kings">Kings College</option>
<option value="imperial">Imperial College</option>
<option value="lse">London School of Economics</option>
</select>
<img src="PriceSelect.jpeg">
<select name="rent_price">
<option selected disabled>Select a weekly rent price</option>
<option value="50">0-£50</option>
<option value="100"> £100-£150</option>
<option value="150">£150-200</option>
<option value="200"> £200+</option>
</select>
<input type="submit" value="Submit form">
</form>
And now, to get values of these (something like this and I recommend to place it above your form):
if (!empty($_POST)) {
// Checking connection in here
$university_id = mysqli_real_escape_string($con, $_POST['university']);
$rent_price = mysqli_real_escape_string($con, $_POST['rent_price']);
$sql = mysqli_query($con, "SELECT * FROM university WHERE name = '".$university_id."'");
$result = mysqli_fetch_assoc($sql);
// Same thing goes for rent price
}

How to insert multiple data from listbox to database

Please look this link to see the code use by me
<pre>
http://jsfiddle.net/JaavS/
</pre>
<pre>
step1: select values in List A
step2: Move the values to list b
Step3:click save to database button
</pre>
After step3, the item in the list b values to be store in database. Can any one help me
Give your selects a name:
Select1:
<select name ="list1[]" id="list1" multiple="multiple" rows=2>
<option value=1>Option 1</option>
<option value=2>Option 2</option>
<option value=3>Option 3</option>
<option value=4>Option 4</option>
<option value=5>Option 5</option>
<option value=6>Option 6</option>
</select>
Select2:
<select name="list2[]" id="list2" multiple="multiple" rows=2>
</select>
In your php file:
<?php
//List 2
foreach ($_POST['list2'] as $selectedOption)
{
$query = "INSERT INTO table (option) VALUES ('$selectedOption')";
mysql_query($query) or die(mysql_error());
}
?>

Categories