I'm not that big of a genius in PHP nor in SQL but I'm getting this strange error, where I see the info that I want to send to the data base on the link and I get no errors, however my database is not getting updated.
<!DOCTYPE html>
<html lang="PT">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Alteração do produto</title>
</head>
<body>
<?php
$lig=new mysqli("localhost", "root", "", "pie");
if($lig->connect_errno != 0){
echo ("Base de dados indisponível");
}
$instrucao=$lig->prepare("UPDATE produto SET produto1 = ? , quantidade1 = ? , preco1 = ? WHERE codigo1 = ?");
$instrucao->bind_param("isid", $_GET['codigo1'], $_GET['produto1'], $_GET['quantidade1'], $_GET['preco1']);
$resultado=$instrucao->execute();
if($resultado==FALSE){
echo "<p>produto não editado</p>";
}
else {
//header( "Location: shoppinglist.php" );
}
$lig->close();
?>
<form method="get">
<label>Código: <input name="codigo1" readonly value="<?php echo $_GET['codigo1'] ?>"></label>
<label>Nome: <input name="produto1" value="<?php echo $_GET['produto1'] ?>"></label>
<label>Quantidade: <input name="quantidade1" value="<?php echo $_GET['quantidade1'] ?>"></label>
<label>Preço: <input name="preco1" value="<?php echo $_GET['preco1'] ?>"></label>
<button type="submit" value="POST">Alterar</button>
</form>
</body>
</html>
I know that there can be some syntax errors on this code, and I'm sorry if so. Thanks in advance!
try with
$instrucao->bind_param("sidi", $_GET['produto1'], $_GET['quantidade1'], $_GET['preco1'], $_GET['codigo1']);
i think you are binding the wrong params shouldn't the first one be $_GET['produto1'] (since it's the first one in the update statement..
Related
I am trying to show people their profiles from my database using PHP.
But every time a button is clicked I want the person_id ($which_person) to go up by 1 so the next person's profile is shown. I don't know how to do that because every time I click a button the page reloads so the variable "which_person" gets reset to 1.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets-putinhouse/css/style1.css" rel="stylesheet" type="text/css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Festive&display=swap" rel="stylesheet">
<title>Put people in a house</title>
</head>
<body>
<?php
include "db/connection.php";
$conn = create_connection();
$which_person = 1; //This resets to "1" every time the page reloads
$getSql = "select * from Person where person_id = 1;";
$data_labels = mysqli_query($conn, $getSql)->fetch_all(MYSQLI_ASSOC);
?>
<div class="pagesize">
<h1>Put people in a house</h1>
<img src="assets-putinhouse/images/create_account.png" alt="profilepic" class="image_person">
<?php
foreach($data_labels as $labels)
{
?>
<li class="labels" data-id="<?php echo $labels['person_id'];?>">
<?php echo $labels["firstname"] ?>
<br>
<br>
<?php echo $labels["secondname"] ?>
<br>
<br>
<?php echo $labels["gender"] ?>
<br>
<br>
<?php echo $labels["descriptie"] ?>
</li>
<?php
}
?>
<br>
<form method="GET">
<input type="submit" class="slytherin_button" value="Slytherin" name="slytherin_button_name">
<br>
<input type="button" class="gryffindor_button" value="Gryffindor" name="gryffindor_button_name">
<br>
<input type="button" class="hufflepuff_button" value="Hufflepuff" name="hufflepuff_button_name">
<br>
<input type="button" class="ravenclaw_button" value="Ravenclaw" name="ravenclaw_button_name">
<br>
<input type="button" class="nextperson_button" value="Go to next person">
<p class="text_firstname">Firstname: </p>
<p class="text_name">Name: </p>
<p class="text_gender">Gender: </p>
<p class="text_info">Info: </p>
<?php
if(isset($_GET['slytherin_button_name'])) {
onFunc($conn, $which_person);
}
if(isset($_GET['gryffindor_button_name'])) {
offFunc();
}
function onFunc($conn, $wich_person){
$sqlUpdate = "UPDATE Person SET slytherin = slytherin + 1 WHERE person_id like '$which_person';"; //this does the value "slytherin" of which_person go up by 1;
mysqli_query($conn, $sqlUpdate);
$wich_person = $wich_person + 1; //which person goes up by 1 so the next person will be shown but gets reset at the start
}
function offFunc(){
echo "Button is clicked";
}
?>
</div>
<script src="assets-putinhouse/js/script.js"></script>
</body>
</html>
´´´
(Untested) Instead of:
$wich_person = 1;
It seems you could do something like:
if (isset($_GET["id"]) {
$wich_person = (int)$_GET["id"] + 1;
}
else {
$wich_person = 1;
}
and then add the following to your form:
<form action="?<?php echo $wich_person ?>" method="GET">
...
</form>
And also, unless I'm mistaken it seems you've forgot to add </form> to your code.
In any event, this code should add the current ID to your URL every time you press submit, where then it gets increased by 1.
Edit: This is intended as a quick solution based on the code structure you provided, but upon reflection not sure if it's exactly what you're looking for. More robust solutions could be had, such as cookies or sessionStorage.
Im kind of new to programming (taking a course currently) and our task over the weekend is to build a website (really basic) where we have to solve a addition of 2 random numbers. I was able to create those random numbers, but what with is, to create something like a field the user (in this case me) has to fill out and "submit"; and then the website should show my own anwser and the correct one. The issue im facing in addition to that is, that when i create a submit function the website refreshes and those random numbers change each time.
My code looks like this (forgive me for using german terms):
<?php
$zufallszahl = rand(1,100) . "+";
$zufallszahl1= rand(1,100);
$solution= bcadd($zufallszahl,$zufallszahl1);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Homework Taschenrechner</title>
</head>
<body>
<h1>Additionen</h1>
<hr>
<h4>Addiere die folgenden Zahlen:</h4>
<p> <?php echo $zufallszahl; echo $zufallszahl1 ?> <p>
<form method="post">
<input type="number" name="Usereingabe" min="0" placeholder="Deine Antwort">
</form>
</body>
</html>
I would really appreciate a "simple" solution, I googled this issue already for an hour, but most solutions are just too complicated for me right now, and i end up not understanding what the code is about. So just to sum up, I would like to know how to create a submit field that doesnt refresh the website so the random numbers stay and a field that "checks" the answer and says its true/false.
Although, if you were allowed to use JS, it would be a lot simpler. However, since you wanted it in just PHP, the code below should work. Feel free to modify it as per your needs
<?php
if(!isset($_POST['zufallszahl']) && !isset($_POST['zufallszahl1'])) {
$zufallszahl = rand(1,100);
$zufallszahl1= rand(1,100);
// $solution= bcadd($zufallszahl,$zufallszahl1);
} else {
$zufallszahl = $_POST['zufallszahl'];
$zufallszahl1 = $_POST['zufallszahl1'];
$solution = bcadd($zufallszahl,$zufallszahl1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Homework Taschenrechner</title>
</head>
<body>
<h1>Additionen</h1>
<hr>
<h4>Addiere die folgenden Zahlen:</h4>
<p> <?php echo $zufallszahl . " + "; echo $zufallszahl1 ?> <p>
<form method="post">
<input type="number" name="zufallszahl" value="<?php echo $zufallszahl; ?>" readonly />
<input type="number" name="zufallszahl1" value="<?php echo $zufallszahl1; ?>" readonly />
<input type="number" name="Usereingabe" min="0" placeholder="Deine Antwort" value="<?php echo $_POST['Usereingabe'] ?? ''; ?>">
<button>Check Answer </button>
<?php if(isset($_POST['Usereingabe'])){ ?>
<?php if($_POST['Usereingabe'] == $solution)
echo "Correct Answer";
else {
echo "Wrong Answer. Correct answer is $solution";
} ?>
<?php } ?>
</form>
</body>
</html>
NOTE
If you are using version below PHP 7, just use <?php echo isset($_POST['Usereingabe'])? $_POST['Usereingabe'] : ''; ?> in place of <?php echo $userSolution ?? ''; ?>
So you can keep them in a _SESSION variable and when they submit the answer you can check the _POST variable against the _SESSION one
<?php
if(isset($_POST['Usereingabe']))
{
if($_POST['Usereingabe'] == $_SESSION['sol']){
$check = "correct answer";
}
else
{ $check = "wrong answer"; }
}
else {
$zufallszahl = rand(1,100) . "+";
$zufallszahl1= rand(1,100);
$solution= bcadd($zufallszahl,$zufallszahl1);
$_SESSION['zufallszahl1']=$zufallszahl1;
$_SESSION['zufallszahl']=$zufallszahl;
$_SESSION['sol']=$solution;
}
// do anything else u want
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Homework Taschenrechner</title>
</head>
<body>
<h1>Additionen</h1>
<hr>
<h4>Addiere die folgenden Zahlen:</h4>
<?php if(isset($check)) { echo $check; } ?>
<p> <?php echo $_SESSION['zufallszahl']; echo $_SESSION['zufallszahl1']; ?> <p>
<form method="post" action="*your file.php*">
<input type="number" name="Usereingabe" min="0" placeholder="Deine Antwort">
<button type="submit">Submit!</button>
</form>
</body>
</html>
You could use two extra input tags in your form of type="hidden" that contain the random numbers in their value fields, and check if they are empty. If they are, generate new ones, otherwise keep the old ones.
I have 3 PHP files , the first one contain fields to compile which mean in html a form. the second , the code that took the fields from the first file and register them inside a folder .txt with a title=the date and time , the third file confirm the registration: i did the 3 files they work : but i would like to reload the fields page, in case where I click on the button and one or more of the fields is empty, with a comment near the field empty :
the first file form.php is:
I put the code only with one field to don't disturb more :`
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<title>Titre de la page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
}
<form action="POST.PHP" method="POST">
<input type="text" name="name" placeholder="name" value="name">
<button type="button">Click Me!</button>
</form>
</body>
</html>
the second file POST.php contain :
<?php
$nameErr= "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Missing";
} else {
$name = $_POST["name"];
$filename="file/". date("Y-m-d_H_m_s") . "_" . uniqid() . ".txt" ;
$fp = fopen( $filename, "w" );
fwrite($fp, $name);
fclose($fp);
include 'registred.php';
}
}
?>
and the third file registred.php contain :
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Titre de la page</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h2>completed registration</h2>
<h3>wish to see you soon !!</h3>
<ul>
<li >name:<?=htmlspecialchars($name)?></li>
</ul>
</body>
</html>
This is not the best way to do that but its good enough to give you a idea about how u r gonna manage this.
<?php session_start();?>
<form action="post.php" method="post">
<input type="text" name="txtName">
<?php
if(isset($_SESSION['mising_data'])){
echo $_SESSION['mising_data'];
}
?>
<input type="submit" name="btnSubmit">
</form>
now in post.php file
<?php
session_start();
if (isset($_POST['btnSubmit'])) {
if (empty($_POST['txtName'])) {
$_SESSION['mising_data'] = 'missing Data';
header('Location: form.php');
}
}
?>
to handle this professionally u should use jquery ajax() method
I'm having a problem with a submit button in a form, when clicked it first reloads the current page, which creates a submit button inside of the original one before posting to the post page.
You can see the behavior example here:
http://tampary.com/pick_game.php?c_id=15&p_id=0&f_id=1&l_id=1
You can click back in your browser after clicking "start game" and you will see the nested controls. Using Chrome and Firefox, you get same results. Any ideas what could be causing this behavior? ideally, I just want the php page to be redirected to after the form submit to just load up cleanly. Any help would be greatly appreciated
The complete code is as follows:
<?php
require_once(dirname(__FILE__).'/grl-lib/db.php');
require_once(dirname(__FILE__).'/grl-lib/ui.php');
require_once(dirname(__FILE__).'/grl-lib/family.php');
require_once(dirname(__FILE__).'/grl-lib/location.php');
require_once(dirname(__FILE__).'/grl-lib/contact.php');
$ui = new x_ui;
$ui->set_ui(1);
if (!empty($_POST)){
$p_id = $_POST['p_id'];
$f_id = $_POST['f_id'];
$l_id = $_POST['l_id'];
$c_id = $_POST['c_id'];
}else{
$p_id = $_GET['p_id'];
$f_id = $_GET['f_id'];
$l_id = $_GET['l_id'];
$c_id = $_GET['c_id'];
}
?>
<!DOCTYPE HTML>
<html lang="<?php echo $ui->getLocale($ui->language_id,1); ?>">
<head>
<?php include_once('grl-lib/gen_include.php'); ?>
<meta name="robots" content="noindex"> <!-- no indexing this page -->
<link href="css/main.css" rel="stylesheet" type="text/css">
<link href="css/devices.css" rel="stylesheet" type="text/css">
<title>Tampary<?php ' '._('game'); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name=viewport content="width=device-width, initial-scale=1">
</head>
<body class="color_bg">
<?php include('standard/header.txt'); ?>
<!-- all content -->
<div class="entire_content_length aling_text_c">
<div class="max_width_responsive align_container_c spc_a_5">
<div class="align_container_c spc_a_5 container_c">
<form id="pick_game" action="game.php" method="post">
<?php
$html = '<input type="hidden" name="p_id" id="p_id" value="'.$p_id.'"><input type="hidden" name="f_id" id="f_id" value="'.$f_id.'"><input type="hidden" name="l_id" id="l_id" value="'.$l_id.'"><input type="hidden" name="c_id" id="c_id" value="'.$c_id.'">';
//$html = $html.'<input type="submit" value="'._('Start Game').'">';
echo $html;
?>
<input type="submit" value="Start Game">
</form>
</div>
</div>
</div>
<?php include('standard/footer.txt'); ?>
</body>
</html>
I see you are using an Ajax script, which is fine. But is definitely the culprit cause I can see it responding the entire page in firebug.
I will bet you that your front page is Ajax to and you append something somewhere (can't find it this fast).
Whatever the case, I'm not convinced that what ever your doing (loading an entire page through AJAX) is good practice.
Looking at your AJAX requests will solve your inception button problem
I'm trying to update my database records with the following code but am having no luck what so ever. Anybody care to help? Thanks
<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Project Sproom</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
if(!empty($_POST['username']) && !empty($_POST['email']))
{
$newusername = mysql_real_escape_string($_POST['username']);
$newemail = mysql_real_escape_string($_POST['email']);
$edit = mysql_query("UPDATE users (Username, EmailAddress) VALUES('".$newusername."', '".$newemail."') WHERE UserID=".$_SESSION['UserID']."");
// }
?>
<div id="container">
<div id="homemenu">
<ul id="navlist">
<li id="active">Home</li>
<li>Edit Profile</li>
</ul>
</div>
<div id="homemain">
<h1>Edit Profile</h1>
<p>This will be the edit profile when i have figured out how to do it...</p>
<br />
<form method="post" action="profile.php" name="editprofile" id="editprofile">
<label for="username">Username: </label> <input type="text" name="username" id="username" value="<?=$_SESSION['Username']?>"/><br />
<label for="email">E-Mail: </label> <input type="text" name="email" id="email" value="<?=$_SESSION['EmailAddress']?>"/> <br />
<input type="submit" name="editprofile" id="editprofile" value="Submit" />
</fieldset>
</form>
</div>
</div>
<?php
}
else
{
?>
<meta http-equiv="refresh" content="0;index.php">
<?php
}
?>
You're using INSERT syntax for an UPDATE query. The syntax should be like this:
UPDATE users SET Username = 'username', EmailAddress = 'email' WHERE UserID = 1;
Docs here.
You haven't connected to the MySQL database, have you?
I didn't see that in this code...
Or is that part of the included "base.php" on top of this script?
I am afraid you need fist establish a connection to a certain MySQL database before trying to update a row in a table.
Edit:
Okay, well then. Try issue the following line of code after the update:
echo "edit was " .$edit;
This is to check whether the update query was executed successfully (in which case it should echoes true) or failed (in which case it echoes false).
So at least you can tell the result of such a mysql_query.
$edit = mysql_query("UPDATE users SET Username='".$newusername."', EmailAddress='".$newemail."' WHERE UserID=".$_SESSION['UserID']."");
Try this