Inserting values from mysql_fetch_assoc into form input - php

I have som problem writing back to table.
I connect to table as wanted and get the information output correctly. The input correctedby, and the checkboxes is saving as i want.
but i dont know how to write the mysql_fetch_assoc back to table
I have this kode;
<?php
session_start();
$_SESSION['mypassword']="myusername";
echo "Logged in as:<br>" .$_SESSION['myusername'];
include "header.inc.php";
include "funksjoner.inc.php";
//in this file the connetion to server
$connection= kobleTil(); //trenger ikke oppgi databasenavn
//Steg 2: SQL-query
$sql = "SELECT * FROM oppgave WHERE modulid=1 AND resultat is NULL ORDER BY RAND() LIMIT 1";
$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
}
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="dont know how to write correct here">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
?>
how can I get the form to get the value from mysql_fetch_assoc into the form?
Is the mysql_fetch_assoc the right thing to use?
Very gratefull for any tip!

$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="'.$nextrow['columnName'].'">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
}
or
$data = null;
$result = mysql_query($sql, $connection);
echo "<hr>";
while ($nextrow= mysql_fetch_assoc($result)){
echo "answer: " . $nextrow['answer'];
echo "<br>Modulid: " . $nextrow['modulid'];
echo "<br>student: " . $nextrow['studentid'];
echo "<br>";
$data = $nextrow['colunmName'];
}
echo '<form name="input" action="tilretting.php" method="get">';
echo'<input type="text" name="correctedby" value="'.$_SESSION['myusername'].'">';
echo 'Not approved<input type="checkbox" name="resultat" value="0">';
echo 'Approved<input type="checkbox" name="resultat" value="1">';
echo '<input type="text" name="studentid" value="'.$data.'">';
echo '<input class="levermodulknapp" type="submit" name="lever1" value="Lever modul 1">';
echo "</form>";
echo "<hr>";
}
?>

Related

saving the options selected in quiz and checking

I have generated a quiz with questions on the same page.I want to save the answers to check with the answers in the database .How can I do it ? Below is the code for how I generated quiz.How to store the selected answer and check?
<?php for($i=1;$i<=3;$i++)//loop for extracting question and options
{?>
<form action = "CHECK.php" method="POST">
<?PHP
$sql = "SELECT QUESTION FROM T_QUESTIONS WHERE QUES_NO=$i LIMIT 1";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>QUESTION $i :</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['QUESTION'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
?>
<?PHP
$s = "select OPTION1,OPTION2,OPTION3,OPTION4,OPTION5,CORRECT_ANSWER from T_QUESTIONS where QUES_NO='$i'";
$result=mysqli_query($conn,$s);
if(!mysqli_query($conn,$s))
echo mysqli_error($conn);
else
while ($row = $result->fetch_assoc()) {
?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION1']; ?>" /> A. <?php echo $row['OPTION1']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION2']; ?>" /> B. <?php echo $row['OPTION2']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION3']; ?>" /> C. <?php echo $row['OPTION3']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION4']; ?>" /> D. <?php echo $row['OPTION4']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION5']; ?>" /> E. <?php echo $row['OPTION5']."<br>"; ?>
<br>
<?php
}
}
?>
</form>
<button style ="color:red;border radius:10px;margin:100px;height:100px;width:200px;font-size:30px"> <a href=CHECK.php>SUBMIT TEST</a></button>
Correct_OPTION is in the same table that of question and options.

Passing HTML hidden variable through a PHP script

