Save the whole PHP file to another one with $_POST variables [closed] - php

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 9 years ago.
Improve this question
For example, I have a simple form with POST target to PHP file:
<form action="language_foo.php" method="post">
<input type="text" name="fname" />
<input type="text" name="age" />
<input type="Submit" name="Submit">
</form>
In language_foo file, I have:
$lang = "$_lang['item.desc']" = $_POST['fname'];
How can I save the user input with exact the same structure, e.g:
$lang = "$_lang['item.desc']" = "Jane";
To another PHP file? I can do file_put_contents, but it will only put "Jane" to file.
Any suggestions?

Simply file_put_contents the below variable $variable_to_put_in_file
$variable_to_put_in_file = '$lang = "$_lang[\'item.desc\']" = "' . $_POST['fname'] . '";';

Are you looking for serialize() ? After filling your $_lang array, you can serialize the whole array to a string and dump it into a file by doing file_put_contents(..., serialize($_lang)), and load it back later with $_lang = unserialize(file_get_contents(...)).

Related

How to use php echo to display user input multiple times [closed]

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 8 months ago.
Improve this question
I need someone to help me with php guides on how i can echo every user input to the screen...
Example:
Html
<form action"bot.php" method="POST">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
Php Code
<?php
//request name from form
$username = $_REQUEST['username'];
//create function
function msg()
{
$username = $_POST['username'];
echo "$username";
echo "\r\n";
}
//echo input to screen
if (isset($_POST['submit']))
{
msg();
}
If I click submit button it will display my input text to the screen, that's fine, If I input another username click on submit button again to display under my previous output to screen...Instead PHP overide my previous output with new username submitted..
Will be glad if anyone can help
This example uses a session. You don't need anything external other than a working PHP installation. I've added comments in the code, but the basic flow is,
start a session
read the names from the session. If this is a new session, default the names to an empty array
read the submitted name
append the submitted name to the existing names
write the existing names to the session
<form action="bot.php" method="POST">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<?php
// Always start the session
session_start();
// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Read the existing names from the session. Default to an empty array if none are set
$existingNames = $_SESSION['names'] ?? [];
// Grab the submitted name
$name = $_POST['name'];
// Add it to the end of the list
$existingNames[] = $name;
// Overwrite the entire session's list
$_SESSION['names'] = $existingNames;
// Output something interesting
echo 'Welcome ' . $name;
echo '<br>';
echo 'There are ' . count($existingNames) . 'name(s).';
echo '<br>';
foreach ($existingNames as $storedName) {
echo $storedName;
echo '<br>';
}
}

Deleting mysql json data in PHP [closed]

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 12 months ago.
Improve this question
I am sharing my codes below with you. I can save new data as json in my database, but I couldn't figure out how to delete what I want from this data. How should I do?
<?php
include("matta/database.php");
?>
<html>
<meta charset="UTF-8">
<?php
$sorgum = mysql_query("SELECT * FROM birlikler WHERE id = 1", $vtbaglan);
$sorgu = mysql_fetch_array($sorgum);
$uyelerim = $sorgu['uyeler'];
$uyesim = json_decode($uyelerim, true);
foreach($uyesim as $bubirtest){
echo ''.$bubirtest['kullanici'].'<br>';
}
echo ''.$uyebilgileri.'
<form method="post">
<input type="text" name="kullaniciadi" placeholder="kullaniciadi"s>
<input type="text" name="rutbe" placeholder="rutbe">
<input type="text" name="kadi" placeholder="karakteradi">
<button type="submit" name="verigir">verigir</button>
</form>';
if(isset($_POST["verigir"])){
$kullaniciadi = $_POST["kullaniciadi"];
$rutbe = $_POST["rutbe"];
$karakteradi = $_POST["kadi"];
$arr = [
'kullanici' => ''.$kullaniciadi.'',
'rutbe' => ''.$rutbe.'',
'karakteradi' => ''.$karakteradi.''
];
$uyesim[] = $arr;
$arrim = json_encode($uyesim);
//echo '<br>'.$uyeler.','.$arrim.'';
$veriekle = mysql_query("UPDATE birlikler SET uyeler = '$arrim' WHERE id = 1", $vtbaglan);
}
?>
my previous question: PHP MySQL Json data saving
Edit:
I tried the code stated by #gguney but could not reach the result. I leave the "uyeler" column in my database below so that I can clearly explain the way I want.
[{"kullanici":"matta","rutbe":"20","karakteradi":"Vitality_Test"},{"kullanici":"Linuxy","rutbe":"19","karakteradi":"Linuxy_Test"}]
To give an example of the operation I want to do; I want to delete the user whose "kullanici" value is "matta" from this line. In other words, I want the new data that I want to reach as a result of my operation to be as follows;
[{"kullanici":"Linuxy","rutbe":"19","karakteradi":"Linuxy_Test"}]
The PHP version I'm using is a bit old. I'm aware of this, but I have to use this version for my current job. I would be very grateful if you could help.
You can update that json field. Or like any other array field. Just use unset.
For example:
unset($arr['rutbe']);
and use again your update mysql query to delete that field.
Or you can use MySQL JSON_REMOVE function
UPDATE birlikler
SET birlikler.uyeler = JSON_REMOVE(birlikler.uyeler, 'rutbe');

