Problem in if and {endif} and {endwhile} in my first Project? - php

This problem in php I need solution in this day
Please for my homework
Can you help me in my homework please because I'm not working but I'm reading in my university Please can you solve this problem.
Can you explain to my this problem or can connection to my desktop to solution this problem or can send explain .
This code
index.php:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<?php require_once 'process.php'?>
<?php
if(isset ($_SESSION['message']));?>
<div class="alert alert-<?=$_SESSION['msg_type']?>">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
// and this problem !
<?php endif ?>
?>
<div class="contener">
<?php
$mysqli = new mysqli('localhost', 'root', 'rootroot','crudcrud') or die(mysqli_error($mysqli));
$result = $mysqli->query("SELECT * FORM data") or die($mysqli->error);
?>
<div class="row justify-content-center">
<table class="table">
<thead>
<tr>
<th>usernames</th>
<th>Passowrds</th>
<th colspan="2">Action</th>
<tr>
</thead>
</div>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row ['username'];?></td>
<td><?php echo $row ['password'];?></td>
<td>
<a href="index.php?edit=<?php echo $row ['id']; ?>"
class="btn btn-info">edit</a>
<a hraf="process.php?delete=<?php echo $row ['id']; ?>"
class="btn btn-danger">
</td>
</tr>
//this problem !
<?php endwhile; ?>
</table>
<?php
function pre_r( $arry ){
echo'<pre>';
print_r($array);
echo'</pre>';
}
?>
<div class="col-lg-6 m-auto">
<form action="process.php" method="post">
<input type="hidden" name="id" value=" <?php echo $id ?> "
<br><br><div class="card">
<div class="card-header bg-dark">
<h1 class="text-white text-center"> Insert Operation </h1>
</div><br>
<label> Username: </label>
<input type="text" name="username" class="form-control" value=" <?php echo $username; ?> " placeholder="Enter Your Username"> <br>
<label> Password: </label>
<input type="text" name="password" class="form-control" value=" <?php echo $password; ?> " placeholder="Password"> <br>
<?php
if ($update == true)
?>
<button class="btn btn-info" type="submit" name="update"> update </button>
<?php else: ?>
<button class="btn btn-success" type="submit" name="save"> Submit </button>
// and this problem !
<?php endif; ?>
</div>
</form>
</div>
</div>
</body>
</html>
code
process.php
=======================================================
<?php
session_start();
$mysqli = new mysqli('localhost', 'root', 'rootroot','crudcrud') or
die(mysqli_error($mysqli));
$id = 0;
$update = false;
$username = '';
$password = '';
if (isset ($_POST['save'])){
$usernames = $_POST['username'];
$passwords = $_POST['password'];
$mysqli->query("INSERT INTO data (username, password) VALUES('$username', '$password')") or
die($mysqli->error);
$_SESSION['message'] = "Record has been saved!";
$_SESSION['msg_type'] = "success!";
header('loocation: index.php');
}
if (isset ($_GET['delete'])){
$id = $_GET['delete'];
$mysqli->query("DELETE FROM data WHERE id=$id") or
die($mysqli->error());
SESSION['message'] = "Record has been deleted!";
SESSION['msg_type'] = "denger!";
header('loocation: index.php');
}
if (isset ($_GET['edit'])){
$id = $_GET['edit'];
$update = true;
$result = $mysqli->query("DELETE FROM data WHERE id=$id") or
die($mysqli->error());
if (count($result)==1){
$row = $result->fetch_array();
$username = $row['username'];
$password = $row['password'];
}
}
if (isset($_POST['update'])){
$id = $_POST['id'];
$username = $_POST['username'];
$password = $_POST['password'];
$mysqli->query("UPDATE data SET username='$username', password='$password' WHERE id=$id") or
die($mysqli->error);
$_SESSION['message'] = "Record has been Update!";
$_SESSION['msg_type'] = "warning!";
header('loocation: index.php');
}

