Let me explain what I want to.
I want to add a value in a list with db add without page change when I input in form, and click submit.
but in this code, I must refresh one more time to add, and also added twice a time.
How can I do that?
<?php
$conn = mysqli_connect('127.0.0.1','MYID','MYPASS','MYDB');
$sql = "SELECT * FROM MYTABLE";
$rs = mysqli_query($conn, $sql);
$list = '';
while($row = mysqli_fetch_array($rs)) {
$list = $list."<li>{$row['title']}</li>";
}
$article = array(
'title' => 'Welcome!',
'description' => 'Hello, Web!'
);
if (isset($_GET['id'])){
$filtered_id = mysqli_real_escape_string($conn, $_GET['id']);
$sql = "SELECT * FROM topic WHERE id={$filtered_id}";
$rs = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($rs);
$article['title'] = $row['title'];
$article['description'] = $row['description'];
}
if ($_POST['title'] != null){
$sql_in = "INSERT INTO topic (title, description, created) VALUES ('{$_POST['title']}', '{$_POST['description']}', NOW())";
$rs_in = mysqli_query($conn, $sql_in);
if ($rs_in === false) {
$msg = mysqli_error($conn);
} else {
$msg = 'Success.';
}
} else {
$msg = 'Fill in';
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WEB</title>
</head>
<body>
<h1>WEB</h1>
<ol>
<?=$list?>
</ol>
<details>
<summary>Create</summary>
<form action="./index.php" method="POST">
<p><input type="txt" name="title" placeholder="title"></p>
<p><textarea name="description" placeholder="description"></textarea></p>
<p><input type="submit"></p>
<p><?=$msg?></p>
</form>
</details>
</body>
</html>
<h2><?=$article['title']?></h2>
<?=$article['description']?>
</body>
</html>
In php realtime isn't a thing but there is a workaround you can use events services like pusher https://pusher.com/docs and receive the events in client side instantly .
there other website offers this so do your search before choosing them also you can always build your event server in node.js , c# , go
Related
This is my code:-
<html>
<body>
<center>
<h2>working time</h2>
<form action="" method="post">
<button name="starttime" class="starttime">starttime</button>
<?php
session_start();
$name = $_SESSION["name"];
if(isset($_POST["starttime"])){
$date = date('Y-m-d');
$date1 = date('H:i:s');
require "database.php";
$n = "";
$d = "";
$sql = "SELECT name, ddate FROM karthick.date WHERE name = '$name' AND ddate = '$date'";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$n = $row["name"];
$d = $row["ddate"];
}
}
if($n == $name && $d == $date){
echo'<script>
alert("Time already registered");
</script>';
}else{
$sql = "INSERT INTO karthick.date (name, ddate, starttime) VALUES ('$name', '$date', '$date1')";
if($conn->query($sql) === TRUE){
echo "starttime inserted";
}else{
echo "error";
}
}
}
?>
</form>
<form action = "" method="post">
<button name="endtime" class="endtime">Endtime</button>
<?php
session_start();
$name = $_SESSION["name"];
if(isset($_POST["endtime"])){
$date = date('Y-m-d');
$date1 = date('H:i:s');
require "database.php";
$n = "";
$d = "";
$sql = "SELECT name, ddate, endtime FROM karthick.date WHERE name = '$name' AND ddate = '$date'";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$e = $row["endtime"];
}
}
if($e != '00:00:00'){
echo'<script>
alert("Time already registered");
</script>';
}else{
$sql = "UPDATE karthick.date SET endtime='$date1' WHERE name = '$name' AND ddate = '$date'";
if($conn->query($sql) === TRUE){
echo "Endtime inserted.<br>";
}else{
echo "error";
}
$sql1 = "UPDATE karthick.date SET worked = (endtime - starttime) WHERE name = '$name' AND ddate = '$date'";
if($conn->query($sql1) == TRUE){
echo "worked time updated";
}else{
echo "error";
}
}
}
?>
</form>
<form action="user.php" method="post">
<button name= "logout" class="logout">Logout</button>
</form>
</body>
</html>
As I run this code it shows me an interrupting message.
Notice:A session had already been started - ignoring session_start()
But the code works fine. Is there any way to hide the notice message?
Only one time session_start() is needed on a page (on top). You have it two times in your page so remove the second one.
Suggestion:-
Always use some error reporting code.
example:-
error_reporting(E_ALL); // check all type of errors
ini_set('display_errors',1);// display those errors
Always add this to your php script in the beginning. It will help you a lot. Thanks.
remove session_start() from your file.
Because u started session twice.
Either the file u r included in your code in that u started session.
so please go through u r code once again
use session_start(); only once and before sending any output (at the very start of page)
or turn of error reporting(not recommended):
add error_reporting(0); at the beggining of page to hide all errors
and notices
add # character before command that causing error or notice( eg.
#session_start();)
change
<html>
<body>
<center>
<h2>working time</h2>
<form action="" method="post">
<button name="starttime" class="starttime">starttime</button>
<?php
session_start();
to
<?php
session_start();
?>
<html>
<body>
<center>
<h2>working time</h2>
<form action="" method="post">
<button name="starttime" class="starttime">starttime</button>
<?php
I'm trying to add a button that generates dummy data into a table with 1 click
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
include("classes/db.class.php");
$currentTime = time();
$totalUsers = '20000';
$usersUS1 = '5000';
$usersUS2 = '5263';
$usersFR1 = '8000';
$usersHK1 = '7425';
$usersUK1 = '0';
if(!empty($_POST))
{
$currentTime = time();
$db = new db();
$sql = "INSERT into connections (users, us1, us2, fr1, hk1, time, uk1) VALUES
('".$db->conn->real_escape_string($totalUsers) ."' ,
'". $db->conn->real_escape_string($usersUS1) ."' ,
'". $db->conn->real_escape_string($usersUS2) ."' ,
'". $db->conn->real_escape_string($usersFR1) ."' ,
'". $db->conn->real_escape_string($usersHK1) ."',
'". $db->conn->real_escape_string($currentTime) ."',
'". $db->conn->real_escape_string($usersUK1) ."')";
$result = $db->conn->query($sql);
return $result;
}
?>
However everytime I press the button, I receive 0 errors and nothing gets stored in the DB.
The php code and html are on the same page.
Does anyone know what's causing the problem?
EDIT:
DB class:
<?php
class db
{
private $m_sHost = "localhost";
private $m_sUserlogin = "root";
private $m_sPassword = "root";
private $m_sDatabase = "radius";
public $conn;
public function __construct()
{
$this->conn = new mysqli($this->m_sHost, $this->m_sUserlogin, $this->m_sPassword, $this->m_sDatabase);
}
}
?>
and the form I use to post:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>screen wall</title>
<link rel="stylesheet" type="text/css" href="styles/style.css">
<!--<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>-->
<script src="scripts/d3-3.5.2/d3.js"></script>
</head>
<body>
<p> Current Timestamp:<?php echo time(); ?> </p>
<form method="POST">
<input type="text" required="required" name="titel" value="test field :)">
<button type="submit" id="generateBtn">Add Data</button>
</form>
</body>
Again, HTML and PHP are on the same page.
First thing first: You should not be using the real_escape_string since the input is not from the user whereas it is hard coded in the code itself.
Secondly an alternative way to rectify the issue is you can use
if (!mysqli_query($db->conn,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
instead of
$result = $db->conn->query($sql);
Hope this helps.
Thank you
I"m attempting to display some data I've sent from ajax to a php file, however for some reason its not displaying it on the page. The way it works it I enter a search term into a input field, and a ajax script post the value to a php script, which return the database value requested back.
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (isset($_POST['name']) === true && empty($_POST['name']) === false) {
//require '../db/connect.php';
$con = mysqli_connect("localhost","root","root","retail_management_db");
$name = mysqli_real_escape_string($con,trim($_POST['name']));
$query = "SELECT `names`.`location` FROM `names` WHERE`names`.`name` = {$name}";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$loc = $row['location'];
echo $loc;
}//close While loop
} else {
echo $name . "Name not Found";
}
}
html form:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Retail Management Application</title>
</head>
<body>
Name: <input type="text" id="name">
<input type="submit" id="name-submit" value="Grab">
<div id="name-data"></div>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="js/global.js"></script>
</body>
</html>
You're appending a MySQL error result to your query, and you're trying to query a query result, try the following:
$query = "SELECT `names`.`location` FROM `names` WHERE`names`.`name` = '$name'";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
Edit:
{$name} that is a string and should be quoted instead.
change it to '$name' in the where clause.
Using:
$result = mysqli_query($con, $query) or die(mysqli_error($con));
will provide you with the reason as to why your query failed.
<?php
include_once("php_includes/check_login_status.php");
if($user_ok != true){
header("location: login.php");
exit();
}
?>
<?php
$id = "SELECT id FROM users WHERE username='$log_username'";
if(isset($_POST["signupbtn"])) {
if ($log_username) {
/// getting data from submitted form into local variables
$x = preg_replace('#[^a-z 0-9]#i', '', $_POST['xbox']);
$p = preg_replace('#[^a-z 0-9]#i', '', $_POST['psn']);
$s = preg_replace('#[^a-z 0-9]#i', '', $_POST['steam']);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR GAMER PROFILES
$sqli = "SELECT username FROM player WHERE xbox='$x' LIMIT 1";
$query = mysqli_query($db_conx, $sqli);
$x_check = mysqli_num_rows($query);
// -------------------------------------------
if ($x_check > 0){
echo "Xbox Gamer-Tag already linked to a user on this website";
exit();
} else if (is_numeric($x[0])) {
echo 'Xbox Gamer-Tag cannot begin with a number';
exit();
}
$sqli = "SELECT username FROM player WHERE psn='$p' LIMIT 1";
$query = mysqli_query($db_conx, $sqli);
$p_check = mysqli_num_rows($query);
// -------------------------------------------
if ($p_check > 0){
echo "PSN User already linked to a user on this website";
exit();
} else if (is_numeric($p[0])) {
echo 'PSN User cannot begin with a number';
exit();
}
$sqli = "SELECT username FROM player WHERE steam='$s' LIMIT 1";
$query = mysqli_query($db_conx, $sqli);
$s_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if ($s_check > 0){
echo "Steam account already linked to a user on this website";
exit();
} else if (is_numeric($s[0])) {
echo 'Steam account cannot begin with a number';
exit();
} else { $sqli = "INSERT INTO player (id, username, xbox, psn, steam, ip, created, lastupdated, notecheck)
VALUES ('$id','$log_username','$x','$p','$s','$ip',NOW(),NOW(),NOW())";
$query = mysqli_query($db_conx, $sqli);
}
echo "Gamer Profiles Updated";
exit();
if (!file_exists("p_player/$log_username")) {
mkdir("p_player/$log_username", 0755);
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="templates/default/style/style.css">
<style type="text/css">
#profiles{
margin-top:24px;
}
#profiles > div {
margin-top: 12px;
}
#profiles > input,select {
width: 200px;
padding: 3px;
background: #F3F9DD;
}
#profiles {
font-size:18px;
padding: 12px;
}
</style>
<script src="js/main.js"></script>
<script src="js/ajax.js"></script>
</head>
<body>
<?php include_once("templates/default/template_pageTop.php"); ?>
<div id="pageMiddle">
<h3>Gamer Profiles</h3>
<form action="player.php" method="POST" name="profiles">
<div>Xbox Gamer-tag: <input type="text" name="xbox"></div>
<div>PSN User: <input type="text" name="psn"></div>
<div>Steam User: <input type="text" name="steam"></div>
<input type="submit" name="signupbtn">
</form>
</div>
<?php include_once("templates/default/template_pageBottom.php"); ?>
</body>
</html>
The $log_username variable comes from the top php script and every thing is fine with it as I can echo the variable and comes back with the logged user on this page.
so basicaly nothing is being written to my database on submitting the button and I am really stumped with it tbh. I am using the MySQLi extension within php 5 as I have been following a tutorial on things and have came to the end of the tutorial and now I am by myself. I was going to included a shot of my database below to prove that the names on the database is fine but with the fact that my rep is 9 I can't add one to make your lives easier as you need to b 10.
have I got a } in the wrong place or labeling something MySQL when it should be MySQLi.
When I submit the form the mkdir is made in the folder and I get sent to the page with the echo "Gamer Profiles Updated"
Thank you to anyone who tries to solve this problem out I have.
I have been trying to do this for hours tbh and from what i have done everything checks fine and it should be working in theory but it just doesn't work. Thank you once again for looking at this and I look forward to your responses.
Change:
$id = "SELECT id FROM users WHERE username='$log_username'";
to:
$result = mysqli_query($db_conx, "SELECT id FROM users WHERE username='$log_username'") or die(mysqli_error($db_conx));
$row = mysqli_fetch_assoc($result) or die ("$log_username not found");
$id = $row['id'];
I'm using some crazy mixture of PHP/JavaScript/HTML/MySQL
$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row != NULL) {
// Display a confirm box saying "Not everyone has entered a bid, continue?"
}
// If confirmed yes run more queries
// Else nothing
What is the best way to have this confirm box display, before completing the rest of the queries?
if($row != NULL) {
?>
<script>alert("not everyone has submitted their bid.");</script>
<?php
}
or
<?php
function jsalert($alert_message){
echo "<script type='text/javascript'>alert('".$alert_message."');</script>";
}
if($row!=null){
jsalert("Not everyone has submitted their bid.");
}
?>
You can't do this in 1 continuous block, as all of the PHP will execute before the confirm (due to server vs. client).
You will need to break these into 2 separate steps and have the client mediate between them:
part1.php:
<?php
$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if ($row != NULL) { ?>
<form id="confirmed" action="part2.php" method="post">
<noscript>
<label>Not everyone has entered a bid, continue?</label>
<input type="submit" value="Yes">
</noscript>
</form>
<script type="text/javascript">
if (confirm("Not everyone has entered a bid, continue?")) {
document.getElementById('confirmed').submit();
}
</script>
<?
} else {
include_once('part2.php');
}
?>
part2.php:
<?php
// assume confirmed. execute other queries.
?>
$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row != NULL) {
// more queries here
} else {
echo "<script>alert('Empty result');</script>";
}
Play with this code and you will get it to work eventually. I know you are not looking for just alertbox , instead you are looking for something like "yes or no" informational box. So check this out.
<?php
?>
<html>
<head>
<script type="text/javascript">
function displayBOX(){
var name=confirm("Not everyone has entered a bid, continue?")
if (name==true){
//document.write("Do your process here..")
window.location="processContinuing.php";
}else{
//document.write("Stop all process...")
window.location="stoppingProcesses.php";
}
}
</script>
</head>
<?php
$query = "SELECT * faculty SET submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row != NULL) {
echo "<script>displayBox();</script>";
}
?>