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

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

Related

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 print all even numbers between two input values typed by user. please write code hint only in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
How to print all even numbers between two input values typed by user. please write code hint only in php.
I can give you a hint. Lookup the modulo operator. With it you can do something like this:
<?php
if (($number % 2) === 1)
{
echo "$number is odd.";
}
if (($number % 2) === 0)
{
echo "$number is even." ;
}
?>
Also. Have a look at how you ask questions on stack overflow. Or they will prevent you from asking questions in the future if you don't adhere to those rules.
use this code, for user input i have created a form.
<html>
<body>
<form method="post">
<input type="number" name="num1">
<input type="number" name="num2">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
// while loop that will print number
while($num1 <= $num2){
// here is the condition to check the EVEN number
if($num1%2!==0){
echo $num1."<br>";
}
// increasing the loope counter by 1
$num1++;
}
}
?>
i hope now your problem has been solved..

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

Display the names of selected checkboxes and the values [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 am trying to display the name of all the selected check boxes and their values. At the moment it shows the values of all the selected check boxes but not the name of the selected check box. How would I do this?
The sections of code are below. The HTML form section has 9 identical checkboxes apart from the name and value.
<?php
echo $_POST['jan'];
echo $_POST['feb'];
echo $_POST['mar'];
echo $_POST['apr'];
echo $_POST['may'];
echo $_POST['jun'];
echo $_POST['jul'];
echo $_POST['aug'];
echo $_POST['sep'];
?>
<div class="checkbox">
<label>$15 :
<input name="jan" value="15" type="checkbox"> January
</label>
</div>
Any help would be great.
Checkbox parameters are only set if the box is checked. So test whether it's set:
if (isset($_POST['jan'])) {
echo 'jan: ' . $_POST['jan'];
}
You can put this into a loop:
$months = array('jan', 'feb', ...);
foreach ($months as $m) {
if (isset($_POST[$m])) {
echo $m . ': ' . $_POST[$m];
}
}

Dropdown variable script [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
I have the following code:
echo "<td><textarea wrap='soft' class=tarea3 name='inston[]'>".$r['inston']."</textarea></td>\n";
I'd like to use that inston variable to associate with a dropdown list. I have working code for the dropdown list:
<tr><td class="tdt">
<?php te('Hosting Server');?>:</td> <td title='Select server'>
<select validate='required:true' class='mandatory' name='manufacturerid'>
<option value=''>Select</option>
<?php
foreach ($agents5 as $a) {
$dbid=$a['id'];
$atype=$a['label']; $s="";
if (isset($manufacturerid) && $manufacturerid==$a['id']) $s=" SELECTED ";
echo "<option $s value='$dbid' title='$dbid'>$atype</option>\n";
}
echo "</select>\n";
?>
</td></tr>
and a sql query for the above mentioned dropdown script,
$sql="SELECT * FROM items where itemtypeid=32 OR itemtypeid=44 order by label";
$sth=db_execute($dbh,$sql);
while ($r=$sth->fetch(PDO::FETCH_ASSOC)) $agents5[$r['id']]=$r;
How can we use the code above to associate inston variable?
Your question isn't clear and from what i understood, is suppose the answer to be this
First of all , use an id for the select tag and textarea and use an onChange function like this:
<textarea wrap='soft' class=tarea3 name='inston[]' id='inston'>".$r['inston']."</textarea>
<select validate='required:true' class='mandatory' name='manufacturerid' id='manufacturerid' onChange='setInston()'>
and the use this javascript function :
function setInston() {
var x = document.getElementById("manufacturerid").value;
document.getElementById("inston").innerHTML= x+"
";
}

Categories