I want to make a program what will count clicks after click button.
I have this code but it don't work. I use mysqli to connect to database and I use query to insert value to database and query to select from database.
<html>
<head>
<meta charset="UTF-8">
<title>Click</title>
</head>
<body>
<form action="#" method="post">
<input type="submit" name="click" value="Klikni mě">
<br>
<?php
if(isset($_POST["click"])){
$connection=new mysqli("hidden","hidden","hidden","hidden");
if($connection == false){
die("Sorry jako");
}
$query="INSERT INTO klik (klikcount) VALUES ('$klik')";
if($connection->query($query) == false){
die("Promiň");
}
$sql="SELECT klikcount FROM klik";
$result=$connection->query($sql);
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
echo $row["klikcount"];
}
}
$klik=$klik+1;
}
?>
</form>
</body>
</html>
thanks.
I try solve your code and I made some changes.
Change position of "$klik = $klik+1;"
Add another SELECT
My new code:
<html>
<head>
<meta charset="UTF-8">
<title>Click</title>
</head>
<body>
<form action="#" method="post">
<input type="submit" name="click" value="Klikni mě">
<br>
<?php
if(isset($_POST["click"])) {
$connection = new mysqli("hidden","hidden","hidden","hidden");
if($connection == false) {
die("Sorry jako");
}
$sql="SELECT klikcount FROM klik";
$result=$connection->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
$klik = $row["klikcount"];
}
}
$klik = $klik+1;
$query = "INSERT INTO klik (klikcount) VALUES ('$klik')";
if($connection->query($query) == false) {
die("Promiň");
}
$sql = "SELECT klikcount FROM klik";
$result = $connection->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["klikcount"];
}
}
}
?>
</form>
</body>
</html>
Related
I can't get the users name to get updated or to deleted the user. Whatever i try doesn't work. When i click on the update button it just goes to the index page and nothing happens and the same happens when i click on the delete button too.
this is the codes i have on my server.php page:
<?php
session_start();
// initializing variables
$idmedlemmer = "";
$brukernavn = "";
$email = "";
$fornavn = "";
$etternavn = "";
$errors = array();
// connect to the database
$db = mysqli_connect("localhost", "root", "", "mymusic");
// Update User
if (isset($_POST['update'])) {
// receive all input values from the form
$brukernavn = mysqli_real_escape_string($db, $_POST['brukernavn']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($brukernavn)) { array_push($errors, "Feltet kan ikke være tomt"); }
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT brukernavn FROM medlemmer LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['brukernavn'] === $brukernavn) {
array_push($errors, "Brukernavn eksisterer allerede");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
mysqli_query($db, "UPDATE medlemmer SET brukernavn='$brukernavn' WHERE idmedlemmer=$idmedlemmer");
$_SESSION['message'] = "Brukernavnet har blitt oppdatert";
header('location: index.php');
}
}
// ...
// ...
// Delete User
if (isset($_GET['brukernavn']))
if (isset($_GET['slett'])) {
$query = sprintf("DELETE FROM medlemmer WHERE idmedlemmer='%s'");
mysqli_query($db, $query);
$_SESSION['brukernavn'] = $brukernavn;
$_SESSION['success'] = "Bruker har blitt slettet";
header('location: Login.php');
}
?>
And this is what i have on my update page:
<?php include('server.php')?>
<?php
if (isset($_GET['update'])) {
$brukernavn = $_GET['update'];
$record = mysqli_query($db, "SELECT * FROM medlemmer WHERE brukernavn=$brukernavn");
if (count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$name = $n['brukernavn'];
}
}
?>
<html>
<head>
<title>Edit Data</title>
<link rel="stylesheet" type="text/css" href="stilark.css">
<meta charset="utf-8">
</head>
<body>
<form name="form1" method="post" action="index.php">
<table border="0">
<tr>
<td>Name</td>
<td><input type="text" name="brukernavn" value="<?php echo ($_SESSION['brukernavn']); ?>"></td>
</tr>
<tr>
<td><input type="hidden" name="idmedlemmer" value="<?php echo $idmedlemmer;?>"></td>
<td><input type="submit" name="update" value="update"></td>
</tr>
</table>
</form>
and this is what i have on my delete page:
<?php include('Server.php')?>
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="stilark.css">
<head>
<title>Slett Bruker</title>
</head>
<body>
Tilbake
<br/><br/>
<form name="form1" method="post" action="index.php">
<table border="0">
<tr>
<td><input type="hidden" name="idmedlemmer" value="<?php echo $_GET['idmedlemmer'];?>"></td>
<td><input type="submit" name="slett" value="slett"></td>
Delete
</tr>
</table>
</body>
</html>
I have tried to check on youtube videoes and read many articles but i just cant seem to get anywhere so i am totaly stuck and appreciate all the help i can get. Hope that you can help me
i have code that work with me good but my problem i need if result 0 redirect as header('Location: checkin.php?identity='.$identity);
here my code
<?php
mysql_connect("localhost", "xxxxxx", "xxxxxx") or die("Connection Failed");
mysql_select_db("xxxxxx")or die("Connection Failed");
//added to prevent sql injection
$identity = mysql_real_escape_string($_POST['identity']);
$query = "select * from blacklist where identity = '$identity'";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "{$line['number']}<br>";
echo "{$line['identity']}</div></td><br>";
echo "{$line['reason']}</div></td><br>";
}
?>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form method="post" name="display" action="index.php" />
<b>Enter the register number:</b><br>
<input type="text" name="identity" />
<input type="submit" value="Search" />
</form>
</html>
You should add this in the first line.
And please use MySQLi.
if (isset($_POST['identity'])) {
//here your mysqli connect
//and more
}
<form ....... input
Before the query check if number of rows select is 0.
if(!(mysql_num_rows($result) == 0)) {
//Your while logic here
}
else
{
header('Location: checkin.php?identity='.$identity);
}
I want to get information from a database, and I want to put it in selectbox as option.
I tried to do it but I could not not put it what is my mistake?(db can connect I just delete server name )
I am not sure how I can put db rows in selectbox as option.
therefore, I think my code has a problem.
p.php
<?php
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM test" ;
$result = mysqli_query($conn, $sql) or die("Query: ($sql) [problem]");
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_row($result)) {
display("<option value=$row[seat_id]>",$row[seatnumber]."\n");
}
display ("</select>", "\n");
} else {
echo "0 results";
}
mysqli_close($conn);
function display($tag , $value) {
echo $tag . $value ;
}
?>
p.html
<html>
<head>
<meta charset="utf-8">
<link href="" rel="stylesheet" type="text/css" />
</head>
<script type="text/javascript">
function transfer(){
var pix = document.getElementById('pix').value;
document.abc.test.value =pix;
}
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
</script>
<script>
function ajaxWay() {
// syntax: $.post(URL,data,callback);
$.get("p.php", function(dataFromtheServer) {
$("#result").html(dataFromtheServer);
});
}
</script>
<body>
<div id="a" style="text-align:center;">
<form name="abc" method="get" action="p.php">
<select id='pix' onchange='ajaxWay()'>
<input type="button" value="click" onclick="transfer();">
<input type="text" name="test" id="test">
</form>
</div>
</body>
</html>
If your main problem is that you are not able to embed options into your HTML, try something like this:
<html>
<head>
<meta charset="utf-8">
<link href="" rel="stylesheet" type="text/css" />
</head>
<script type="text/javascript">
function transfer(){
var pix = document.getElementById('pix').value;
document.abc.test.value =pix;
}
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
</script>
<script>
function ajaxWay() {
// syntax: $.post(URL,data,callback);
$.get("prefinal.php", function(dataFromtheServer) {
$("#result").html(dataFromtheServer);
});
}
</script>
<body>
<div id="a" style="text-align:center;">
<form name="abc" method="get" action="p.php">
<?php
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM test" ;
$result = mysqli_query($conn, $sql) or die("Query: ($sql) [problem]");
?>
<select id='pix' onchange='ajaxWay()'>
<?php
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_row($result))
{
echo '<option value="' . $row[0] . ">' . $row[0] . '</option>';
}
}
mysqli_close($conn);
?>
</select>
<input type="button" value="click" onclick="transfer();">
<input type="text" name="test" id="test">
</form>
</div>
</body>
</html>
Note that in the above I am using your code from p.php to generate the actual content for the select options. The form should NOT submit to p.php, but to some other script that will process the form and perform the required actions. I would help more if I knew more about what you were trying to achieve!
so I am trying to display multiple results from a database when a query is searched, the query is passed from a search box on another page.
I have it displaying one result, but that is all it will display.
I need it to display all the results that are relevant to the search query.
the php code is below
<meta charset="UTF-8">
<?php
$mysqli = new mysqli('localhost', 'scott', 'tiger','courses');
if ($mysqli->connect_errno)
{
die('Database connection failed');
}
//$m->set_charset('utf8');
$search_sql = "
SELECT title, summary, id
FROM course
WHERE title LIKE '%".$_POST['searchBar']."%'";
$result = $mysqli->query($search_sql) or die($mysqli->error);
$search_result = $result->fetch_assoc();
?>
<!doctype html>
<head>
<meta charset="utf-8">
<h1>Search Results</h1>
</head>
<body>
<h3><?= $search_result['title'] ?></h1>
<p><?= $search_result['summary'] ?></p>
</body>
and the code for the search bar
<!doctype html>
<html>
<Head>
<meta charset = "utf-8">
<title>Search</title>
</head>
<body>
<h2>Search</h2>
<form name="search" method="post" action="SearchResultsPage.php">
<input name="searchBar" type="text" size="40" maxlength="60" />
<input type="submit" name="Submitsearch" value="Search" />
</form>
</body>
Does anyone have any suggestions?
Thanks in advance;
You will need to place it in a while loop to show multiple results, the fetch function you're using will only retrieve one row, if you place it in a loop you can keep fetching until there is nothing to fetch:
//$m->set_charset('utf8');
$search_sql = "
SELECT title, summary, id
FROM course
WHERE title LIKE '%".$_POST['searchBar']."%'";
$result = $mysqli->query($search_sql) or die($mysqli->error);
?>
<!doctype html>
<head>
<meta charset="utf-8">
<h1>Search Results</h1>
</head>
<body>
<?PHP while($search_result = $result->fetch_assoc()) { ?>
<h1><?= $search_result['title'] ?></h1>
<p><?= $search_result['summary'] ?></p>
<?PHP } ?>
</body>
P.S. your code is vulnerable to SQL injection, you should read about prepared statements. More Info on that
You can iterate over your query results with a while loop. To complete the example I added the necessary data cleaning.
<?php
// function to clean post data
function cleanPost(&$value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = cleanPost($v);
}
return $value;
}
else {
$value = mysql_real_escape_string($value);
return trim(htmlentities(strip_tags($value)));
}
}
// search function
function search() {
// check if post data is set
if (isset($_POST['searchBar'])) {
// make link with db
$link = mysqli_connect('localhost', 'scott', 'tiger','courses');
if (!$link)
return false;
}
// clean your post data
$cleanPostData = cleanPost($_POST);
// query
$sql = "SELECT title, summary, id FROM course WHERE title LIKE '%".$cleanPostData['searchBar']."%'";
$result = mysqli_query($link, $sql);
// iterate over results
if (isset($result) && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
// here is your data
echo $row['title'] . "< br/>";
echo $row['summary'] . "< br/>";
echo $row['id'] . "< br/>";
}
}
}
}
// call search function
search();
?>
I want to select query in specific page. I created table in database:
First, I added phpexec on plugin for using PHP on page. Then, I tested Select query and it was ok. Finally, I want to create form for checking Serial Number. Here is my code:
<html>
<head>
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"><br><br>
Serial Number: <input type="Text" Name="Num1"><p>
<input type="Submit" value="Calculate">
</form>
<phpcode>
<?php
if (count($_POST) > 0 && isset($_POST["Num1"])
{
$servername = "localhost";
$username = "******";
$password = "*******";
$dbname = "******";
$serialNum = $_POST["Num1"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Install FROM SN WHERE serial = $serialNum";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["Install"];
}
} else {
echo "no result";
}
$conn->close();
}
?>
</phpcode>
</body>
</html>
Actual output is:
How to solve this issue?
Your form is broken... Try it this way
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>Serial Number: <input type="Text" Name="Num1" value=""></p>
<input type="Submit" value="Calculate">
</form>
hi maybe you can try with this
is a little example and check your values and id, names for values
and sintaxis for html and php
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<br><br>
Serial Number: <input type="text" name="Num1" id="Num1">
<br><br>
<input type="submit" name="submit" id="submit" value="Calculate">
<br><br>
</form>
<phpcode>
<?php
if(isset($_POST["submit"]))
{
function Conect()
{
if (!($link=mysql_connect("localhost","username","password")))
{
echo "error to conect to database.";
exit();
}
if (!mysql_select_db("databasename",$link))
{
echo "Error to select database.";
exit();
}
return $link;
}//end function Conect
$serialNum = mysql_real_escape_string($_POST["Num1"]);
$query="SELECT Install FROM SN WHERE serial = $serialNum";
$action=mysql_query($query,$link) or die("Error: ".mysql_error());
if(mysql_num_rows($action) > 0)
{
?>
<table border="1">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>INSTALL</td>
</tr>
<?php
while($row=mysql_fetch_array($action))
{
echo "<tr>".
"<td>".$row["Install"]."</td>".
"</tr>";
}//end while
}
else
{
echo "don't exist recordsfor list ";
}//end if
mysql_close($link);
}//end if
?>
</phpcode>
</body>
</html>
good luck ..!!