Deleting mysql json data in PHP [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 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');

Related

PHP table creation with name and score [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 4 years ago.
Improve this question
I am having some serious trouble for some reason with creating a basic PhpMyAdmin database and making a page with a scoreboard. There will be a game that you play, and when you're done you can input a score and your name using basic form elements. When you press submit the page will reload and you will be able to see the top ten scores, ranked by highest first.
My issue is that I have no idea where to start with this. I have just started Php and don't wish for anything crazy. I have the ~/php/db_connect.php set up correctly already; I just need to make the function work.
How do you recommend I go through with this? Example code is extremely helpful.
I know the first response is "what have you tried?" and I haven't tried much.
This is what I have right now:
// define variables and set to $name = $myArray[0];
$babyinfo = fgets($myfile);
$myfile = scoreboard-dk;
$myArray = explode(',', );
$score = $myArray[1];
$name = $myArray[2];
$insertStmt = "INSERT INTO scoreboard-dk ('score','name') VALUES ('$score','$name')";
// Inserting Babynames into database
$db->query($insertStmt);
?>
<form action=" $db;?>" method="post">
Name: <input type="text" name="name" value=" echo $name;?>" required><br>
Score: <input type="text" name="score-dk" value=" echo $score;?>" required><br>
<input class="btn btn-primary" type="submit">
</form>
<tr> <th scope=row> echo $i;?></th> <td> echo $score;?></td> <td> echo $name;?></td> <td> echo $votes;?></td> </tr>
Thanks in advance.
Ok. First some mistakes you made:
$myfile = scoreboard-dk; isn't working. This way it would be a constant. You need the "$" or quotation marks if it should be a string.
$myArray = explode(',', ); I don't know what you want to do? The second argument is missing. This statement won't work. Second argument has to be a string.
You have to properly escape the query before executing the statement.
You can do this by replacing the following line before building the string:
$score = $db->real_escape_string($myArray[1]);
$name = $db->real_escape_string($myArray[2]);
Furthermore are you sure you use the correct indices for the array access? Counting starts with 0, not with 1.
You can't use PHP code without the opening tags. I thought that you cut that away at the start of the file. You always have to open PHP code blocks with
Perhaps you should search for example code elsewhere. I think stack is more for specific questions. But the code actually shows that you lack of some basic knowledge ... no offense.
The reason people can't help you is that your question is way too broad and everyone will have a different approach about how to implement it.
That being said, here is the pseudo code I would use to implement this. It can be done in a single file. Good luck!
File: score_keeper.php
<?php
error_msg = array
if (form submitted)
$name = name from form
$score = score from form
// Do validation to ensure name and score is as expected.
if name is empty
error_msg[] = 'Name cannot be empty'
if score is not numeric
error_msg[] = 'Score must be numeric'
if empty(error_msg)
// INSERT
// Make sure you use parameterized queries
SQL = INSERT into table (name, score) VALUE (?, ?)
end-if
end-if
// READ top 10
SQL = SELECT name, score FROM table WHERE ...
if !empty(error_msg)
show error_msg
?>
<form method="post">
<input name="name">
<input name="score">
</form>
HTML table
<?php
// output top 10 results

How to name an array after content from a string [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 5 years ago.
Improve this question
I've a form tag which sends the name for an array to a function but how can I name my array after this? Ok, code says more than thousand words:
<?php
function dynamic($id, $name, ... ){
...
echo "<select id='$id' name='$name' size='...' multiple>";
...
echo "<select id='$id' name='$name'>";
...
}
?>
<form ... method="post">
<p>
<?php
echo dynamic("dynamic1", "choice1", ...);
?>
</p>
<p>
<?php
echo dynamic("dynamic2", "choice2", ...);
?>
</p>
<p>
<?php
echo dynamic("dynamic3", "choice3", ...);
?>
</p>
<input type="submit" value="send"/>
</form>
I want to create a list where you can select multiple items but for this $name needs ot be an array. The array should named like the second variable. In one case it should be named choice1 in another choice2
Like how do I get from $name = "choice1"; to choice1[]
#edit Added a new line in function to show my problem. somtimes $name needs to be and array and sometimes not
Any ideas?
You are looking to use dynamic variable names, which is possible in PHP, but you need to be careful with this. Production code using this can be difficult to maintain and throw errors quite easily.
Anyway, lets say you have a value in the form $_POST that you want to use as a variable name. You would do so like this.
$id = "gettheidsomewhere";
${$id}[] = "whatever";
Like i said, use this carefully. Dynamic variable names are dangerous and very hard to debug when things break.
If you do not know the value used for $id, then you will need to loop through your post variables and assign them accordingly. I would assume you want to add some extra logic, but here is a basic example.
Using a key value loop you can obtain the name of the post variable, stored as $key and the value. So for $_POST["something"] = "test", when this line is looped over, $key will be "something" and $value will be "test".
foreach ($_POST as $key => $value)
{
${$key}[] = $value;
}

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>';
}

how to post SQL variable through session and get it in next page [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 want to post $dataHeadArr value through session and get it in next page.
<?php
if(isset($_POST["search"]))
{ $storename=$_POST["StoreName"];
$dataHeadArr=$db->query("SELECT *FROM `opening_stk`");?>
On for the page you have posted put
<?php
if(isset($_POST["search"])){
session_start();
$storename=$_POST["StoreName"];
$dataHeadArr=$db->query("SELECT * FROM `opening_stk`");
while($row=mysql_fetch_assoc($dataHeadArr)){
$allRows[]=$row;
}
$_SESSION['dataHeadArr']=$allRows;
}
?>
For the page that you want to retreive the variable put
<?php
session_start();
$dataHeadArr=$_SESSION['dataHeadArr'];
//This mysql result been parsed already, so you should be
//able to access values when you loop through it from here
for($i=0;$i<count($dataHeadArr); $i++){
echo "Printing row ".$i."</br>";
foreach($dataHeadArr[$i] as $key=>$item){
//Prints out all value for the row
echo $item[$key]."</br>";
}
echo "</br>";
}
?>
You may just set it in the POST or SESSION array and then retrieve it on next page. For ex-
$_POST['dataHeadArr']=$dataHeadArr;
To store...
$_SESSION['dataHeadArr'] = 'Whatever';
Then to retrieve simply..
if(isset($_SESSION['dataHeadArr'])){
echo $_SESSION['dataHeadArr'];
}
Make sure to call session_start(); at the top of every page.

Save the whole PHP file to another one with $_POST variables [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 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(...)).

Categories