I have to create a program which allows users to delete an update lecturers from a lecturers table. For some reason the table which my program displays is not accurate(e.g older values than current table). And i cannot delete or update. I have been stuck on this for quite a while and apologies if it is a simple solution i am only a beginner.
<?php
//includes
include("CONFIG/connection.php"); //include the database connection
include("LIBRARY/helperFunctionsTables.php"); //include the database connection
include("LIBRARY/helperFunctionsDatabase.php"); //include the database connection
require_once("CONFIG/config.php"); //include the application configuration settings
?>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;" charset="UTF-8">
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo __CSS; ?>
</head>
<body>
<header>
</header>
<?php
include("PAGE_CONTENT/navigation.php");
//----------------MAIN SECTION----------------------------//
//========================================================//
echo "<section class='wide'>";
$table = 'lecturer'; //table to delete records from
$PK = "LectID"; //Specify the PK of the table
if (isset($_POST['delrecord'])) //if the form has been submitted
{
//DELETE query
$selectedID = $_POST['delrecord']; //get the ID of the record we want to delete from the form
$sqlDel = "DELETE FROM $table WHERE $PK='$selectedID'"; //create the SQL
if (deleteRecord($conn, $sqlDel)) //call the deleteRecord() function
{
echo "<h3>RECORD WITH PK=$selectedID DELETED</h3>";
} else {
echo "<h3 >RECORD WITH PK=$selectedID CANNOT BE DELETED</h3>";
}
//Re-display table with delete options
//Query string
$sqlData = "SELECT * FROM $table"; //get the data from the table
$sqlTitles = "SHOW COLUMNS FROM $table"; //get the table column descriptions
//execute the 2 queries
$rsData = getTableData($conn, $sqlData);
$rsTitles = getTableData($conn, $sqlTitles);
//check the results
$arrayData = checkResultSet($rsData);
$arrayTitles = checkResultSet($rsTitles);
//use resultsets to generate HTML tables with DELETE button
generateDeleteTable($table, $PK, $arrayTitles, $arrayData);
//close the connection
$conn->close();
} else if (isset($_POST['editrecord'])) //if the form has been submitted
{
echo "am in inside";
//DELETE query
$selectedID = $_POST['editrecord']; //get the ID of the record we want to delete from the form
$sqlEDIT = "SELECT FROM $table WHERE $PK='$selectedID'"; //create the SQL
if (deleteRecord($conn, $sqlEDIT)) //call the deleteRecord() function
{
echo "<h3>RECORD WITH PK=$selectedID DELETED</h3>";
} else {
echo "<h3 >RECORD WITH PK=$selectedID CANNOT BE DELETED</h3>";
}
include 'FORMS/newUserForm.html';
$lectFirstName = $_POST[lectFirstName];
$lectLastName = $conn->real_escape_string($_POST['fName']);
$sqlEdit = "UPDATE $table SET FirstName=lectFirstName, LastName=lectLastName WHERE $PK='$selectedID'";
if (deleteRecord($conn, $sqlEDIT)) //call the deleteRecord() function
{
echo "<h3>RECORD WITH PK=$selectedID DELETED</h3>";
} else {
echo "<h3 >RECORD WITH PK=$selectedID CANNOT BE DELETED</h3>";
}
//Re-display table with delete options
//Query string
$sqlData = "SELECT * FROM $table"; //get the data from the table
$sqlTitles = "SHOW COLUMNS FROM $table"; //get the table column descriptions
//execute the 2 queries
$rsData = getTableData($conn, $sqlData);
$rsTitles = getTableData($conn, $sqlTitles);
//check the results
$arrayData = checkResultSet($rsData);
$arrayTitles = checkResultSet($rsTitles);
//use resultsets to generate HTML tables with DELETE button
generateDeleteTable($table, $PK, $arrayTitles, $arrayData);
//close the connection
$conn->close();
} else //this is the first time the form is loaded
{
//display table with delete options
//Query string
$sqlData = "SELECT * FROM $table"; //get the data from the table
$sqlTitles = "SHOW COLUMNS FROM $table"; //get the table column descriptions
//execute the 2 queries
$rsData = getTableData($conn, $sqlData);
$rsTitles = getTableData($conn, $sqlTitles);
//check the results
$arrayData = checkResultSet($rsData);
$arrayTitles = checkResultSet($rsTitles);
//use resultsets to generate HTML tables with DELETE button
generateDeleteTable($table, $PK, $arrayTitles, $arrayData);
//close the connection
$conn->close();
}
echo "</section>";
Related
I have two tables and I want to count how many 1 is in 1-2 table. If it is same amount than you can insert 1 if not than you can not insert 1 in to the table.
My code insert data even when it is equal or not:
<?php
$msg = '';
if(isset($_POST['rogzit_in'])) {
$barcode = $_POST['barcode'];
// Belépés ellenőrzése
if(!isset($_POST['barcode'])) {
$sql_barcodeInEll = "SELECT COUNT(barcode) FROM log_in WHERE barcode = '$barcode'";
$result_barcodeInEll = mysqli_query($dbCon, $sql_barcodeInEll);
$sql_barcodeOutEll = "SELECT COUNT(barcode) FROM log_out WHERE barcode = '$barcode'";
$result_barcodeOutEll = mysqli_query($dbCon, $sql_barcodeOutEll);
if(mysqli_num_rows($result_barcodeInEll) != $result_barcodeOutEll) {
$msg = '<h4 class="col-12 text-center mb-3 text-danger">Már bejelntkezett!</h4>';
}
}
if(isset($_POST['barcode'])) {
$sql_beszuras = "INSERT INTO log_in(barcode) VALUES ('$barcode')";
if(mysqli_query($dbCon, $sql_beszuras)) {
?>
<script type="text/javascript">
window.location = "home.php/";
</script>
<?php
}
}
}
?>
Thank you for the help!
I have two tables in one database. The first one is the g1 where the buttons' data is located. The second is the gradeone, where the enrollees' data is located. I want to display the data from the table "gradeone" by clicking the
specific buttons.
Assuming that I added 2 sections. Section 1 and section 2. I click the button section1. By clicking it, I want to display the data of the enrollee from table "gradeone" where the section is 1.
<?php
$dsn = 'mysql:host=localhost;dbname=admin';
$username = 'root';
$password = '';
try{
// Connect To MySQL Database
$con = new PDO($dsn,$username,$password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo 'Not Connected '.$ex->getMessage();
}
$sectionnumber ="";
$datasuccess ="";
$error ="";
function getPosts(){
$posts = array();
$posts[1] = $_POST['sectionnumber'];
return $posts;
}
if(isset($_POST['add'])){
$data = getPosts();
if(empty($data[1])){
$error = 'Enter The User Data To Insert';
}else {
$insertStmt = $con->prepare('INSERT INTO g1(sectionnumber) VALUES(:sectionnumber)');
$insertStmt->execute(array(
':sectionnumber'=> $data[1]
));
if($insertStmt){
$datasuccess = "<font color='#f8234a'>New added</font>";
}
}
}
?> //Code for adding a button
<?php
require 'connection.php';
$sql = "SELECT sectionnumber FROM g1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<button type='button' class='btn'>Section " . $row["sectionnumber"] . "</button></a><hr>";
}
} else { echo "<B>No Sections</B>"; }
$con->close();
?> //Code for displaying the button
<html>
<body>
<form action=">
<input type="number" name="sectionnumber">
<input type="submit" name="add">
</form>
</body>
</html>
Good day,
I have done extensive research on this issue but unfortunately none of the related issues solved my problem.
Here I have a very basic PHP mySQLi db connection. The connection succeeds and so does the query that is run on the table. The issue is that the the result set will not display. All of my references are correct and when I check to see if the result set is populated, it is. I believe the issue is with my while block but no errors are returned when this is run.
Thank you for your time
<?php
$db = mysqli_connect('localhost','root','','securitour') //connection to the database
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<?php
$query = "SELECT * FROM location"; //The SQL query
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query); //query the table an store the result set in a variable
$row = mysqli_fetch_array($result); //create an array and store the records of the result set in it
if (mysqli_num_rows($result) != 0) //to check if the result set contains data
{
echo "results found"; //THIS is what is returned.
}
else
{
echo "results not found";
}
while ($row = $result->fetch_assoc()) //itterate through the array and display the name column of each record
{
echo $row['name'];
}
mysqli_close($db);
?>
</body>
</html>
Lots of things not right here.
You are processing the mysqli_query() function twice - there is no need.
You are selecting all fields in your SQL query (SELECT *). You should select fields by name.
You're interchanging between procedure and class-based MySQLi - You should stick to one or the other.
Try this instead:
<?php
$db = mysqli_connect('localhost','root','','securitour') //connection to the database
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<?php
$query = "SELECT name FROM location"; //The SQL query
$result = mysqli_query($db, $query) or die('Error querying database'); //query the table an store the result set in a variable
if(mysqli_num_rows($result) > 0){
echo "Results found!";
while($row = mysqli_fetch_array($result)){ //create an array and store the records of the result set in it
echo $row['name'];
}
} else {
echo "results not found";
}
mysqli_close($db);
?>
</body>
</html>
You don't need to run mysqli_query() twice. and you need to use mysqli_fetch_assoc for associative array
<?php
$db = mysqli_connect('localhost','root','','securitour') or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<?php
$query = "SELECT * FROM location"; //The SQL query
$result = mysqli_query($db, $query) or die('Error querying database.'); //query the table an store the result set in a variable
$row = mysqli_fetch_assoc($result); //create an array and store the records of the result set in it
if (mysqli_num_rows($result) != 0) //to check if the result set contains data
{
echo "results found"; //THIS is what is returned.
} else {
echo "results not found";
}
foreach ( $row as $name=>$val) {
echo $name . ':' . $val . '<br>';
}
mysqli_close($db);
?>
</body>
</html>
<?php include('dbcon.php');
include('header.php');
//variable
$clientID='';
$billAmount='';
$arrear='';
$monthlyBill='';
$surcharge='';
//data add
$clientID=$_POST['clientID'];
$sqll = "SELECT `client`.`clientId` , (
`arrear` + `surcharge` + `monthlyBill`) AS 'billamount', (
FROM `billifno`
JOIN `client` ON `billifno`.`clientID` = `client`.`clientID` WHERE `billifno`.`clientID`='".$_POST["clientID"]."'";
$result = mysqli_query($con,$sqll);
// if ($result->num_rows > 0)
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$clientId = $row['clientId'];
$arrear = $row['arrear'];
$monthlyBill= $row['monthlyBill'];
$billAmount = $row['billAmount'];
$surcharge = $row['surcharge'];
}
//echo $billAmount;
$sql = "INSERT INTO `billIfno`
(`clientID`, `billAmount`,`arrear`, `monthlyBill`,`surcharge`) VALUES
('$clientID','$billAmount','$arrear','$monthlyBill','$surcharge')";
if ($con->query($sql)=== true)
{
echo "Recorded!!!";
}
else
{
echo "Not Recorded !!!";
}
$con->close();
?>
Hare I am taking some value and process it using query then again insert into billInfo table but it not inserting the new values like
If I echo billAmonu it print the correct value that is calculated by query bt that value is not inserting into database.
This a sample delete code I created to delete entries MySQL database. When I execute this code, Successful message displays but entries wont delete from the database. Records are kept track the primary key in the 'events' table 'id'
<?php
require 'dbconnect.php';
session_start();
$id=$_SESSION['id'];
$id = "0";
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( !empty($_POST)) {
// keep track post values
$id = $_POST['id'];
// delete data
$sql="DELETE * FROM events WHERE events.id='$id'"; //mysqli query
$result=mysqli_query($conn,$sql); //connection string and mysqli query variable
if($result==true)
{
echo "<script type='text/javascript'>alert('successfully DELETED!')</script>"; // javascript message for Successful delete
echo "<script>setTimeout(\"location.href = 'list_events.php';\",15);</script>"; // return page
}
else
{
echo "<script type='text/javascript'>alert('failed to DELETE RETRY!')</script>"; //failed to delete message
echo "<script>setTimeout(\"location.href = 'list_events.php';\",15);</script>"; //return page
}
header("Location: list_events.php"); // header page
}
?>
You can use this
$sql="DELETE FROM events WHERE events.id='{$id}'"; //mysqli query
Just to be safe.
Do check if you are getting some value in $id and it also exist in database.
Use DELETE FROM instead of DELETE * FROM and you can use the following commands for more security:
$sql="DELETE FROM events WHERE events.id=?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('d',$id);
/* execute prepared statement */
$result = $stmt->execute();