POST a hidden input + multiple options PHP - php

Hi there I'm quite new to PHP
I have this problem:
I would like to POST a multiple choice + a hidden field from a form:
<?php
if (isset($_SESSION['nickname']))
{
$result = mysql_query("SELECT * FROM users");
$teamsCount = ceil(mysql_num_rows($result)/2);
for ($i=1; $i<=$teamsCount; $i++)
{
// TEST: echo $i . " TeamsCount er: " . $teamsCount. "<br>";
?>
Team <? echo $i; ?>
<form name="addTeam" action="buildTeams.php" method="POST">
<input type="hidden" name="hiddenField" value="<?php $i; ?>" />
<select name="teams[]" multiple="multiple" size="<?php echo mysql_num_rows($result); ?>">
<?php
$query = mysql_query("SELECT * FROM users");
while ($row=mysql_fetch_array($query))
{
$id=$row["ID"];
$nick=$row["Nick"];
?>
<option value="<?php echo $id; ?>"><?php echo ucfirst($nick); ?></option>
<?php
}
?>
</select>
<input type="submit" value="Make them teams!!" />
</form>
<?php
}
}
?>

I think you have an error in this line:
<input type="hidden" name="hiddenField" value="<?php $i ?>" />
It should be
<input type="hidden" name="hiddenField" value="<?php echo $i ?>" />
Edit:
Put the team id in the select name. Example:
<select name="teams[<?=$i?>][]">
And in PHP do:
foreach ($_POST['teams'] as $team_id => $choices)

I think you should check $_POST['hiddenField'] to obtain hidden value

Related

Old values not appearing in text field when called

I'm trying to call the old values to be edited. What part am I wrong at?
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
if (mysqli_num_rows($record) == 1 ) {
$n = mysqli_fetch_array($record);
$BookNo = $n['BookNo'];
$ISBN = $n['ISBN'];
$title = $n['title'];
$author = $n['author'];
$publisher = $n['publisher'];
$status = $n['status'];
$cost = $n['cost'];
}
}
?>
<a href="viewBook.php?edit=<?php echo $row['BookNo']; ?>" class="edit_btn" >Edit</a>
</td>
<?php
if (isset($_GET['edit'])) { ?>
<form method="post" action = "viewBook.php">
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
<input type="text" name="ISBN" value="<?php echo $ISBN; ?>">
<input type="text" name="title" value="<?php echo $title; ?>">
<input type="text" name="author" value="<?php echo $author; ?>">
<input type="text" name="publisher" value="<?php echo $publisher; ?>">
<input type="text" name="status" value="<?php echo $status; ?>">
<input type="text" name="cost" value="<?php echo $cost; ?>">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Save</button>
<?php endif ?>
<?php } ?>
</form>
So far, what it does is, when the user clicks the edit button, it just shows 6 text fields. I thought by doing what I did, it was supposed to show the details already filled in the textbox.
When you do
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
$BookNo is not defined.
maybe you wanted to do something like this:
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$id'");
<form method="post" action = "viewBook.php">
your form method is "post" but you are checking $_GET You must check $_POST
if (isset($_GET['edit']))
you are passing value in $id And using $BookNo which not define.
only 6 input field will be show because first one is using hidden property.
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
when you click on submit button data will be receive by $_POST

Getting radio button value and storing in php