This piece is completely wrong:
if(isset ($_SESSION['message']));?> //<-- : not ;
<div class="alert alert-<?=$_SESSION['msg_type']?>">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
//<-- no closing tag
<?php endif ?> //<-- missing ;
Should be:
if(isset ($_SESSION['message'])):?>
<div class="alert alert-<?=$_SESSION['msg_type']?>">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
endif; ?>
It get's pretty ugly when you use a lot of PHP and HTML in the same file, it might be time to look at using a template engine.

Related

Page to update mysql entry does not run sql query

I have three pages at the moment:
config.php: contains the configuration for mysql connection
list.php: lists entries in a mysql table
edit.php: edits a specific entry selected in list.php
config.php:
define('DB_SERVER', 'server');
define('DB_USERNAME', 'user');
define('DB_PASSWORD', 'pass');
define('DB_NAME', 'database');
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
list.php:
<?php
require_once "config.php";
session_start();
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<meta http-equiv="Content-type" content="text/html; charset=utf8">
<title>Bienvenue</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body{ font: 14px sans-serif; text-align: center; }
</style>
</head>
<body>
<table class="table table-striped">
<tr>
<th></th>
<th>column 1</th>
<th>column 2</th>
</tr>
<?php
$result = mysqli_query($link, "select * from TABLE");
while($row = mysqli_fetch_array($result)){
echo "<tr>
<td><a href='edit.php?id=" . $row['IDCOLUMN'] . "' class='btn btn-success ml-3'>Modifier</a></td>
<td>" . $row['column 1'] . "</td>
<td>" . $row['column 2'] . "</td>
</tr>"; //$row['index'] the index here is a field name
}
mysqli_close(); //Make sure to close out the database connection
?>
</table>
</body>
</html>
edit.php:
<?php
require_once "config.php";
session_start();
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
$id = $_GET['id'];
$resname_err = $rescity_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate new password
if(empty(trim($_POST["nom_residence"]))){
$resname_err = "Entrer le nom de la résidence.";
} elseif(empty(trim($_POST["ville_residence"]))){
$rescity_err = "Entrer la ville de la résidence.";
}else{
$resname1 = trim($_POST["nom_residence"]);
$rescity1 = trim($_POST["ville_residence"]);
}
// Check input errors before updating the database
if(empty($resname_err) && empty($rescity_err)){
// Prepare an update statement
$sql = "UPDATE residences SET RESIDENCE = ?, VILLE = ? WHERE residenceID = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssi", $resname1, $rescity1, $id);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
header("location: list.php?=".$resname1.$rescity1);
exit();
} else{
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
mysqli_stmt_close($stmt);
}
}
}
?>
<!DOCTYPE html>
<html lang>
<head>
<meta charset="utf8">
<meta http-equiv="Content-type" content="text/html; charset=utf8">
<title>Bienvenue</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<?php
$result = mysqli_query($link, "select * from residences where residenceID=".$id);
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results
$resname = $row['RESIDENCE'];
$rescity = $row['VILLE'];
}
mysqli_close($link)
?>
<div class="wrapper">
<h2>modify entry</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group">
<label>value name1</label>
<input name="nom_residence" class="form-control <?php echo
(!empty($resname_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $resname; ?>">
<span class="invalid-feedback"><?php echo $resname_err; ?></span>
</div>
<div class="form-group">
<label>value name2</label>
<input name="ville_residence" class="form-control <?php echo
(!empty($rescity_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $rescity; ?>">
<span class="invalid-feedback"><?php echo $rescity_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Enregistrer">
<a class="btn btn-link ml-2" href="list.php">Annuler</a>
</div>
</form>
</div>
</body>
</html>
My current problem is that the Mysql entry is never updated.
This section runs:
header("location: list.php?=".$resname1.$rescity1);
So I know every condition is met correctly
Note: I added this part to know their values when redirected to list.php:
.$resname1.$rescity1
I ran all the code inside a PHP shell and it worked perfectly.
Of course, I had to set the values resname1 and rescity1 manually, so I'm thinking it must be some formatting issue?

Unable to use $_SESSION in an other file (PHP)

Hello good afternoon, I'm having the trouble of not being able to use add a $_POST value to my $_SESSION variable so that I can use it later in my profile file.
I can give it other values like "hello" outside the if and it echoes hello in levraiprofilewesh.php.
I've tried making $_SESSION a super global variable and global but it didn't work.
I've been stuck on it for a few hours :(.
I don't need to upload the website on the web, it's just for local use, so there's no need for privacy feedback although it has helped me for the future.
login.php code:
<?php
session_start();
include("db_connect.php");
if(isset($_POST['login_button'])) {
$user_email = trim($_POST['user_email']);
$user_password = trim($_POST['password']);
$_SESSION['password'] = $user_password;
$usql = "SELECT * FROM users WHERE Email='$user_email' && Password='$user_password'";
$uresult = mysqli_query($db, $usql) or die("database error:". mysqli_error($db));
$urow = mysqli_fetch_assoc($uresult);
//while($row = mysqli_fetch_array($uresult))
//$STPMARCHEFRERE = $row['Firstname'];
//$SESSION = $row['Firstname'];
if($urow['Password']==$user_password){
setcookie("userid",$user_password,time()+(60*60*24*7));
setcookie("useremail",$user_email,time()+(60*60*24*7));
//$GLOBALS['SESSION'] = $user_password;
$time=time();
$queryz = "UPDATE Users
Set Online='Online',
Time='$time'
WHERE Password='$user_password' ";
$db->query($queryz) or die('Errorr, query failed to upload');
echo "ok";
} else {
echo "email or password does not exist."; // wrong details
}
}
?>
levraiprofilewesh.php code:
<?php
session_start();
include("login.php");
$connection = mysqli_connect('localhost', 'root','','phpchart');
if(isset($_SESSION['password'])) {
$username = $_SESSION['password'];
$query = "SELECT *
FROM users
WHERE Password = '%$username%'";
$select_user_profile_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($select_user_profile_query)) {
$post_tamere = $row['Firstname'];
$post_tondaron = $row['Sirname'];
$post_tasoeur = $row['Phone'];
$post_tonneveu = $row['Institution'];
$post_julia = $row['Email'];
}
}
?>
<?php
if(isset($_POST['edit_user'])) {
$user_firstname = $_POST['user_firstname'];
$user_lastname = $_POST['user_lastname'];
$user_role = $_POST['user_role'];
//$post_image = $_FILES['image']['name'];
//$post_image_temp = $_FILES['image']['tmp_name'];
$username = $_POST['username'];
$user_email = $_POST['user_email'];
$user_password = $_POST['user_password'];
//$post_date = date('d-m-y');
//move_uploaded_file($post_image_temp, "./images/$post_image" );
$query = "SELECT randSalt FROM users";
$select_randsalt_query = mysqli_query($connection, $query);
if(!$select_randsalt_query) {
die("Query Failed" . mysqli_error($connection));
}
$row = mysqli_fetch_array($select_randsalt_query);
$salt = $row['randSalt'];
$hashed_password = crypt($user_password, $salt);
$query = "UPDATE users SET ";
$query .="user_firstname = '{$user_firstname}', ";
$query .="user_lastname = '{$user_lastname}', ";
$query .="user_role = '{$user_role}', ";
$query .="username = '{$username}', ";
$query .="user_email = '{$user_email}', ";
$query .="user_password = '{$hashed_password}' ";
$query .= "WHERE username = '{$username}' ";
$edit_user_query = mysqli_query($connection,$query);
confirmQuery($edit_user_query);
}
?>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<div id="wrapper">
<!-- Navigation -->
<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Welcome to Profile
<small>Author</small>
</h1>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Firstname</label>
<input type="text" value="<?php echo $post_tamere; ?>" class="form-control" name="user_firstname">
</div>
<div class="form-group">
<label for="post_status">Lastname</label>
<input type="text" value="<?php echo $post_tondaron; ?>" class="form-control" name="user_lastname">
</div>
<div class="form-group">
<select name="user_role" id="">
<option value="subscriber"><?php echo $user_role; ?></option>
<?php
if($user_role == 'admin') {
echo "<option value='subscriber'>subscriber</option>";
} else {
echo "<option value='admin'>admin</option>";
}
?>
</select>
</div>
<!--<div class="form-group">
<label for="post_image">Post Image</label>
<input type="file" name="image">
</div>-->
<div class="form-group">
<label for="post_tags">Phone</label>
<input type="text" value="<?php echo $post_tasoeur; ?>" class="form-control" name="username">
</div>
<div class="form-group">
<label for="post_content">Email</label>
<input type="email" value="<?php echo $post_tonneveu; ?>" class="form-control" name="user_email">
</div>
<div class="form-group">
<label for="post_content">Password</label>
<input type="password" value="<?php echo $post_julia; ?>" class="form-control" name="user_password">
</div>
<h1><?php echo $_SESSION['superhero']; ?></h1>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="edit_user" value="Update Profile">
</div>
</form>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>

CRUD OPERATION USING HTML and MYSQL PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Notice: Undefined variable: db in C:\xampp\htdocs\xampp\index1.php on line 32
Warning: mysqli_query() expects parameter 1 to be mysqli, null given
in C:\xampp\htdocs\xampp\index1.php on line 32
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
null given in C:\xampp\htdocs\xampp\index1.php on line 43 Notice:
Undefined variable: update in C:\xampp\htdocs\xampp\index1.php on line
69
<!DOCTYPE html>
<html>
<head>
<title>CRUD: CReate, Update, Delete PHP MySQL</title>
</head>
<body>
<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif?>
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM info WHERE id=$id");
if (count($record) == 1) {
$n = mysqli_fetch_array($record);
$name = $n['name'];
$address = $n['address'];
}
}
?>
<?php
$results = mysqli_query($db, "SELECT * FROM info");?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) {?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td>
<a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>
</td>
<td>
Delete
</td>
</tr>
<?php }?>
</table>
<form>
<form method="post" action="php_code.php" >
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="input-group">
<label>Name</label>
<input type="text" name="name" value="">
</div>
<div class="input-group">
<label>Address</label>
<input type="text" name="address" value="">
</div>
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Save</button>
<?php endif?>
</div>
</form>
</body>
</html>
2ND FILE
<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'crud');
$name = "";
$address = "";
$id = 0;
$update = false;
if (isset($_POST['save'])) {
$name = $_POST['name'];
$address = $_POST['address'];
mysqli_query($db, "INSERT INTO info (name, address) VALUES ('$name', '$address')");
$_SESSION['message'] = "Address saved";
header('location: index.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$address = $_POST['address'];
mysqli_query($db, "UPDATE info SET name='$name', address='$address' WHERE id=$id");
$_SESSION['message'] = "Address updated!";
header('location: index.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM info WHERE id=$id");
$_SESSION['message'] = "Address deleted!";
header('location: index.php');
}
And your code is fully prone to SQL injection
Read about the PHP Prepared statements.
https://www.w3schools.com/php/php_mysql_prepared_statements.asp
Suppose file 1 name is codefile.php and file 2 containing connection is connection.php So, write in codefile.php include("connection.php")
Change $db to $conn in codefile.php as in connection.php, you have created connection in $conn variable
Check below link:
w3schools.com/php/php_includes.asp
codefile.php
<?php
include("connection.php");
$results = mysqli_query($conn, "SELECT * FROM info");
?>
<!DOCTYPE html>
<html>
<head>
<title>CRUD: CReate, Update, Delete PHP MySQL</title>
</head>
<body>
..................
..................
..................
connection.php
<?php
session_start();
$db_host = "localhost";
$db_location = " ";
$db_name = "crud";
$conn= mysqli_connect ($db_host,$db_name, $db_location,$db_name)or die ("could not connect to mysql");
..................
..................
..................

can't update postgres database using PHP

I am trying to creat a crud application using PHP, I have succeeded to display my data table from postgres but when i try i got some errors. this is my code :
<?php require 'database.php'; $id = null; if ( !empty($_GET['Code_Espece'])) { $id = $_REQUEST['Code_Espece']; } if ( null==$id ) { header("Location: index.php"); } if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST)) {
$firstnameError = null;
if (isset($_POST['Nom_Scien'])) {
$firstname = $_POST['Nom_Scien'];
}
$valid = true;if (empty($firstname)) { $firstnameError = 'Please enter firstname'; $valid = false; }
if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'UPDATE espece SET Nom_Scien = ? WHERE Code_Espece = ?';
$q = $pdo->prepare($sql);
$q->execute(array($firstname));
Database::disconnect();
header("Location: index.php");
}
}else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM espece where "Code_Espece" = ?';
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$firstname = $data['Nom_Scien'];
Database::disconnect();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Crud-Update</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
</div>
<form method="post" action="update.php?Code_Espece=<?php echo $id ;?>">
</div>
<div class="control-group<?php echo!empty($firstnameError) ? 'error' : ''; ?>">
<label class="control-label">Nom Scientifique</label>
<br />
<div class="controls">
<input type="text" name="nomscientifique" value="<?php echo!empty($firstname) ? $firstname : ''; ?>">
<?php if (!empty($firstnameError)): ?>
<span class="help-inline"><?php echo $firstnameError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-success" name="update" value="update">
<a class="btn" href="index.php">Retour</a>
</div>
</form>
</div>
</body>
</html>
The problem is when i put what i want to change and click in the button 'Update' it return the following message : 'Please enter firstname' inspite of teh input isnot empty.
Thank you ;
Change <input type="text" name="nomscientifique" value="<?php echo!empty($firstname) ? $firstname : ''; ?>"> to <input type="text" name="Nom_Scien" value="<?php echo!empty($firstname) ? $firstname : ''; ?>">