I have an HTML form that is echoed from my php script. When I try to pass a hidden variable through the script, the code does not work as intended.
$threadNumber=$row["Tid"];
echo "<center><b>Message# ", $messageNumber , ": </b><br>";
echo $row["Mtitle"] , " in Thread# " , $row["Tid"] , "<br>";
echo "The Message was Written on " , $row["Mdate"] , '<br>';
echo $row["Mbody"];
echo "<br><br>";
echo '<form action="messageReply.php" method="post">';
echo '<textarea name="reply" rows=5 cols=30 placeholder="Reply to the Message?"></textarea>';
echo '<input type="hidden" name="Mtitle" value="<?php echo $row["Mtitle"] ?>">';
echo '<input type="submit" value="Send Message">';
echo '</form>';
The result looks like this:
and when I try to read $_POST["Mtitle"] in the messageReply.php script, I get an error saying such an index does not exist.
Try this:
echo '<input type="hidden" name="Mtitle" value='.$row['Mtitle'].'>';
Your Complete Code (Modified):
$threadNumber=$row["Tid"];
echo "<center><b>Message# ", $messageNumber , ": </b><br>";
echo $row["Mtitle"] , " in Thread# " , $row["Tid"] , "<br>";
echo "The Message was Written on " , $row["Mdate"] , '<br>';
echo $row["Mbody"];
echo "<br><br>";
echo '<form action="messageReply.php" method="post">';
echo '<textarea name="reply" rows=5 cols=30 placeholder="Reply to the Message?"></textarea>';
echo '<input type="hidden" name="Mtitle" value='.$row['Mtitle'].'>';
echo '<input type="submit" value="Send Message">';
echo '</form>';
Note: You are using php in php. Please change it below code:
<?php
echo '<input type="hidden" name="Mtitle" value="'.$row["Mtitle"].'">';
?>

Checkbox values in to array

<?php
$query_o_meno = "SELECT meno FROM uctovnictvo GROUP BY meno ORDER BY meno";
$result_meno = mysql_query($query_o_meno) or die(mysql_error());
?>
<form method="post">
<?php
echo "<strong>Meno:</strong>";
echo "<br />";
while($row = mysql_fetch_array($result_meno)){
echo '<input type="checkbox" name=" option_meno[] " value="XXX" />' . $row['meno'];
echo "<br />";
}
?>
<input type="submit" name="Filtrovat" value="Filtrovat" />
</form>
Hello could u please help me to solve:
SOLVED - Instead of XXX in value="XXX" i wanna have value of $row['meno']
TO DO - Checked values I wanna insert in to array
Thank you
echo "<input type='checkbox' name='option_meno[]' value='".$row['meno']."' />" . $row['meno'];
Does that put the value in the 'value'?

update mysql row with html form and php

I've been looking through many threads on here without finding a solution to my problem.
I've created a form that is supposed to show content of a database in input boxes, and when i change the content, it should be updated in the database.
No errors, nothing gets changed.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result)){
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
if(isset($_POST['update'])){
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $sql, $con );
if(! $retval ){
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
mysqli_close($con);
?>
The form show the database content fine, but nothing happens when changed.
I appreciate any help I can get.
This is what it looks like now.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $con, $sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="nameid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
Now i get this error.
Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 17
Warning: mysqli_query(): Empty query in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 19
Could not update data:
Because your udate is at the end of the page put it above the rest.
And also change isset($_POST['update'] to isset($_POST['gem']
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = "UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'";
$retval = mysqli_query($con,$sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
try to replace :
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
with :
echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';
In your form you are having update button name as gem but you are using if(isset($_POST['update'])) to run update query.
Change it to if(isset($_POST['gem']))
The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"
Edit :: Answered already.
echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';
Just delete the second td from above!!!

Passing a selected mysql_fetch_array() value to a processing script

This script takes all values from the users table and outputs them in a 'send friend request' type scenario for a social network I'm building. So how do I successfully pass $row['id'] to process-request.php?
$userid = $_SESSION['userid'];
$results = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($results)) {
if($userid != $row['user_pid']) {
echo $row['firstname'] . " " . $row['lastname'];
echo "<form method='POST' action='processing/process-request.php'>";
echo '<input name="accepted" type="submit" value="Send User Request" /><br />';
echo '<input name="AddedMessage" placeholder="Add a message?" type="textbox" />';
echo '<br>Select Friend Type: ' . '<br />Full: ';
echo '<input name="full_friend" type="checkbox"';
echo '<input type="hidden" name="id" value="' . $row["id"] . '" />';
echo '</form>';
echo "<br /><hr />";
} elseif ($userid == $row['user_pid']) {
echo $row['firstname'] . " " . $row['lastname'];
echo "<br />";
echo "You all are already friends";
}
}
Since you're already using sessions,
$_SESSION['row-id'] = $row['id'];

Categories