this is radio button code
now what if radio button does not have a constant name how would i store the data in database because to store the data in database we will need a name of form attribute
$sql1="select * from questions where email='". $_SESSION['email'] ."'";
$row=mysqli_query($conn,$sql1);
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer4'];?>
if we know the name of radio button we can access it using
<input type="radio" value="<?php echo $result['answer4'];?>" name="name"><?php echo $result['answer4'];?>
$name=$_POST['name'];
but in the above code name of radio button is not fixed.questions is the table which consists of qid and questions with multiple options that is answer1,answer2 etc
i want to store the option select by user into database.for which i need to know the name of radio button
how should i use post in this case
$name=$_POST['what should go in here'];
You can get the radio button value along with question ID as:
Basic Example:
<?php
$array = array(1,2); // your Question ID array
?>
Your Form:
<form method="post" action="">
<?php
foreach ($array as $key => $qid) {
?>
<input type="radio" value="1" name="radio[<?=$qid?>]">
Answer 1
<input type="radio" value="2" name="radio[<?=$qid?>]">
Answer 2
<input type="radio" value="3" name="radio[<?=$qid?>]">
Answer 3
<input type="radio" value="4" name="radio[<?=$qid?>]">
Answer 4
<?php
echo "<br/>";
}
?>
<input type="submit" name="submit">
</form>
In PHP:
<?php
if(isset($_POST['submit'])) {
$query = array();
foreach ($_POST['radio'] as $key => $value) {
$query[] = "('$value','$key')";
}
$sql = "INSERT INTO table (answer,questionID) VALUES ";
$sql .= implode(",", $query);
echo $sql;
}
?>
In this example query look like:
INSERT INTO table (answer,questionID) VALUES ('2','1'),('3','2')
Few Suggestions:
- Your code is open for SQL Injection, you must need to prevent your code with SQL Attack and this reference will help you to understand: How can I prevent SQL injection in PHP?
- Make sure your column name and table name not having any conflict, currently, you are using same name for both.
Update with Your Code:
<?php
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer4'];?>
<?
}
?>
In PHP:
<?php
if(isset($_POST['submit'])) {
$query = array();
foreach ($_POST['radio'] as $key => $value) {
$query[] = "('$value','$key')";
}
$sql = "INSERT INTO table (answer,questionID) VALUES ";
$sql .= implode(",", $query);
echo $sql; // run this query in mysqli_query()
}
?>
Few More Instructions:
- Change the table name as per your table name
- Change the column name according to your column.
- Use INSERT query at once, no need to use it inside the loop.
Create an array at the end of while loop like this :
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer4'];?>
<? $names[] = $result['qid'];
}
if(isset($_POST['submit'])) {
for ($i=0; $i<count($names); $i++) {
if(isset($_POST[$names[$i]]) {
$rate = $_POST[$names[$i]];
$sql ="INSERT INTO answer (answer, qid) VALUES (".$rate.", ".$names[$i] .")";
mysqli_query($con, $sql);
}
}
}
?>

MySQL UPDATE not working?

I'm making a site where the owner has to be able to update their events, but my update code isnt working even though im 99% sure I havent made any errors.
First the form where you press update:
<?php
$sql = "SELECT * FROM events ORDER BY id ASC";
$res = $objCon->query($sql) or die('fejl i query:'.mysqli_error($objCon));
while($row=$res->fetch_array()) {
$id = $row['id'];
echo "<div class='eventpost'>";
echo "<div class='dato'>";
echo $row['id'];
echo "</div>";
echo "<p class='overskrift'>";
echo "<a href='update.php?id=$id'>RET </a>";
echo "<a href='code_delete.php?id=$id'>SLET</a>";
echo $row['overskrift'];
echo "</p>";
echo "</div>";
}
?>
then the update form:
<form action="code_update.php" method="POST">
<label>Dato:<br>
<input type="text" name="dag" value="<?php echo $data['dag']; ?>"></label>
<label>MÃ¥nede:<br>
<input type="text" name="month" value="<?php echo $data['month']; ?>"></label>
<label>Overskrift:<br>
<input type="text" name="overskrift" value="<?php echo $data['overskrift']; ?>"></label>
<label>Tekst:<br>
<input type="text" name="tekst" value="<?php echo $data['tekst']; ?>"></label>
<input type="hidden" name="id" value="<? echo $id; ?>">
<input type="submit" value="Opret">
</form>
and finally the update code
<?php
session_start();
if($_SESSION['auth'] == 2){
include('incl_db.php');
$id = $_POST['id'];
$overskrift = $_POST['overskrift'];
$dag = $_POST['dag'];
$month = $_POST['month'];
$tekst = $_POST['tekst'];
$sql = "UPDATE events SET overskrift='$overskrift', dag='$dag', month='$month', tekst='$tekst' WHERE id='$id'";
$res = $objCon->query($sql);
header('location:events.php');
}else{
header('location:index.php');
}
?>
Probably shorthand tags are disabled in your php version.So try changing this
<input type="hidden" name="id" value="<? echo $id; ?>">
to this
<input type="hidden" name="id" value="<?php echo $id; ?>">
You can check this answer for more.

How to create a list of html radio buttons from php array?

I have following code (it's piece of bigger code):
<?php
include_once 'init/init.funcs.php';
$x = $_SESSION['answering']['index'];
echo $_SESSION['answering']['questions'][$x-1];
$result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused
where kysimus= "' .$_SESSION['answering']['questions'][$x] . '"');
$question_id = mysql_result($result4, 0);
$result5 = mysql_query('SELECT * from katse_valik_vastused
where kysimus_id="'. $question_id . '"');
if($result5 === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
//foreach($options as $option=>$option_value) {
//echo $option_value;
$count=count($options);
?>
<html>
<br>
<form method="post" action="answering.php">
<input type="radio" name="1"><?php echo $options[0]?><br>
<input type="radio" name="2"><?php echo $options[1]?><br>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
Right now there are two fixed radio buttons. But I want it to have as many buttons, as many elements are in array "options" and each of them to have a value of one element written next to it. How could I do it?
Use a for loop for this: http://www.php.net/manual/en/control-structures.for.php
for ($i = 1; $i < count($options); $i++) {
?>
<input type="radio" name="<?php echo $i; ?>"><?php echo $options[$i]?><br>
<?php
}
Try like this:
<?php
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
?>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach($options as $option=>$option_value) {
?>
<input type="radio" name="<?= $option; ?>"><?php echo $option_value?><br>
<?php }?>
<input name= "submit" type="submit" value="Vasta">
</form>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach ($options as $index=>$option) {
echo "<input type='radio' name='{$index}'>{$option}<br>";
}
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
Try using the below code.
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach ($options as $key => $value) {
?>
<input type="radio" name="<?php echo $key; ?>"><?php echo $options[$key] ?><br>
<?php
}
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
you can do this using for each, like this:
<form method="post" action="answering.php">
<?php foreach ($options as $key => $value): ?>
<input type="radio" name="<?php echo $key; ?>" /><?php echo $value; ?><br />
<?php endforeach; ?>
<input name= "submit" type="submit" value="Vasta">
</form>

Updating Post not working

I recently made a code that updates my posts on my blog. It worked perfectly on localhost. But when i uploaded it online it did not work any more. The weird thing is it doesn't even display a error so i have no idea where to look. Can someone please help me ?
require('config.php');
$query = "SELECT * FROM project ORDER BY idproject DESC";
$result = mysqli_query($verbinding, $query ) or die (mysqli_error('kan geen verbinding maken met de database'));
if(isset($_POST['editBut'])){
$editTitle = $_POST['editName'];
$editThis = mysqli_query($verbinding, "SELECT * FROM project WHERE title = '".$editTitle."'");
$values = mysqli_fetch_assoc($editThis);
}
if(isset($_POST['update'])){
$editedTitle = $_POST['newTitle'];
$editedText = $_POST['newTekst'];
$oldTitle = $_POST['oldTitle'];
$date = $_POST['datum'];
$updater = mysqli_query($verbinding, "UPDATE Project SET title='".$editedTitle."', content='".$editedText."' WHERE title='".$oldTitle."' AND datum='".$date."'");
echo $updater;
header('location:editPost.php?id=1');
}
if(isset($_GET['id'])){
echo 'post has been succesfully updated';
}
<?php if(isset($_POST['editBut'])){ ?>
<form action="" method="post">
Title: <input type="text" name="newTitle" value="<?php echo $values['title'] ?>"><br>
Text: <textarea type="text" name="newTekst" id="newTekst"><?php echo $values['content'] ?></textarea><br>
<input type="hidden" value="<?php echo $values['title'] ?>" name="oldTitle">
<input type="hidden" value="<?php echo $values['datum'] ?>" name="datum">
<input type="submit" name="update" value="Edit post">
</form>
<?php } else { ?>
<p>Find the post you want to edit:</p>
<form action="" method="post">
<select name="editName">
<?php
while ($row = mysqli_fetch_assoc($result)) {
?> <option value="<?php echo $row['title'] ?>"><?php echo $row['title'] ?></option>
<?php } ?>
</select>
<input type="submit" name="editBut" value="Choose">
</form>
<?php } ?>
In update query replace your table name with small letter.
replace Project with project

Categories