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'];
Related
I am having a table with <input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"
populated from data that i fetch from database like this:
echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">';
echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>";
while($r = mysql_fetch_array($result)) {
$my[] = $r['0'];
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo "<pre>";
print_r($my);
echo "</pre>";
if(isset($_POST['unsubscribe'])){
foreach($my as $key=>$value){
$email = $value;
}
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
echo '<button style="position:fixed;bottom:5;left:5;">Change</button>';
echo '</table></form>';
The table looks like this:
I have tried this:
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
But the value is empty
So each time i press unsubscribe button the corresponding email to be deleted. How is this possible?
Your form has many elements with the same name. How can the browser determine which element's value to send to the server when the form is posted? Generally the last one takes precedence, but I suspect that behavior may be undefined and browser-specific.
If each individual table row needs to be a separately post-able form, then each row needs its own form:
echo '<td>
<form method="POST" action="somePage.php">
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</form>
</td>';
That way when the browser posts the form to the server, it knows specifically which email and unsubscribe elements to use. Since there's only one of each for that form.
You have to wrap your inputs in a <form> tag.
echo '<form>';
while($r = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo '</form>';
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
Based on your code above it looks like it's a syntax error. Try the update below
if(isset($_POST['unsubscribe'])){
$email = $_POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' ); </script>";
}
I'm attempting to update all of the fields displayed from a MySQL table. The delete feature currently works, but I'm not getting any output whatsoever for the update feature. I feel like I'm missing something very basic. Any input is appreciated!
<form action="" method="post">
<?php
$con = mysqli_connect($host,$username,$password,$dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = mysqli_query($con,"SELECT * FROM customers");
while($row = mysqli_fetch_array($query))
{
echo "<tr>";
echo "<td><input type=\"text\" name=\"FirstName\" value=\"" . htmlspecialchars($row['FirstName'], ENT_QUOTES, 'UTF-8') . "\"></td>";
echo "<td><input type=\"text\" name=\"LastName\" value=\"" . htmlspecialchars($row['LastName'], ENT_QUOTES, 'UTF-8') . "\"></td>";
echo "<td>" . htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['address'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['phone'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['product'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['firmware'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['purchase_date'], ENT_QUOTES, 'UTF-8') . "</td>";
echo '<td align="center"><input type="hidden" name="email" value="';
echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8');
echo '"><input type="submit" name="delete" value="X"> </td>';
echo "</tr>";
echo '<tr><td colspan="9"><input type="hidden" name="email" value="';
echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8');
echo '"><input type="submit" name="edit" value="Update"></td></tr>';
}
echo "</table>";
?>
<?php
if(isset($_POST['delete'])) {
$email = $_POST['email'];
$delete = "DELETE FROM customers WHERE email = '$email'";
if ($con->query($delete) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $con->error;
}
if (isset($_POST['edit'])) {
$email = $_POST['email'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$edit = "UPDATE customers SET Firstname = '$FirstName', LastName = '$LastName' WHERE email = '$email'";
if ($con->query($edit) === TRUE) {
echo $edit;
echo "Record updated successfully";
} else {
echo $edit;
echo "Error updating record: " . $con->error;
}
}
$conn->close();
}
?>
</form>
check your spelling on conn
Try this:
<form action="" method="post">
<?php
$conn = mysqli_connect($host,$username,$password,$dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// $query = mysqli_query($conn,"SELECT * FROM customers");
$query = $conn->query("SELECT * FROM customers");
while($row = $query->fetch_assoc())
{
echo "<tr>";
echo "<td><input type=\"text\" name=\"FirstName\" value=\"" . htmlspecialchars($row['FirstName'], ENT_QUOTES, 'UTF-8') . "\"></td>";
echo "<td><input type=\"text\" name=\"LastName\" value=\"" . htmlspecialchars($row['LastName'], ENT_QUOTES, 'UTF-8') . "\"></td>";
echo "<td>" . htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['address'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['phone'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['product'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['firmware'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['purchase_date'], ENT_QUOTES, 'UTF-8') . "</td>";
echo '<td align="center"><input type="hidden" name="email" value="';
echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8');
echo '"><input type="submit" name="delete" value="X"> </td>';
echo "</tr>";
echo '<tr><td colspan="9"><input type="hidden" name="email" value="';
echo htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8');
echo '"><input type="submit" name="edit" value="Update"></td></tr>';
}
echo "</table>";
?>
<?php
if(isset($_POST['delete'])) {
$email = $_POST['email'];
$delete = "DELETE FROM customers WHERE email = '$email'";
if ($conn->query($delete) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
if (isset($_POST['edit'])) {
$email = $_POST['email'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$edit = "UPDATE customers SET Firstname = '$FirstName', LastName = '$LastName' WHERE email = '$email'";
if ($conn->query($edit) === TRUE) {
echo $edit;
echo "Record updated successfully";
} else {
echo $edit;
echo "Error updating record: " . $conn->error;
}
}
$conn->close();
}
?>
</form>
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>";
}
?>
I have an code that gets the 'branches' from the database. Each company can have multiple 'branches'.
Only thing is, that is doesn't work. Can you guys figure out what's wrong?
$getbranches = "SELECT * FROM branches ORDER BY naam ASC";
$querygetbranches = mysql_query($getbranches);
while($rijbranche = mysql_fetch_assoc($querygetbranches))
{
echo "<tr>";
echo "<td width='400'>";
echo $rijbranche['naam'];
echo "</td>";
echo "<td>";
$get2 = "SELECT * FROM bedrijf_branche WHERE bedrijf_id = '$id'";
$query2 = mysql_query($get2);
while ($rij20 = mysql_fetch_assoc($query2))
{
$branche_id = $rij20['branche_id'];
}
if($branche_id == $rijbranche['id_branche']){
?>
<input type="checkbox" name="branche[]" value="<?php echo $rijbranche['id_branche']; ?>" CHECKED></input>
<?php
}
else
{
?>
<input type="checkbox" name="branche[]" value="<?php echo $rijbranche['id_branche']; ?>"></input>
<?php
}
echo "</td>";
}
Try the following code
<?php
$id = $_GET['id'];
// Output BRANCHES
$getbranches = "SELECT * FROM branches ORDER BY naam ASC";
$querygetbranches = mysql_query($getbranches);
while ($rijbranche = mysql_fetch_array($querygetbranches)) {
echo ' <tr>' . "\n";
echo ' <td width="400">' . $rijbranche['naam'] . '</td>' . "\n";
// Output CHECKBOX
$get2 = mysql_query("SELECT * FROM bedrijf_branche WHERE bedrijf_id = '" . $id . "' AND branche_id = '" . $rijbranche['id_branche'] . "'");
$rij20 = mysql_fetch_array($get2);
$branche_id = $rij20['branche_id'];
if ($branche_id == $rijbranche['id_branche']) {
$checkbox = '<input type="checkbox" name="branche[]" value="' . $rijbranche['id_branche'] . '" checked="checked" />';
}
else {
$checkbox = '<input type="checkbox" name="branche[]" value="' . $rijbranche['id_branche'] . '" />';
}
echo ' <td>' . $checkbox . '</td>' . "\n";
echo ' </tr>' . "\n";
}
?>
Found a couple of errors I fixed in the above code.
You're closing the <input> fields incorrectly
Your second while() loop is unnecessary as there should only be one row returned
You have to add branche_id to your second mysql_query!
Don't close and re-open your <?php ?> tags for every HTML line when you can just add an echo
Your HTML-syntax is wrong.
The way you close the input tag and the way you want to check the chechbox is wrong
Try this
<input type="checkbox" name="branche[]" value="<?php echo $rijbranche['id_branche']; ?>" checked="checked" />
I am trying to get the values of a dynamically created set of checkboxes in PHP but apparently I couldn't get it. The source codes are below.
The "managestaff.php" page would allow searching for staff via their names and throws out a list of names with checkboxes for the admin to check them and click on a "delete" button at the bottom to delete the staff whom are being checked.
The deletion would be done on "deletestaff.php" as the "delete" button on "managestaff.php" simply forwards these values to "deletestaff.php" to do deletion work of the staff.
"managestaff.php" page codes:
<b><h3>Manage Staff</h3></b><br/>
<form action="managestaff.php" method="POST">
<input name="form" type="hidden" id="form" value="true">
<table width=300>
<tr>
<td width=112>Staff Name: </td>
<td width=188><input type="text" class="textfield" name="sname" /><br/></td>
</tr>
</table><br/>
<input type="submit" value="submit" />
</form>
<?php
if (isset($_POST['form']) && (isset($_POST['sname'])) && $_POST['form'] == 'true') {
$space = ' ';
$staffname = mysql_real_escape_string($_POST['sname']);
$query = 'SELECT * from staff where staffname like \'%' . $staffname . '%\'';
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo '<br><br>';
echo '<table>';
echo '<tr><th>Staff ID' . $space . '</th><th>Staff Name' . $space . '</th></tr>';
echo '<form action="deletestaff.php" method="POST">';
echo '<input name="delstaffform" type="hidden">';
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['staffid'] . '</td><td>' . $row['staffname'] . '</td>';
// :Begin - dynamic checkbox generation for deleting staff
echo '<td>';
echo '<input type="checkbox" name="delstaff" value="' . $row['staffid'] . '" />';
echo '</td>';
// :End
echo '</tr>';
}
echo '<tr align="right"><td colspan="3"><input type="submit" value="delete"/></td></tr>';
echo '</form>';
echo '</table>';
}
}
?>
"deletestaff.php" page codes:
<?php
print_r('POST: ' . $_POST);
echo '<br>';
if (isset($_POST['delstaffform']) && isset($HTTP_POST_VARS)) {
echo 'Submission of delstaffform FOUND !';
echo 'Staff to delete' . $HTTP_POST_VARS['delstaff'];
}
else{
echo 'Submission of delstaffform NOT FOUND !';
}
?>
The "deletestaff.php" doesn't do delete for now as it's a test page.
The current output I get is "Submission of delstaffform NOT FOUND !".
Thanks for the solutions.
Try this:
<input type="checkbox" name="delstaff[]" value="' . $row['staffid'] . '"/>';
print_r your $_POST and you'll see it sticks your submissions nicely into an array for you.
<?php
if (isset($_POST['delstaff']) && is_array($_POST['delstaff'])) {
echo 'Submission of delstaffform FOUND !';
$array = $_POST["delstaff"];
foreach($array as $value){
echo "<br>Value: ".$value."<br>";
}
} else {
echo 'Submission of delstaffform NOT FOUND !';
}
?>
Found the answer on my own but nevertheless you are helpful :D . Thanks a lot.