Can´t update database with sent value - php

I can not update my database with sent variable "$id_zaznamu". I do not know why. If I use number, for example 5 or 4, it works. With variable "$id_zaznamu" doesn´t. But in the different examples with variabable $id_zaznamu everythning works correctly. In other words exactly the same variable "$id_zaznamu" works correctly in different situations, in this case not. It seems to me as if it is not integer, and it is. For any help I will be very thankful.
PHP:
$id_zaznamu=$_GET['id_zaznamu']; //get from different page
$error="";
if(isset($_GET['modify']))
{
$zadavatel=mysqli_real_escape_string($link,$_GET['zadavatel']);
$kontakt=mysqli_real_escape_string($link,$_GET['kontakt']); $somevalue=$_GET['someid'];
if(mysqli_query($link, "UPDATE zaznamy SET zadavatel='$zadavatel', kontakt='$kontakt' WHERE id_zaznam='$somevalue'"))
{
header("location:vypis.php");
}else
{
echo "Error: " . $sql . "<br>" . mysqli_error($link);
}
}
HTML:
<form method="GET" action="upravbyt.php"><br>
<label>Name:
<input type="text" name="zadavatel" class="field col-12" placeholder="Meno zadávateľa"/></label><br><br>
<label>Contact:
<input type="text" name="kontakt" class="field col-12" placeholder="Email"/></label><br><br>
<input type="hidden" name="someid" value="<?php $id_zaznamu=$_GET['id_zaznamu'];?>">
<input type="submit" name="modify" id="submit" class="btn" value="Upraviť"/><br><br>
</form>

You should add
<input name="id_zaznamu" value="...">

Related

Problem: Part of the PHP exercise does not run correctly

I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.
<?php
if (isset($_POST['submit_btn']))
{
$id = $_POST['id'];
$fn = trim($_POST['name']);
$ln = trim($_POST['lastname']);
$age = trim($_POST['age']);
$q = "UPDATE `users` SET `fn` = '$fn',
`ln` = '$ln',
`age` = '$age'
WHERE id = '$id'";
mysqli_query($dbconnect,$q);
if (mysqli_affected_rows($dbconnect) > 0)
redirect("?msg=ok&id=**$id**");
else
redirect("?msg=error&id=**$id**");
}
else
echo ("Not In If(isset)");
?>
<form action="" method="post">
<label for="name">FirstName:</label>
<input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
<br>
<label for="lastname">LastName:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
<br>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $row['age']?>">
<br>
<input type="submit" name="submit_btn" value="Save">
<a href="index2.php">
Back
</a>
</form>
</body>
Bold sections do not work here.
Below is a picture of the result:
In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.
Sorry if the result is styleless and boring and I just created this page to practice php😁
Thank you for being responsive🙏🙏🙏
You are mistaking a POST request with a GET request.
Part, which appears in the URL is sent to the webserver in GET request.
Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).
Please check the examples in php.net:
POST variables: https://www.php.net/manual/en/reserved.variables.post.php
GET variables: https://www.php.net/manual/en/reserved.variables.get.php
You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.
if (isset($_GET['submit']))
{
$number = $_GET['number'];
echo $number
? "Number which was submitted: $number <br>"
: 'Number is not set';
} else {
echo 'Form has not been yet submitted';
}
?>
<form action="" method="get">
<input type="number" name="number" placeholder="Number">
<input type="submit" name="submit" value="Save">
</form>

values sent with POST/GET will not print as variables

