Execute SQLITE query not working in PHP - php

I am trying to edit a row in my SQLite table with a button which will execute a SQLite query. But it doesn't change anything
Any help is appreciated.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>FORM</title>
<meta http-equiv="content-type" content="text/html; charset="utf-8">
<Link rel="stylesheet" href="ac.css" type="text/css" media="print"/>
<link rel="stylesheet" type="text/css" href="css/ma.css" />
</head>
<body>
<?php
date_default_timezone_set("America/New_York");
$handle = new SQLite3("./d/idb.db");
if(isset($_GET["index"])) {
$sel = $_GET["index"]+1;
} else {
Header('Location: la.php');
exit;
}
if (isset($_POST['submit'])) {
$handle->query("UPDATE tri SET c = 'NM' WHERE mj = 4");
}
?>
<form>
<input type="submit" value="submit" name="submit" id="button">
</table>
</form>
</body>
</html>

Related

Empty page on sql - php querying

I'm making a website where people can see their personal stats. I'm doing this by retrieving SQL data with a php system. Here are my 2 files :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>
</body>
</html>
And this is the file that is loaded when a user clicks on search :
<?php
mysql_connect("localhost", "root", "Medionakoya0102") or die("Error
connecting to database: ".mysql_error());
mysql_select_db("isolate") or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
$query = $_GET['query'];
$min_length = 3;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM Player
WHERE (`name` LIKE '%".$query."%') OR (`uuid` LIKE '%".$query."%')")
or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<p><h3>".$results['join_date']."</h3>".$results['gems']."
</p>";
}
}
else{
echo "No results";
}
}
else{
echo "Minimum length is ".$min_length;
}
?>
</body>
</html>
Now the problem is, the index loads, but when I click on search, sometimes the page is just blank, or it gives me an error : HTTP ERROR 500
Hope someone can help
A 500 HTTP error is a response from the webserver, so your form is submitting correctly but there is most likely an error from PHP. Check your web server logs for an error message correlating to the form submissions. If you're not sure what the error is telling you please edit your question to include the error and I'm sure we can get it solved.
Also, some information about your environment would be useful: which webserver are you using, what's your PHP version, etc.

Dynamic form php is not passing values

