I am tring to do a script for update user info from database. For this I have used textboxex to fetch userinfo from db for any userID entered and to edit the same to update. My code is
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
ENTER USER ID TO UPDATE DATA:<br/>
<input type="text" name="userid" value="<?php if(isset($_POST['userid']))echo $_POST['userid'];?>">
<input type="submit" name="submit" value="Find Details"/>
</form>
<?php
if(isset($_POST["submit"])){
$userID=$_POST['userid'];
if(!$userID){
echo "<script>alert('Please fill all the details.')</script>";
exit(0);
}
global $conn; // Create connection
$conn = mysqli_connect('localhost', 'root','root','test');// Check connection
if (!$conn){
die("Connection failed: " . mysqli_connect_error());
}
$query="SELECT * FROM user WHERE ID='$userID'";
$result=$conn->query($query);
$row=$result->fetch_array();
if($row!=null){
$id=$row['ID'];
$name=$row['name'];
$username=$row['username'];
$type=$row['type'];
//$action=echo "<?php echo htmlspecialchars($_SERVER['PHP_SELF']);";
echo "<div id='item'>";
echo "<form action='' method='post'>";
echo"Existing Data is as:<br><br>";
echo "User ID:<input type='text' name='idval' value='$id' wrap='physical'><br><br>";
echo "Name:<input type='text' name='nameval' value='$name' wrap='physical'><br><br>";
echo "User Name:<input type='text' name='userval' value='$username' wrap='physical'><br><br>";
echo "User Type:<input type='text' name='typeval' value='$type' wrap='physical'><br><br>";
// echo "<input type='submit' name='update' value='UPDATE'>";
echo "</form>";
echo "</div>";
}?>
This is working fine and as expected. Now what I want is that as soon as when I hit that update button values changed/edited/updated values should be get updated and then displayed back to user. I have my update query and it is working fine on DB when tested. How can implement the same in php?
Redirect again to the URL you are using to get reflected changes, OR put your update query before select query so you can get updated value in select query.
Related
This is my HTML form:
<form action='buddy_update.php'>
<input type='text' name='buddy1' required='' placeholder='Player ID / E-mail'>
<input type='hidden' name='id' value=''%".$id."%''>
<input type='submit' value='Request Buddy #1!'>
</form>
This is my PHP on buddy_update
<?php
include 'credentials.php';
$id=$_GET['id'];
$buddy1=$_GET['buddy1'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$id=$_GET['id'];
$buddy1=$_GET['buddy1'];
$sql = "UPDATE buddy SET buddy1_id = '".$buddy1."' WHERE main_player = '".$id."'";
if (mysqli_query($conn, $sql)) {
echo $id;
echo $buddy1;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
$buddy1 comes through absolutely fine but $id doesn't.
For what it's worth I have also changed the value='' to a plain text input on the HTML form and it still doesn't work. The output in the PHP form is still blank.
EDIT
No idea why I've defined variables twice! Took the one out but still same problem
There are few things to consider:
Your html form should be in .php file
Your form needs to have method='get', <form action='buddy_update.php' method='GET'>
<input type='hidden' name='id' value="<?php if(isset($_GET['id'])){echo $_GET['id'];}?>"> in case you are getting this GET variable`
Your code can't access the $id value.
Why don't you try this
<input type='hidden' name='id' value='<?php echo $id ?>'>
assuming you define $id above this statement
You have to ensure that the input (hidden) always has a value.
Else it will throw invalid index error.
$id is not getting any value here:
It seems you have defined form in php.Since $id states that variable is defined in php.
so you can try to replace your code with some thing like below code
echo "<form action='buddy_update.php'>
<input type='text' name='buddy1' required='' placeholder='Player ID / E-mail'>
<input type='hidden' name='id' value='".$id."'>
<input type='submit' value='Request Buddy #1!'>
</form>";
Hi this is my first post so please excuse any errors
Background if this helps
i've created several pages for a website
submit form - puts data in to database - all working ok
summary page - pulls through elements of database - all working ok
edit page - this is where my problem is
my problem
when creating a edit page it does not pull through the updated variable from the form and just updates the database with a empty field
so here is the code
edit.php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$var_value = $_GET['id'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<form action="/update.php" method="post">
<label>Property Title
<input type="text" name="title" value="<?php echo $row[title]?>" />
<form method='get' action='update.php'>
<input type='hidden' name='id' value= "<?php echo $row[id]?>" >
<input type='submit' class='button radius' value='update' >
</form>
</label>
so that should display whats currently in the database then when a user changes it they click update and it should up date in the database
here is the update.php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$title =$_POST['title'];
$var_value = $_GET['id'];
$result = $conn->query($sql);
$sql = "UPDATE aparthousesalerent SET title='$title' WHERE id = '$var_value' ";
echo $var_value;
echo $title;
if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
i added in the extra line of echo $var_value and echo $title; to check if that worked and it did but it still doesn't pass it through to the database
thanks for looking
<form action="/update.php" method="post">
<label>Property Title
<input type="text" name="title" value="<?php echo $row[title]?>" />
<form method='get' action='update.php'>
<input type='hidden' name='id' value= "<?php echo $row[id]?>" >
<input type='submit' class='button radius' value='update' >
</form>
as you can see there are nested form
So, remove the form and make method post because you are using $_POST in php file.
<form action="/update.php" method="post">
<label>Property Title
<input type="text" name="title" value="<?php echo $row[title]?>" />
<input type='hidden' name='id' value= "<?php echo $row[id]?>" >
<input type='submit' class='button radius' value='update' >
</form>
and use one either GET or POST
$title =$_POST['title'];
$var_value = $_POST['id'];
This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body> <table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
The user must then be able to click on submit and have those values updated in the mysql database.
Right now when I click the submit button, it posts to edit form.php which has this:
<?php
//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}
?>
But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.
Where is my problem? :) Thx
Name your checkbox like this:
<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">
And as Amal already said update your code to PDO or MySQLi
you can do this with a tag :-
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
on your php code you get :-
$all_checkes_checkbox = $_POST['checkbox'];
here is your all checked checkbox:-
and this array also bale key and value
I need some help on this problem I have, 2 PHP files, one is a form the other the PHP code to insert the data to the database. I am using a userID in a session to keep the data entered for the same user, my problem is after I submit the data to the database my page have to redirect to another page in the same session with the same userID but display the entered data in the table, but every time I submit it goes to the right page but without the userID, (437465375.php?userID=) if I put the user id in on the end (437465375.php?userID=33) it goes to the right page displaying the data enter in the table.
Here is the code for the form:
<div class="posttext">
<p><strong>PRIMARY INFORMATION TO BE FURNISHED BY A TAXPAYER AS REQUIRED BY SARS.</strong></p>
<?php
if (!isset($_SESSION['userID'])) {
echo " <form method='post' id='contatti' action='savetrip1.php?userID='" . $_GET['userID']."'>";
?>
<div id="contactform">
<?php
echo "<p><h2><strong>Kilometre Recording</strong></h2></p>";
echo "<div class='commentfield'>";
echo "<label for='author'>Work Km Travelled ( Total Km only not Speedomiter meter reading): </label> <input type='text' value='".$row['travelledKm']."' name='travelledKm' id='TravelledKm' />";
echo "</div>";
echo "<div class='commentfield'>";
echo "<label for='author'>Date: YYYY-MM-DD </label><input type='text' value='".$row['LoggedDate']."' name='LoggedDate' id='LoggedDate' />";
echo "</div>";
echo "<p><h2><strong>Business Travel Details</strong></h2></p>";
echo "<div class='commentfield'>";
echo "<label for='author'>From Where did you Travel: </label> <input type='text' value='".$row['fromk']."' name='fromk' id='fromk' />";
echo "</div>";
echo "<div class='commentfield'>";
echo "<label for='author'>To Where did you Travel: </label> <input type='text' value='".$row['tok']."' name='tok' id='tok' />";
echo "</div>";
echo "<div class='commentfield'>";
echo "<label for='author'>Reason: </label> <input type='text' value='".$row['Reason']."' name='Reason' id='Reason' />";
echo "</div>";
echo "<div class='commentfield'>";
echo "<label for='author'> Fuel & Oil Costs (R): </label> <input type='text' value='".$row['Fuel']."' name='Fuel' id='Fuel' />";
echo "</div>";
echo "<div class='commentfield'>";
echo "<label for='author'>Repairs & Maintenance Costs (R): </label> <input type='text' value='".$row['Repairs']."' name='Repairs' id='Repairs' />";
echo "</div>";
echo "<div class='contactbutton'>";
echo "<input type='submit' class='contact-button' name='submit' id='invia'value='Save Trip' />";
echo "<input type='reset' class='contact-button' name='clear' value='Clear Input' />";
echo "</div>";
echo "</form>";
}
?>
</div></div>
</div>
And here is the php code for submiting the data to the mysql database:
<?php
session_start('userID');
$host = "localhost";
$db = "database";
$user = "user";
$pass = "pass";
$userID=$_SESSION['ID'];
$travelledKm=$_POST['travelledKm'];
$LoggedDate=$_POST['LoggedDate'];
$fromk=$_POST['fromk'];
$tok=$_POST['tok'];
$Reason=$_POST['Reason'];
$Fuel=$_POST['Fuel'];
$Repairs=$_POST['Repairs'];
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db) or die(mysql_error());
if (isset($_POST['submit']) == true) {
$query = "INSERT INTO `webimckr_lockbook`.`trip_log`(`userID`,`LoggedDate`,`travelledKm`,`fromk`,`tok`,`Reason`,`Fuel`,`Repairs`)VALUES ('$userID','$LoggedDate','$travelledKm','$fromk','$tok','$Reason','$Fuel','$Repairs')";
$result=mysql_query($query) or die(mysql_error());
if($result){
header("Location: 437465375.php?userID=".$_GET['userID']."");
}
else {
echo "ERROR";
}
}
?>
<?php
// close connection
mysql_close();
?>
I am very new to PHP and Mysql and learning on my own, please assist me with this so that I can understand it and for future use.
session_start() was generating the phpsessionId which was unique for each browser, so u can check with your form post only
each refresh form will post your data what ever it is, so whether you can check post is empty or not
This line does not look right to me:
$userID=$_SESSION['ID'];
you are using userID at the top then calling ID?? Use same name
use $_POST['userID'] instead of $_GET['userID']
PHP problem here, I've made a login/logout kind code, with a insert and delete function that stand by login and logout.
So the problem is that after I insert the the text I simply cannot delete it, cause the delete button is like a simple
turn back botton, and doesn't make his work, nothing
in the if(isset($_POST['delete'])) condition seems to work.
May the problem be that I'm using two void action
that refer to the same page? cause the first button
work and the second not.
Anyone can understand why?
<html>
<header></header>
<body>
<!-- START PHP -->
<?php
//If not submit i put the submit form
if(!isset($_POST['send'])){
echo "<form name='send' action='' method='POST'>
<input type='text' name='text' value=''/>
<input type='submit' name='send' value='send' />
</form>";
}<!-- IF END -->
//If submit was set I insert $text into the db and I render
//the delete button
else {
$conn= mysql_connect('localhost','root','');
mysql_select_db('db_try',$conn ) or die(mysql_error());
$dato=$_POST['dato'];
mysql_query(" INSERT INTO test (value) VALUES ('$text') ") or die(mysql_error());
echo "Operation complete";
//Now i render the delete submit button...
echo "<form name='delete' action='' method='POST'>
<input type='submit' name='delete' value='delete' />
</form>";
//...and if i push it NOTHING, like it's only
//a return to the first form button
if(isset($_POST['delete'])){
mysql_query(" DELETE FROM test WHERE value='$text' ") or die(mysql_error());
echo "<br>Text'".$text."' deleted";
}
}<!-- ELSE END-->
?><!-- END PHP -->
</body>
</html>
There is a logic problem with your code. When the delete button is clicked, the script runs again. The first condition you have - if(!isset($_POST['send'])) will now pass, since the send button is no longer set, and so it goes into the if statement and never runs your delete code.
Your script also appears to be vulnerable to SQL injection.
Here is the right way to do this, it is a quick tip, you need to work a little more on mysql insert security etc.
<html>
<header>
<body>
<?php
$conn= mysql_connect('localhost','root','');
mysql_select_db('db_try',$conn ) or die(mysql_error());
if(isset($_POST['send'])){
$text = $_REQUEST['text'];
mysql_query(" INSERT INTO test (value) VALUES ('$text') ") or die(mysql_error());
$answer = "Operation complete";
$form = "<form name='delete' action='' method='POST'>
<input type='submit' name='delete' value='delete' />
</form>";
}
else if(isset($_POST['delete'])){
mysql_query(" DELETE FROM test WHERE value='$text' ") or die(mysql_error());
$answer = "Text'".$text."' deleted";
}
else {
$form = "<form name='send' action='' method='POST'>
<input type='text' name='text' value=''/>
<input type='submit' name='send' value='send' />
</form>";
}
print "<h1>" . $answer . "</h1>";
print $form;
?>
</body>
</header>
</html>
I think it may also work...
if (!isset($_POST['submit']) || isa($_POST['submit'] != 'login'))