I am working on a project to take two submitted variables and turn them into php variables (eventually they will multiply together) but as of now I cannot get the variables to be treated as such/echo.
I tried changing from POST to GET and the variables are sent through (appear in query line) but they don't print on the page
<?php
if (isset($_POST['submit'])) {
echo $_POST['length'];
echo $_POST['numPass'];
}
?>
<form method="post" action="">
<input type="number" name="length">
<input type="number" name="numPass">
<input type="submit">
</form>
I expect that the variables will be echoed as a regular statement.
ie. length=2 and numPass=4
24
You can get the $_GET and $_POST request using $_REQUEST
if (isset($_REQUEST['submit'])) {
echo $_REQUEST['length'];
echo $_REQUEST['numPass'];
}
<form method="post" action="">
<input type="number" name="length">
<input type="number" name="numPass">
<input type="submit" value="submit">
</form>
$POST['submit'] does not exist because your submit button does not have a name, it needs a name the same as your other inputs
It is better to check the input to avoid possible errors. You can try its example:
<?php
if( $_POST["length"] && $_POST["numPass"] ) {
echo "1: " . $_POST['length'] . "<br>";
echo "2: " . $_POST['numPass'] . "<br>";
echo $_POST['length'] * $_POST['numPass'];
}
?>
<form method="post" action = "<?php $_PHP_SELF ?>">
<input type="number" name="length">
<input type="number" name="numPass">
<input type="submit">
</form>

How to read post values easier in PHP

I am posting a values from a HTML form to a php file. In my current code I am submitting several post values and have to do a check for each post variable if they are set.
I'd like to know if there is a more effective way of doing this. One requirement is that the values will be inserted in order.
HTML form:
<form>
<input type=text name=exercise1> <input type=text name=sets1>
<input type=text name=exercise2> <input type=text name=sets2>
<input type=text name=exercise3> <input type=text name=sets3>
<input type=text name=exercise4> <input type=text name=sets4>
...
</form>
SQL table:
id autoincrement
exercise varchar(200)
sets varchar(10)
I tried the next code:
$exercise1 = $_POST['exercise1'];
$sets1 = $_POST['sets1'];
$exercise2 = $_POST['exercise2'];
if(isset($exercise1)){
$sql = "insert into exercises (exercise, sets) values ($exercise1, $sets1)";
execute_sql($sql);
}
if(isset($exercise2)){
$sql = "insert into exercises (exercise, sets) values ($exercise2, $sets2)";
execute_sql($sql);
}
Default form method is GET, so you are probably not getting anything while trying to read $_POST. To fix it, you need to change this:
<form>
to this:
<form method="post">
To make it easier, you should redefine your form, so it would be an array:
<input type="text" name="exercise[]"> <input type="text" name="sets[]">
<input type="text" name="exercise[]"> <input type="text" name="sets[]">
<input type="text" name="exercise[]"> <input type="text" name="sets[]">
<input type="text" name="exercise[]"> <input type="text" name="sets[]">
That will keep an order as in code. Some browsers are not sending empty values, so it would be better to manually order them (so you would know, if there was no answer or whatever):
<input type="text" name="exercise[1]"> <input type="text" name="sets[1]">
<input type="text" name="exercise[2]"> <input type="text" name="sets[2]">
<input type="text" name="exercise[3]"> <input type="text" name="sets[3]">
<input type="text" name="exercise[4]"> <input type="text" name="sets[4]">
Now you can iterate through it in PHP like this:
<?php
foreach($_POST["exercise"] as $id => $exercise){
echo "EXERCISE $id: " . $exercise . ", SETS $id: " . $_POST["sets"][$id] . "<br />";
}
?>
Please note, that your SQL query is probably vulnerable to injection attacks!
Instead of raw query, you should use something like mysqli_real_escape_string() (or similar; depends what lib are you using to connect to database):
<?php
$sql = "insert into exercises (exercise, sets) values (" . mysqli_real_escape_string($exercise) . "," . mysqli_real_escape_string($_POST["sets"][$id]) .")";
?>

php multipage input value session variable print