How to convert data from database into a link? [closed]

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 7 years ago.
Improve this question
I have a list of courses that looks like this:
course
SE1111
SE2222
SE3333
I need to show it in my page and then it should redirect to a page saving the value (name of course) for future purposes.
This is what I have done so far:
<div id="join_courses" class="">
<form action="" method="post">
<label>Join a course:<br></label>
<input placeholder="select a course" list="courses" name="courses">
<datalist id="courses">
<option value="
<?php
include('student_register.php');//include php code that gets the tables (courses)
while($table = mysqli_fetch_array($result)) {
echo $student_courses[0];
}
?>
This gives me the input list form but it is showing me all the "courses" in one same field. (NOT IN A LIST).
any suggestions on how to approach this?
try with a proper anchor something like this
while($table = mysqli_fetch_array($result)) {
echo '<a href="yourPath/'. $student_courses[0] . '">' .
$student_courses[0] . '</a>';
}

Array value from a forum input element [closed]

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 8 years ago.
Improve this question
I was under the impression that what ever is inserted into a <input type="text" name="something"> would be recieved as a string in PHP with $_POST['something'].
But now im running a tools to test my website and somehow $_POST['something'] can be an array.
How is that possible ?
If in your form you have inputs like <input name="something[]" ... /> you can have many of them or <select name="something[]" multiple ... />, etc.
$_POST['something'] would be an array.
It's common 'hack'. You should always verify that variables you get are in format you expect.
Example with $_GET:
http://127.0.0.1/hack_test.php?a[]=3&a[]=5?a[]=3&a[]=5
Example with $_GET and 'keys' of array:
http://127.0.0.1/hack_test.php?a[3]=3&a[hack_name]=5
If you put:
<?php
var_dump($_GET);
In hack_test.php it will show:
array(1) {
["a"]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "5"
}
}
Variable $_GET['a'] is array with 2 elements!
It's like that in PHP, because website forms sometimes require that feature.
Example:
<form ..>
<input type="checkbox" name="multicheckbox[]" value="chicken" />
<input type="checkbox" name="multicheckbox[]" value="apple" />
<input type="checkbox" name="multicheckbox[]" value="sugar" />
</form>
I called it 'hack', because:
If you use other PHP feature 'string is array of bytes' then someone can send you modified data to script, ex. $x = "abc"; $a = $x[0]; echo $a; -> 'a'
If you put data from input [form] in SQL query without verification, hacker can use it to make 'invalid query format' and in some cases, it let him get some information from database!

PHP Function name needs to be a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to make an incredibly basic php form that outputs some text a user puts in to a database. Looked at a couple other questions like this to make sure and from what I can tell no one else asked about this. Here's the create.php that takes input from the html.
<?php
include 'connection.php';
$firstname= $_POST('inputName');
$lastname= $_POST('inputName2');
if($_POST ['SUBMIT']) { echo "please fill out the form";
header('location: ../index.html');
} else {
mysql_query("INSERT INTO requestdata ('firstname', 'lastname')
VALUES ('$firstname', '$lastname')") or die(mysql_error());
echo "User has been added"; header ('Location: ../index.html');
}
?>
Here's the html code for the form:
<form action = "php/create.php" method = "POST">
First Name <input type ="text" name='inputName' value=""/>
Last Name <input type="text" name='inputName2' value=""/>
<br />
<input type="submit" name = "button">
</form>`
and the error I'm getting is:
"Fatal error: Function name must be a string in
mywebsite.com/php/create.php on line 8
I think your $_POST variables are wrong.
Please try $_POST['inputName'] instead of $_POST('inputName') and $_POST['inputName2'] instead of $_POST('inputName2')

Categories