Using following code I have fetched Student ID and Student name from mysql table in html table. In third column which is Obtained Marks I am getting student marks through input box. Now I want to insert all three columns (Student ID, Student Name and Obtained Marks) in new table which is testrecord.
<html>
<body>
<?php
$connection = mysqli_connect ('localhost', 'admin', 'password', 'db');
if (!$connection) {
echo 'Not connected to server';
}
$select_db = mysqli_select_db($connection, 'db');
if (!$select_db) {
echo 'Not connected to database';
}
$SelectClass = $_POST ['selectclass'];
$sql= "SELECT * FROM students WHERE class = '$SelectClass'";
$query = mysqli_query($connection, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($connection));
}
mysqli_close($connection);
?>
<body>
<div class="container">
<form class="well form-horizontal" action="insert_marks.php" method="post">
<h1><strong>Please enter marks of each student for subject</strong></h1>
<form action="" method="post">
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query)) {
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student_name" value='.$row['student_name'].'>
<td>
<div class="search-block clearfix">
<input name="obtmarks" placeholder="" type="number">
</div>
</td>
</tr>';
$total += $row['stu_id'];
$no++;
}
?>
</tbody>
</table>
<button type="submit" class="btn btn-warning" value="insert" align="right">Update<span class="glyphicon glyphicon-send"></span></button>
</form>
</div>
</body>
</html>
insert_marks.php:
<html>
<body>
<?php
$connection = mysqli_connect ('localhost', 'admin', 'password', 'db');
if (!$connection) {
echo 'Not connected to server';
}
$select_db = mysqli_select_db($connection, 'db');
if (!$select_db) {
echo 'Not connected to database';
}
//***********Form Submit Goes Here***********//
while
if($_POST) {
$student_id = $_POST['student_id'];
$student_name = $_POST['student_name'];
$student_marks = $_POST['obtmarks'];
$sql= "INSERT INTO testrecord (student_id,student_name,obtained_marks) VALUES ('$student_id','$student_name','$student_marks')";
if (mysqli_query($connection, $sql)) {
echo "Marks added successfully.";
echo "<br>";
echo "<br>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
}
mysqli_close($connection);
?>
</body>
</html>
There are 20 entries in table. After inserting marks for each student in text box, above coding inserts only last record in mysql table 'testrecord'. Can you please correct the insert_marks.php code.
Change on Html table tbody part:
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query)) {
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id[]" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student_name[]" value='.$row['student_name'].'>
<td>
<div class="search-block clearfix">
<input name="obtmarks[]" placeholder="" type="number">
</div>
</td>
</tr>';
$total += $row['stu_id'];
$no++;
}
?>
</tbody>
in insert_marks.php change while part:
//***********Form Submit Goes Here***********//
while
if($_POST) {
$student_id = $_POST['student_id'];
$student_name = $_POST['student_name'];
$student_marks = $_POST['obtmarks'];
for($i = 0; $i < count($student_id); $i++){
$sql= "INSERT INTO testrecord (student_id,student_name,obtained_marks) VALUES ('$student_id[$i]','$student_name[$i]','$student_marks[$i]')";
if (mysqli_query($connection, $sql)) {
echo "Marks added successfully.";
echo "<br>";
echo "<br>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
}
}
Try this
<html>
<body>
<?php
$connection = mysqli_connect ('localhost', 'admin', 'password', 'db');
if (!$connection) {
echo 'Not connected to server';
}
$select_db = mysqli_select_db($connection, 'db');
if (!$select_db) {
echo 'Not connected to database';
}
$SelectClass = $_POST ['selectclass'];
$sql= "SELECT * FROM students WHERE class = '$SelectClass'";
$query = mysqli_query($connection, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($connection));
}
mysqli_close($connection);
?>
<body>
<div class="container">
<form class="well form-horizontal" action="insert_marks.php" method="post">
<h1><strong>Please enter marks of each student for subject</strong></h1>
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query)) {
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student['.$no.'][student_id]" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student['.$no.'][student_name]" value='.$row['student_name'].'>
<td>
<div class="search-block clearfix">
<input name="student['.$no.'][obtmarks]" placeholder="" type="number">
</div>
</td>
</tr>';
$total += $row['stu_id'];
$no++;
}
?>
</tbody>
</table>
<button type="submit" class="btn btn-warning" value="insert" align="right">Update<span class="glyphicon glyphicon-send"></span></button>
</form>
</div>
</body>
</html>
insert_marks.php:
<html>
<body>
<?php
$connection = mysqli_connect ('localhost', 'admin', 'password', 'db');
if (!$connection) {
echo 'Not connected to server';
}
$select_db = mysqli_select_db($connection, 'db');
if (!$select_db) {
echo 'Not connected to database';
}
//***********Form Submit Goes Here***********//
if(isset($_POST['student']) && !empty($_POST['student'])) {
$queryStr = '';
$cnt = count($_POST['student']);
foreach ($_POST['student'] as $key => $student) {
$queryStr .= "('".$student['student_id']."','".$student['student_name']."','".$student['obtmarks']."') ";
if (($key + 1) != $cnt) {
$queryStr .= " , ";
}
}
if ($queryStr != '') {
$sql= "INSERT INTO testrecord (student_id,student_name,obtained_marks) VALUES $queryStr";
if (mysqli_query($connection, $sql)) {
echo "Marks added successfully.";
echo "<br>";
echo "<br>";
}
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
}
mysqli_close($connection);
?>
</body>
</html>
Related
I trying to insert my data from table_request into table_list by clicking the approve/reject button under the table.
Users will click the checkboxes to select all or select a specific row. After approve, the data in the table_request insert into table_list and deleted from table_request.
The current problem is about the foreach and inserts statement is wrong.
After I click approve, it can only insert table_request id into table_list.
This is my table_request.php
<form name="bulk_action_form" action="action.php" method="post" onSubmit="return approve_confirm();"/>
<table class="bordered">
<tr>
<th><input type="checkbox" name="select_all" id="select_all" value=""/></th>
<th>Name</th>
<th>Remark</th>
</tr>
<?php
$query = "select * from `table_request`;";
if(count(fetchAll($query))>0){
foreach(fetchAll($query) as $row){
?>
<tr>
<td><input type="checkbox" name="checked_id[]" class="checkbox" value="<?php echo $row['id']; ?>"/></td>
<td><?php echo $row['Name'] ?></td>
<td><?php echo $row['Remark'] ?></td>
</tr>
<?php } }else{ ?>
<tr><td colspan="5">No records found.</td></tr>
<?php } ?>
</table>
<input type="submit" name="approve_btn" value="Approve"/>
</form>
This is my action.php
<?php
session_start();
include_once('connection.php');
if(isset($_POST['approve_btn']))
{
$idArr = $_POST['checked_id'];
$Name = $_POST['Name'];
$Remark = $_POST['Remark'];
foreach($idArr as $key => $value)
{
$save = "INSERT INTO table_list(id,Name,Remark) VALUES ('".$value."','".$Name[$key]."','".$Remark[$key]."')";
$query = mysqli_query($conn,$save);
}
$query .= "DELETE FROM `table_request` WHERE `table_request`.`id` = '$id';";
header("Location:table_request.php");
}
?>
connection.php file
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = 'database';
// Create connection
$conn = mysqli_connect($server_name, $user_name, $password, $db_name);
// Check connection
if (mysqli_connect_errno()) {
die("Connection failed: ");
}
table_request.php file
<?php include_once('connection.php'); ?>
<form name="bulk_action_form" action="action.php" method="post" onSubmit="return approve_confirm();"/>
<table class="bordered">
<tr>
<th>Select</th>
<th>Name</th>
<th>Remark</th>
</tr>
<?php
$query = mysqli_query($conn, "SELECT * FROM table_request");
if (mysqli_num_rows($query) > 0) : foreach($query as $row) : ?>
<tr>
<td><input type="checkbox" name="checked_id[]" class="checkbox" value="
<?php echo $row['id']; ?>"/></td>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['Remark']; ?></td>
</tr>
<?php endforeach; else : ?>
<tr><td colspan="5">No records found.</td></tr>
<?php endif; ?>
</table>
<input type="submit" name="approve_btn" value="Approve"/>
</form>
action.php file
<?php
include_once('connection.php');
if(isset($_POST['approve_btn'])) {
foreach ($_POST['checked_id'] as $id) {
$query = mysqli_query($conn, "SELECT * FROM table_request WHERE id = $id");
$result = $query->fetch_object();
$save = "INSERT INTO table_list(id, Name, Remark) VALUES ('".$result->id."','".$result->Name."','".$result->Remark."')";
// For save data
mysqli_query($conn, $save);
// For delete data
mysqli_query($conn, "DELETE FROM table_request WHERE id = $id");
}
header('Location:table_request.php');
}
I want to have two identical search bars but which lead to different results. However the problem and that the result of a search is then displayed during a search on the second search bar, the second result erases the first. I do not understand how this can be possible, if there is a way to display the results at the same time on the same page, I am interested. thank you in advance
Code one
<?php
include_once ('searcht.php');
?></br><?php
include_once ('searche.php');
?>
Code for search bar one "searcht.php"
<div class="t"><form method="post" enctype="multipart/form-data">
<label>Search</label>
<input type="text" name="searcht">
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
$hostt = "localhost";
$db_namet = "photos";
$usernamet = "root";
$passwordt = "";
try{
$cont = new PDO("mysql:host={$hostt};dbname={$db_namet}", $usernamet, $passwordt);
}
catch(PDOException $exceptiont){
//to handle connection error
echo "Connection error: " . $exceptiont->getMessage();
}
if (isset($_POST["submit"])) {
$strt = $_POST["searcht"];
$stht = $cont->prepare("SELECT image, image_nom, image_text FROM images WHERE image_nom =
'$strt'");
$stht->setFetchMode(PDO:: FETCH_OBJ);
$stht -> execute();
if($rowt = $stht->fetch())
{
?>
<br><br><br>
<table>
<tr>
<th>Image</th>
<th>Nom</th>
<th>Fiche</th>
</tr>
<tr>
<td class='image'><?php print "<img src='images/$rowt->image'>";?></td>
<td><?php print $rowt->image_nom; ?></td>
<td><?php print $rowt->image_text;?></td>
</tr>
</table>
<?php
}else{
echo "Aucune personne s'apelle comme çà";
}
}
?>
</div>
Code for search bar two "searche.php"
<div class="e"><form method="post" enctype="multipart/form-data">
<label>Search</label>
<input type="text" name="searche">
<input type="submit" name="submite">
</form>
</body>
</html>
<?php
$host = "localhost";
$db_name = "photos";
$username = "root";
$password = "";
try{
$cone = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}
catch(PDOException $exception){
//to handle connection error
echo "Connection error: " . $exception->getMessage();
}
if (isset($_POST["submite"])) {
$stre = $_POST["searche"];
$sthe = $cone->prepare("SELECT image, image_nom, image_text FROM images WHERE image_nom =
'$stre'");
$sthe->setFetchMode(PDO:: FETCH_OBJ);
$sthe -> execute();
if($rowe = $sthe->fetch())
{
?>
<br><br><br>
<table>
<tr>
<th>Image</th>
<th>Nom</th>
<th>Fiche</th>
</tr>
<tr>
<td><?php echo $rowe->image_text;?></td>
<td><?php echo $rowe->image_nom; ?></td>
<td class='image'><?php echo "<img src='images/$rowe->image'>";?></td>
</tr>
</table>
<?php
}else{
echo "Aucune personne s'apelle comme çà";
}
}
?>
</div>
I am not really sure what was the difference in searcht.php and searche.php. Anyway, you can just use two buttons to separate the request. And, you can use if(isset($_POST['submitt'])) for the submitt submission and if(isset($_POST['submite'])) for the submite submission.
<div class="t">
<form method="post" enctype="multipart/form-data">
<label>Search</label>
<input type="submit" name="submitt" value="Submit T">
<input type="submit" name="submite" value="Submit E">
</form>
<?php
if(isset($_POST["submitt"])) {
$hostt = "localhost";
$db_namet = "photos";
$usernamet = "root";
$passwordt = "";
try{
$cont = new PDO("mysql:host={$hostt};dbname={$db_namet}", $usernamet, $passwordt);
}
catch(PDOException $exceptiont){
echo "Connection error: " . $exceptiont->getMessage();
}
$stht = $cont->prepare("SELECT image, image_nom, image_text FROM images WHERE image_nom = '$strt'");
$stht->setFetchMode(PDO:: FETCH_OBJ);
$stht->execute();
} else if(isset($_POST["submite"])) {
$hostt = "localhost";
$db_namet = "photos";
$usernamet = "root";
$passwordt = "";
try{
$cont = new PDO("mysql:host={$hostt};dbname={$db_namet}", $usernamet, $passwordt);
}
catch(PDOException $exceptiont){
echo "Connection error: " . $exceptiont->getMessage();
}
$stht = $cont->prepare("SELECT image, image_nom, image_text FROM images WHERE image_nom = '$strt'");
$stht->setFetchMode(PDO:: FETCH_OBJ);
$stht->execute();
}
if(isset($stht)){
if($row = $stht->fetch()){
?>
<br><br><br>
<table>
<tr>
<th>Image</th>
<th>Nom</th>
<th>Fiche</th>
</tr>
<tr>
<td class='image'><?php print "<img src='images/$row->image'>";?></td>
<td><?php print $row->image_nom; ?></td>
<td><?php print $row->image_text;?></td>
</tr>
</table>
<?php
}else{
echo "Aucune personne s'apelle comme çà";
}
}
?>
</div>
Please bear with me, I'm not familiar yet with the language. I have a table that lists an applicant record such as applicant number, name and status. I want to update an applicant status either 'hired' or 'failed' on a specific row using a PHP form. However, I'm not sure how to get the specific submit name on its row upon submission. Or if you have a workaround I would appreciate that. Thank you so much for your help.
<!DOCTYPE html>
<html>
<h2>Applicant Records</h2>
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password ="";
$mysql_database = "applicantrecord";
// Create connection
$conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqli = "SELECT id, firstname, lastname, status FROM applicant";
$result = $conn->query($sqli);
if ($result->num_rows > 0) { ?>
<table class="table">
<thead>
<tr>
<th>Applicant No.</th>
<th>Lastname</th>
<th>Firstname</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<?php
// output data of each row
echo "<tbody>";
while($row = $result->fetch_assoc())
{ ?>
<tr>
<td>
<?php echo $row["id"];
$appid = $row["id"];
?>
</td>
<td>
<?php echo $row["lastname"]; ?>
</td>
<td>
<?php echo $row["firstname"]; ?>
</td>
<td>
<?php echo $row["status"]; ?>
</td>
<td>
</td>
<td>
<div>
<form action="" role="form" method="post" name="form<?php echo $appid; ?>">
<select name="applicant_status">
<option value="Hired">Hire</option>
<option value="Failed">Fail</option>
</select>
</p>
<button type="submit" class="btn btn-default" name = "submit<?php echo $appid; ?>" data-dismiss="modal">Submit</button>
</form>
<?php
if(isset($_POST["submit"])){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$newappid = $appid;
$newapptstatus = $_POST['applicant_status'];
$connect = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $connect ) {
die('Could not connect: ' . mysql_error());
}
$sql_sub = "UPDATE applicant ". "SET status = '$newappstatus'".
"WHERE id = '$newappid'" ;
mysql_select_db('applicantrecord');
$retval = mysql_query( $sql_sub, $connect );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
echo "<script type= 'text/javascript'>alert('An error occured! Applicant status update failed!');</script>";
}
echo "<script type= 'text/javascript'>alert('Applicant status updated successfully!');</script>";
mysql_close($connect);
}
?>
</div>
</td>
</tr>
<?php }
echo "</tbody>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</html>
In your if statement where you check that $_POST['submit'] is set, the index 'submit' does not exist. Thus isset($_POST['submit']) evaluates to false and your query to update the table is never being executed.
The variable $appid is being changed with each row that is added, so when the page is done loading and the submit button is pushed on a certain row, $appid won't necessarily contain the correct row number.
To get around this, you could use a hidden input in your form:
<input name="id" value="<?php echo $appid ?>" type="hidden">
Then you can replace isset($_POST['submit']) with isset($_POST['id']) and set $newappid = $_POST['id'] to get the row number to be changed.
I want to create a select dropdownlist which retrieves data from a table "teamtable" and displays it on a page where the user enters his choice and the corresponding ID for the choice is submitted in other database "user" where the column is a foreign key.
Tables and their contents-
teamtable-
idTeam(INT)(PK) - 1,2,3
teamName(VARCHAR) - Team-1, Team-2, Team-3
user-
team(INT)(FK)
<html>
<head>
<script type="text/javascript">
function validateForm()
{
var f=document.forms["reg"]["team"].value;
if ((f==null || f==""))
{
alert("All Field must be filled out");
return false;
}
}
</script>
<form name="reg" action="user_exec.php" onsubmit="return validateForm()" method="post">
<table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td colspan="2">
<div align="center">
<?php
$remarks=$_GET['remarks'];
if ($remarks==null and $remarks=="")
{
echo 'Register a new user';
}
if ($remarks=='success')
{
echo 'Registration Success';
}
?>
</div></td>
</tr>
<tr>
<td><div align="right">Team:</div></td>
<td>
<?php
$mysqli_hostname = "localhost";
$mysqli_user = "root";
$mysqli_password = "my_pass";
$mysqli_database = "my_db";
$prefix = "";
$bd = mysqli_connect($mysqli_hostname, $mysqli_user, $mysqli_password) or die("Could not connect database");
mysqli_select_db($mysqli_database, $bd) or die("Could not select database");
$sql = "SELECT idTeam,teamName FROM teamtable ";
$result = mysqli_query($sql);
echo "<select name='team'>";
while ($row=mysqli_fetch_array($result))
{
echo "<option value='" . $row['idTeam'] ."'>" . $row['teamName'] ."</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><input name="submit" type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</head>
</html>
<?php
include('connection.php');
$sql = "SELECT idTeam, teamName FROM team";
$result = $conn->query($sql);
echo "<select name='team'>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['idTeam'] ."'>" . $row['teamName'] ."</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
I have three buttons on a page and I want to submit that form on the same page, depending to the button press, corresponding query will run. But there are some problem with the script.
<?php
$db = mysql_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db('easy_excel', $db) or die(mysql_error($db));
if ($_POST['submit'] =='delete_aabb')
{
$query ='DELETE FROM aabb';
mysql_query($query, $db) or die(mysql_error($db));
echo 'All Data from aabb Deleted succecfully!';
}
elseif ($_POST['submit'] =='delete_bbcc') {
$query ='DELETE FROM bbcc';
mysql_query($query, $db) or die(mysql_error($db));
echo 'All Data from bbcc Deleted succecfully!';
}
elseif ($_POST['submit'] =='show_bbcc') {
$query = 'SELECT
name
FROM bbcc';
$result = mysql_query($query, $db) or die(mysql_error($db));
echo '<table border="1">';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
?>
<html>
<head>
<title>Say My Name</title>
</head>
<body>
<form action="#" method="post">
<table>
<tr>
<input type="submit" name="submit" value="delete_aabb" />
</tr>
<tr>
<input type="submit" name="submit" value="delete_bbcc" /></td>
</tr>
<tr>
<input type="submit" name="submit" value="show_bbcc" /></td>
</tr>
</table>
</form>
</body>
</html>
This script is not running. Please help me.
I advice you to use MySQLi instead of deprecated MySQL. And I think you're referring to the isset() function.
<html>
<head>
<title>Say My Name</title>
</head>
<body>
<?php
/* ESTABLISH CONNECTION USING MYSQLi */
$con=mysqli_connect("localhost","root","","easy_excel");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
if (isset($_POST['delete_aabb'])) /* IF DELETE_AABB IS CLICKED */
{
mysqli_query($con,"DELETE FROM aabb");
echo 'All Data from aabb Deleted succecfully!';
}
else if (isset($_POST['delete_bbcc'])) { /* IF DELETE_BBCC IS CLICKED */
mysqli_query($con,"DELETE FROM bbcc");
echo 'All Data from bbcc Deleted succecfully!';
}
else if (isset($_POST['show_bbcc'])) { /* IF SHOW_BBCC IS CLICKED */
$result=mysqli_query($con,"SELECT name FROM bbcc");
echo '<table border="1">';
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
?>
<form action="" method="POST">
<table>
<tr>
<input type="submit" name="submit" value="delete_aabb" name="delete_aabb"/>
</tr>
<tr>
<input type="submit" name="submit" value="delete_bbcc" name="delete_bbcc"/></td>
</tr>
<tr>
<input type="submit" name="submit" value="show_bbcc" name="show_bbcc"/></td>
</tr>
</table>
</form>
</body>
</html>