please correct me here.
i have created multi page form where i want to pass data from each pages to final pages and then submit those on email. first one is apply.php, there are many input fields, but i have listed some of those, here when some enters passport number in passport field, i want this to be passed in everypage of the form and print this at couple of places on each page. here getting some issues when passing some of these fields.
this is first page ( apply.php )
<?php
// Start the session
session_start();
?>
<form name="search_form" method="post" onSubmit="return chk();" action="apply2.php">
<input name="passportno" id="passportno" type="text" maxlength="20" placeholder="Enter Passport No." size="43" >
<input name="birthdate" type="date" class="textBoxDashed" size="43" id="birthdate" datepicker="true" datepicker_min="01/01/1900" datepicker_max="21/11/2017" maxlength="10" datepicker_format="DD/MM/YYYY" isdatepicker="true" value="">
<input name="button1" type="submit" value="Continue">
this is apply2.php . here there is some issues, i am not able to find, as you can see below codes, i am able to print date of birth but not able to print passport no ( input from form1 ). Please correct where i am wrong here.
<?php
session_start();
$msg="";
////include("connect.php");
if (isset($_POST['button1']))
{
extract($_POST);
$code=strtolower($_POST['captcha_code']);
$sess=strtolower($_SESSION["code"]);
if ($sess==$code)
{
$appid=time().rand();
$result=mysqli_query($con,"select *from registration where email='$email'");
if (mysqli_fetch_row($result)>0)
{
?>
<script>
alert("This email is already exist");
</script>
<?php
}
else
{
$query="insert into registration values('$appid','$passportno','$birthdate','$email')";
if (mysqli_query($con,$query))
$msg="Data saved Successfully.Please note down the Temporary Application ID $appid";
else
echo "not inserted".mysqli_error($con);
if (!isset($_SESSION["appid"]))
{
$_SESSION["appid"]=$appid;
}
}
}
else
{
?>
<?php
}
}
?>
<form name="OnlineForm" method="post" onsubmit="return OnSubmitForm();" action="apply3.php">
<input name="applid" id="applid" value="<?php echo $_SESSION["appid"];?>">
<input type="hidden" name="birthdate" value="<?php echo $birthdate;?>"><b><?php echo $birthdate;?>
<input name="passportno" type="text" class="textBoxDashed" id="passportno" value="" size="43" maxlength="14" value="<?php echo $passportno;?>">
input name="sc" type="submit" class="btn btn-primary" id="continue" value="Save and Continue" onclick="document.pressed=this.name">
Don't use extract. Also do some checking to see if the data is set. As for not getting the the data try $_POST['passportno'] and if you want to pull the values and put them back into the input boxes simply use <?php echo isset($_POST['passportno'])?$_POST['passportno']:'' ?> to return nothing if it is not defined.
Also you need to do add some protection to your inputs.
You can add protection by using $passportno = mysqli_real_escape_string($con, $passportno);

Failing to send checkbox value to MySQL database?

I'm trying to store a Boolean value in my database based off a checkbox(Watched) in a form. If the checkbox isn't checked on form submit, it should send a 0; Otherwise a 1.
However, it sends a value of 0 no matter if it's checked or not. The Title Field and Genre field send perfectly, however the Watched conditional is not working correctly. I'm stumped and having a hard time wrapping my head around this, any thoughts?
<?php
require 'config/config.php';
require 'config/db.php';
// Check for Submit
if(isset($_POST['submit'])) {
// Get Form Data
$title = mysqli_real_escape_string($conn, $_POST['title']);
$genre = mysqli_real_escape_string($conn, $_POST['genre']);
if (isset($_POST['watched']) && ($_POST['watched'] == "value")) {
$watched = 1;
} else {
$watched = 0;
}
$query = "INSERT INTO movies(name, genre, watched) VALUES('$title', '$genre', '$watched')";
if (mysqli_query($conn, $query)) {
header('Location: '.ROOT_URL.'');
} else {
echo "ERROR: " . mysqli_error($conn);
}
}
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<div class="md-form">
<input type="text" id="form1" class="form-control" name="title">
<label for="form1" class="">Title</label>
</div>
<div class="md-form">
<input type="text" id="form1" class="form-control" name="genre">
<label for="form1" class="">Genre</label>
</div>
<div class="form-group">
<input type="checkbox" id="checkbox1" name="watched">
<label for="checkbox1">Have you watched it already?</label>
</div>
<input type="submit" name="submit" value="submit" class="btn btn-primary">
</form>
You never set an actual value for the checkbox in your form HTML, so the value of the checkbox will never exist which means your if statement will always return false.
Simply change this:
<input type="checkbox" id="checkbox1" name="watched">
to this:
<input type="checkbox" id="checkbox1" name="watched" value="value">

Categories