Data can not be inserted to the table

I have a db.php file which connection is established here to the database
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "aigsonlinedb";
$con = new mysqli($host,$user,$pass,$db_name);
function formatDate($date){
return date('g:i a', strtotime($date));
}
?>
"Index.php" file here is a form where the data should be sent and retrieved from the database
<?php
include 'db.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Chat System in PHP</title>
<link rel="stylesheet" href="style.css" media="all"/>
<script>
function ajax(){
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
document.getElementById('chat').innerHTML = req.responseText;
}
}
req.open('GET','chat.php',true);
req.send();
}
setInterval(function(){ajax()},1000);
</script>
</head>
<body onload="ajax();">
<div id="container">
<div id="chat_box">
<div id="chat"></div>
</div>
<form method="POST" action="index.php">
<input type="text" name="name" placeholder="enter name"/>
<textarea name="msg" placeholder="enter message"></textarea>
<input type="submit" name="submit" value="Send it"/>
</form>
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$msg = $_POST['msg'];
$query = "INSERT INTO chat (name,msg) VALUES ($name','$msg')";
$run = $con->query($query);
if($run){
echo "<embed loop='false' src='chat.wav' hidden='true' autoplay='true'/>";
}
}
?>
</div>
</body>
</html>
chat.php where the data is fetched from the database
<?php
include 'db.php';
$query = "SELECT * FROM chat ORDER BY id DESC";
$run = $con->query($query);
while($row = $run->fetch_array()) :
?>
<div id="chat_data">
<span style="color:green;"><?php echo $row['name']; ?></span> :
<span style="color:brown;"><?php echo $row['msg']; ?></span>
<span style="float:right;"><?php echo formatDate($row['date']); ?></span>
</div>
<?php endwhile;?>
The only problem is the data can not be sent to the database.
In the end of String ; vor SQL Statements. And PHP variables in double quotes and extra double quotes in values for define the datatype of sql

Categories