I have a MySQL table called room which contains an id and room name. I dynamically generate all those buttons. The idea is to get a list of all the rooms, tab-2-room.php shows all the rooms (that much works). The user clicks on a room, the next page shows you the list of all the items that can be found in that room. At this point I just need the variable to pass and that is not working. I have done this 1000 thousand times and this time the form does not want to pass any values.
If you require any additional info please ask so I can provide with it.Can someone spot the mistake here?
The controller.php file contains the new_room function which is below.
The following file is called tab-2-room.php
<?php
session_start ();
include_once 'controller.php';
if(isset($_POST['room'])) {
header('location: tab-2-items.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Tab 2 Room - Selection</title>
</head>
<body>
<div class="container">
<form enctype="multipart/form-data" method="post" action="">
<h1>ROOMS</h1>
<?php room_new(); ?>
<h2></h2>
</form>
</div>
</body>
</html>
The following file is called tab-2-item.php
<?php
session_start ();
include_once 'controller.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Tab 2 items - Data Entry</title>
</head>
<body>
<div class="container">
<form enctype="multipart/form-data" method="post" action="">
<h1>ROOM ITEMS </h1>
<?php
global $query;
var_dump($_POST);
$roomId = $_POST['room-id'];
echo 'this is room id:'.$roomId.'</br>';
//query to get all items that belong to the room-id
?>
</form>
</div>
</body>
</html>
PHP
function room_new(){
global $query;
//Dinamically displays values of size_standard table
$moveType = $_SESSION['move-type'];
$queryRoom = "SELECT * FROM room WHERE room_type = $moveType OR room_type = 3";
$resultRoom = $query->query($queryRoom) or die ('Could not execute Room query'. mysqli_error($query));
for ($i = 1; $i <= $resultRoom->num_rows; $i++) {
$rowRoom = $resultRoom->fetch_assoc();
extract($rowRoom);
echo 'id: '.$id;
echo ' counter: '.$i;
echo "<input type='submit' class='' name='room' value='$room_name'><br />";
echo "<input type='hidden' name='room-id' value='$id'>";
}
}
The global var query is set like this:
$GLOBALS['query'] = new mysqli($host, $username, $password, $db_name) or die("cannot connect");
global $query;
if ($query->connect_errno) {
printf("Connect failed: %s\n", $query->connect_error);
exit();
}

Unable to echo the data from ssh execution to webpage in PHP

I am writing a PHP script where I take input from user like server IP and one more input.
After this is available, I make ssh call to the server and execute some command. I want to show the output on the web page but i gets error as 500 (server error).
Here is my main script where i get input from user:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Centos Health Checker</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="login-form">
<form method="post" action="redirectPage.php">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="IP" placeholder="System IP" required /></td>
</tr>
<tr>
<td><input type="text" name="Component" placeholder="Component" required /></td>
</tr>
<tr>
<td><button type="submit" name="submit">Check System</button></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
Here is my landing page:
<?php
function Connect()
{
include('/home/as5714/php_data/Net/SSH2.php');
$IP1 = $_POST['IP'];
$Component1 = $_POST['Component'];
$ssh = new Net_SSH2($IP1);
if (!$ssh->login('centos', 'centos')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
}
if(isset($_POST['submit']))
{
Connect();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $IP1; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>cleartuts</label>
</div>
<div id="right">
<div id="content">
Cennecting to ' <?php echo $IP1; ?> and Data is : <?php echo $data2; ?> Go Back
</div>
</div>
</div>
</body>
</html>
I am getting server error(500) while using above php code.
Please note that I am working on vim editor on a linux server for the script and do not have any logs available. Also, If I just run the program to do ssh connection and execution of command in server directly (php 123.php), I get proper data on console.
Please help.

writing JavaScript with PHP

I am trying to right some some JavaScript to define a variable using PHP. The variable has a little jQuery in it which should get rendered by the client. I say "should" but really mean I want it to. What should I do to make o2 the same as o1 (i.e. covert the jQuery)?
Thank you!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title></title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
$(function() {
var o1={"foo":{"bar": "abc/index.php?="+$("#id").val()}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "'.$d.'?id=\"+$(\"#id\").val()"}}';
//echo($json);
$json=json_decode($json);
//echo(print_r($json,1));
$json=json_encode($json);
//echo($json);
echo("var o2=".$json.";");
?>
console.log(o2);
});
</script>
</head>
<body>
<input type="hidden" id="id" value="123" />
</body>
</html>
You have this:
<input type="hidden" id="id" value="123" />
So I can assume that you are getting that id from a db right? Maybe like this:
<input type="hidden" id="id" value="<?php echo $row[id];?>" />
If this is the case do the following (I tested and works):
You don't need hidden inputs to do it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title></title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
$(function() {
<?php
//From your SQL query you obtained
//$row['id'] = 123;
$id = $row['id'];
?>
var o1={"foo":{"bar": "abc/index.php?=" + <?php echo $id ?>}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "'.$d.'?='.$id.'"}}';
//echo($json);
$json=json_decode($json);
//echo(print_r($json,1));
$json=json_encode($json);
//echo($json);
echo("var o2=".$json.';');
?>
console.log(o2);
});
</script>
</head>
<body>
</body>
</html>
Untested, may contain errors:
<script>
$(function() {
var o1={"foo":{"bar": "abc/index.php?="+$("#id").val()}};
console.log(o1);
<?php
$d='abc/index.php';
$json='{"foo":{"bar": "' . $d . '?="+$("#id").val()}}';
echo("var o2=".$json.';');
?>
console.log(o2);
});
</script>
I prefer make json with the json_encode, to avoid unexpected problems with parse and validation of json.
the json_encode and json_decode only work with UTF-8, another charset will result in a blank string, if it has special chars.
<script>
<?php
$d = "abc/index.php";
$json['foo']['bar'] = $d . '?id=$("#id").val()';
echo "var o2 = ".json_encode($json);
?>
console.log(o2);
</script>

Checking Login from form then redirecting to another page

This was my last attempt that I tried as a last effort:
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Customer Login</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="wrapper">
<div class="login">
<form name="loginForm" action="loginCheck.php" method="post">
<?php require("protect/serverInfo.php"); ?>
Email: <input type="text" name="Email" maxlength="35" /><br />
Password: <input type="text" name="Password" maxlength="4" /><br />
<input type="submit" name ="submit"/>
</form>
</div>
</div>
</body>
</html>
loginCheck.php
<?php
session_start();
$_SESSION['email'] = $_POST['Email'];
$_SESSION['password'] = $_POST['Password'];
require("protect/serverInfo.php");
$myusername=$_POST[Email];
$mypassword=$_POST[Password];
$result = mysql_query("SELECT * FROM Customers WHERE Email='$myusername' AND Password=$mypassword");
$count=mysql_num_rows($result);
if($count==1){
header('Location: customer.php');
exit();
}
else{
header('Location: index.php');
exit();
}
?>
customer.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<?php
session_start();
$myusername = $_SESSION['email'];
$mypassword = $_SESSION['password'];
?>
</head>
<body>
<?php
echo"success";
?>
</body>
</html>
I just need a very simple way to have a form post, the post info to be checked if correct then redirect if correct and pass the post data with it. I have been trying to use sessions and redirects but it doesn't to work quite right. What is the easiest way to accomplish this. At the moment I have been using PHP to check the login info from a MySQL database.
You need to use "session_start()" before you do anything else on the page.
Other few things, I avoid storing passwords on a page.. it just seems like a security issue.
Your login form should generate $_SESSION data based on the mysql information returned from the queury, not the form information that the user submited. You need to check against your customers database, to make sure they are an actual customer.
Also, avoid using the "header()" function, especially when working with sessions. I typically have a "redirect" function in php that does something like this...
function redirect($url) {
echo "<script type='text'/javascript'>window.location='" . $url . "';</script>";
}

Categories