Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to look for a way to update a column.
Like here:
Database: temptestsite
Table: firstsection
then there is username, heading etc (not a log in type situation).
Say user sends in the username and I want them to be able to change it. How does one UPDATE this specific column. Anything I have tried so far from w3schools inserts a new column. Could someone please make an example of how to do this with PHP?
Like let there be a text box and then a button or a hyperlink next to it saying "update" or "change", whatever user types in that text box, gets updated in the "usernames" column in the database.
Your general process will be:
Check if post data is available
If it is, execute a mysql update call
Build the form
Here is some skeleton code:
<?php
$link = mysqli_connect("myhost","myuser","mypassw","mybd");
$id = $_GET['id'];
if (!empty($_POST)) {
$link->query("UPDATE users SET username = '".$link->real_escape_string($_POST['username'])."' WHERE id = '".$link->real_escape_string($id)."'");
}
$user = mysqli_fetch_assoc($link->query("SELECT * FROM users WHERE id = '".$link->real_escape_string($id)."'"));
?>
<form method="POST">
<input type="text" name="username" value="<?php echo $user['username'] ?>"/>
<input type="submit" value="Update"/>
</form>
There is two update method in rest system,
One of them is PUT , other one is PATCH,
Most frameworks make that request via hidden _method attr, like in laravel
<form method="POST">
<input type="hidden" name="_method" value="PUT"/>
<input type="text" name="column"/>
<input type="submit" value="Update"/>
</form>
PUT is using for changing entire column,
PATCH is just patching as you can understand,
First understand and manage what you really need, then watch & read about them.
As an answer maxhud is explained code version of it.
But it is not the good way to make it with post request.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I want to write a program that when a user click on a link, it will open the modem page (with specific ip address). In the login page I want to automatically fill the login form with default values and press Submit button automatically, so the user doesn't need to enter the Username and password. Any help is appreciated.
If the form you are trying to fill out has a CSRF(Cross site request forgery) then you will not be able to achieve this, however if it's not protected from CSRF, then you can simple submit a POST request to that URL. First, you need to gather some data.
Get the action destination from the login form using inspect element (sometimes you won't find an obvious tag, then find the action triggered by clicking the login button)
Gather the field names being sent to that controller (input name='{name_here}')
Create an HTML page that contains an html form that points to the same controller, with the same fields, populated by your PHP variables
Submit the form using javascript
Here's an example of me sending a "username" and "Password" to "form.cgi" which is the destination of my modems login form
<form action="192.168.100.1/form.cgi" method="post" hidden>
<input type="hidden" name="username" value="<?php echo $username;?>" />
<input type="hidden" name="password" value="<?php echo $password;?>" />
<input type="submit" value="Login" />
</form>
<script>
$(document).ready(function() {
$('form').submit();
});
</script>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
In my web application, I am trying to add a "Edit button" option to enable a user to change his information which he has already submitted. "Edit button" trigger a Modal with edit form which is pre-filled with values from Mysql database (provided by user at the time of registration).
But complete value of variables is not coming in input fields.
For example: Originally user filled his name as Asheesh Kumar which is saved in the database but edit form input field shows only first word of the name "Asheesh".
HTML code below:
<div class="col-md-4 ">
<input type="text" class="form-control" id="name" name="name" value=<?php echo $Name; ?>>
</div>
What I am getting in Input field:
screenshot of output
its happening for all Input fields in Edit form.
Well.. hard to tell without any code. But, I would start checking the following:
The variable is really coming complete from the database?
The html input does have a maxlength attribute?
If you're using javascript to set the input value, are you escaping the especial characters to avoid any javascript error?
Update
After the update in question (that was missing the code), I could notice that the problem is because you're not opening and closing the quotes of the value atribute of your input field.
You should change this:
<input ... value=<?php echo $Name; ?>>
To this: (note the closing " of the value attribute)
<input ... value="<?php echo $Name; ?>">
Only two possible error you have made.
Most probably your MySQL Type Length
As for the HTML Input Field, instead of using input use Text Area when you call out value from database.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to make a form that after i click submit,it will generate .sql file to insert the value to MySQL database. so I can transfer the value to other server real time.
How to generate SQL file that contain a script to insert value from input form?
we have a lot of outlet, usually we use web app from browser to input database, but there is a problem when the connection is down, so we tried to make the web app running from outlet server and transfer all the input to HO ftp server.
I already create batch file to transfer the file to ftp and will use cronjob to run it automatically, what left is I want to generate SQL file to insert the input data, so I can import the SQL to HO database from ftp server.
Here is the example:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
It will show name, email, text box and submit button. I want to generate SQL file that insert name and email input when I click submit button. not generate SQL file from MySQL.
Honestly I think this is a bad solution for security reason. FTP is a breakable protocol, but anyway: you can do this by calling the PHP method:
file_put_contents();
So after compose the SQL string to store into the file, you can do something like this:
$dir = 'folderPATH';
$dataToStore = "INSERT INTO table_name (name, email, ...) VALUES ('$name', '$email');"
file_put_contents($dir, $dataToStore);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am working on student database. Each Applicant can apply for upto three programs. I want to include add / remove program option on my php form without using javascript. Is it possible to do it wihout javascript?
please help!!!
You can do simple form to add info to Database with Submit Button that's for adding . For Deleting you can make Delete page that GET user id that you want to delete .
PHP only executes at runtime, ie when the page first loads. To do this, you'd need to reload the page every time you wanted a change to the page. So what I see is you have the first page they land on offer a form for the user to choose which of your three programs they're interested in, when the form submits, the page reloads and the server - queuing off the user's selection - loads and displays the appropriate program form.
If you want to do it without reloading the page, it is not possible without JavaScript as PHP cannot execute without talking to the server.
Here's an example of a form that at first displays a question to the user, posts to itself, then conditionally displays a field based on the user's input:
<?php if (!isset($_POST['program'])) : ?>
<form method="POST" action="">
Should we show the program field?<br>
<input type="radio" value="y" name="program" id="program_y"><label for="program_y">Yes</label><br>
<input type="radio" value="n" name="program" id="program_n"><label for="program_n">No</label><br>
<input type="submit">
</form>
<?php else : ?>
<form method="POST" action="form_submit_url_here">
<label for="name">Name:</label>
<input name="name" type="text" id="name"><br>
<?php if ($_POST['program'] === 'y') : ?>
<label for="program_name">Name of your program:</label>
<input type="text" name="program_name" id="program_name"><br>
<?php endif; ?>
<input type="submit">
</form>
<?php endif; ?>
Short answer: you can't.
PHP requires information to be processed either by that page or by another, and therefore data needs to be sent. Javascript (using AJAX) can be used to send that data from the page without a page refresh or relocation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am getting confused as I read various posts about the typical task I am trying to perform, no doubt countless others have done it before. So at this point in 2013 what is the best practice solution to achieve the following:
I have an html page which asks the user for a number of values, x1, x2, .....xn.
There may be 10 to 20 such inputs. As such I don't want them to be lost or to have to be re-inputted again for whatever reason.
perform calculations based on user inputs then output the results (eg. y1, y2, y3..y5).
As suggested by some of the posts here, I tried "echo" (for PHP), that works fine. However, it shows the results in a new page so the user input is no longer visible.
I want the user to see both - their input and the resultant. That way they can print if they want to and see all info on one page.
I prefer to use "basic" technologies, ie. technologies that most users can use, that they don't have to click OK on some warning, change some setting on their browser, accept some thing or other etc..
Thanks in advance!
Create inputs with html.
Choose between reloading page or AJAX
If you choose reloading then use
code:
<form action="nextfile.php" method="POST">
<input type="text" value="" name="y1" />
<input type="text" value="" name="y2" />
<input type="submit" value="calc me" name="submit" />
</form>
Then in nextfile.php you need get values with $_POST and if you want them to be saved use $_SESSION
For example
<?php
session_start();
if(isset($_POST['y1']) && isset($_POST['y2']))
{
$_SESSION['res'] = (int)$_POST['y1'] * (int)$_POST['y2'];
}
The code above will perform calculation on two inputs with name y1 and y2 and save them in session.
If you want AJAX then you need to visit this page and see examples
You should think of JavaScript solution because it will fit your needs and no server code is needed.
The simplest way is to submit the form to the same page and repopulate the input fields:
// calc.php
<?php
if (isset($_POST['foo'])) {
echo 'Result: ', $_POST['foo'] + 1;
}
?>
<form action="calc.php" ...>
<input name="foo" value="<?php if (isset($_POST['foo'])) echo htmlspecialchars($_POST['foo']); ?>">
...
</form>
The more modern version would be to submit the calculations via AJAX and populate the result via Javascript without reloading the page.