I am trying to get the code to update some data that has been selected by the user.
What is happening is that its updating everything in that column to the value the user has edited it to.
For example i have bob, bob1, bob2 and i want to select bob1 to edit. I then edit it so bob1 is now fred1, when it updates it updates every value to fred1. So there is 3 fred1's.
Could someone help? please.
update.php
<?php
session_start();
include_once 'connection.php';
if (isset($_POST['update'])) {
$success = updateValue($_POST['name']);
if (!$success)
echo 'Sorry, the update failed';
session_destroy();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Main Page</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
View a Member's Details:
<select name='members' value='members'id="Mmembers">
<?php
$results = getResults('members');
if ($results) {
foreach ($results as $row) {
echo '<option value="' . $row['member_id'] . '">' . $row['name'] . '</option>';
}
}
else
echo '<option value="0"0"> No Data</option>';
?>
</select>
<input type="submit" id="submit" value="Submit"/>
<br/>
<br/>
</form>
<?php
if (isset($_POST['members'])) {
$ResultSet = getMemberResults(($_POST['members']));
echo "<h1> Member Details. </h1>";
echo "<table border='1' cellpadding='6'>";
echo "<tr> <th>Id</th> <th>Name</th>";
foreach ($ResultSet as $row) {
echo "<form action=update.php method=POST>";
echo "<tr>";
echo "<td>" . "<input type=text name=member_id value=" . $row ['member_id'] . " </td>";
echo "<td>" . "<input type=text name=name value=" . $row ['name'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
}
?>
</body>
</html>
connection.php
<?php
//Connect to the database
function getSQLConnection() {
$mysqlConnection = new PDO('mysql:host=localhost;dbname=', "root", "");
return $mysqlConnection;
}
//Get all results from members table
function getResults($tablename) {
$sql = "SELECT * FROM " . $tablename;
$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
function getMemberResults($id) {
$sql = "SELECT * FROM members WHERE members.member_id = '$id'";
$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
function updateValue($id) {
$sql = "UPDATE members SET name='$id' WHERE members.member_id = members.member_id";
$mysqlConnection = getSQLConnection();
$ResultSetting = $mysqlConnection->query($sql);
return $ResultSetting;
}
?>
Your SQL UPDATE statement is incorrect.
WHERE members.member_id = members.member_id
The above where is always going to resolve true for all rows, making your call
UPDATE members SET name='$id'
You should set the name and also add the members id (a unique identity) in the WHERE cause.
/** give functions descriptive names **/
function updateMemberName($id, $name) {
$sql = "UPDATE members SET name='$name' WHERE id = '$id'";
//...
update.php
You would then just need to modify the function call
if (isset($_POST['update'])) {
$success = updateMemberName($_POST['member_id'], $_POST['name']);
function updateValue($id) {
$sql = "UPDATE members SET name='$id' WHERE members.member_id = members.member_id";
This is equivalent to
UPDATE members SET name='$id'
as the where clause evaluates to TRUE for all rows
Answering only to make it work like you expect it to, This function:
function updateValue($id) {
$sql = "UPDATE members SET name='$id' WHERE members.member_id = members.member_id";
...
}
Needs to be
function updateValue($id, $value) {
$sql = "UPDATE members SET name='$value' WHERE members.member_id=$id";
...
}
Related
I am using the following code to display certain rows from my database table:
<?php
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'Error';
exit;
}
if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
$db = include "connect2db.php";
$query = "select * from notes where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo '<p>Number of rows found: '.$num_results.'</p>';
for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<i>';
echo stripslashes($row['date']);
echo '</i><br /> ';
echo '<b>';
echo stripslashes($row['notetitle']);
echo '</b><br /> ';
echo stripslashes($row['note']);
echo '<br /><br /> ';
echo '</p>';
}
$result->free();
$db->close();
?>
Now I would like to display an edit-link for each row displayed, that can open a new page in which it is possible to edit a specific row. I already have the code that lets you edit the row:
<?php
if ($_REQUEST['save']=="Save") { // is data submitted?
// create variables
$noteid = $_REQUEST['noteid'];
$coursename = $_REQUEST['coursename'];
$notetitle = $_REQUEST['notetitle'];
$note = $_REQUEST['note'];
$query = "UPDATE notes SET ";
$query .= "coursename='$coursename', ";
$query .= "notetitle='$notetitle', ";
$query .= "note='$note' ";
$query .= "WHERE noteid='$noteid'";
$result = $db->query($query);
} elseif ($_REQUEST['delete']=="Delete") { // is data to be removed?
$noteid = $_REQUEST['noteid'];
$query="DELETE FROM notes WHERE noteid='$noteid'";
$result = $db->query($query);
}
?>
<div class="formular">
<div class="row1">
<p>Id</p>
<p>Notetitle</p>
<p>Note</p>
</div>
<?php
$query = "SELECT * FROM notes ORDER BY noteid DESC";
$result = $db->query($query);
while ($row = mysqli_fetch_array($result)) {
echo "<form ".$_SERVER['PHP_SELF']." name='edit-form' method='post' class='row1'>\n";
echo "<p class='align_top padding_top'>".$row['noteid']."<input type='hidden' name='noteid' value='".$row['noteid']."' /></p>\n";
echo "<p class='align_top'><input type='text' name='notetitle' value='".$row['notetitle']."' /></p>\n";
echo "<p><textarea name='note' rows='10' cols='50'>".$row['note']."</textarea></p>\n";
echo "<p><input type='submit' name='save' value='Save' /></p>";
echo "<p><input type='submit' name='delete' value='Delete' /></p>";
echo "</form>\n";
}
echo '</div>';
$result->free();
$db->close();
?>
What I am struggling with is how to display an edit-link for each row that lets you open a page where you can edit/delete the content of only that row.
I hope someone can help, I am very new at this.
Thank you!
Add a button next to each row that opens an edit page (or modal) with the id inside, example: <button onclick="edit('randomId')">Edit RandomId </button>
You could implement something different that accepts the unique id of that specific row and open a new page or modal with it.
<form action='movies.php' method='POST'>
Language: <select name="language">
<option selected>hindi</option>
<?php
require("config.php");
$result="SELECT language FROM movies";
$q = mysqli_query($conn,$result) or die(mysql_error());
while ($row=mysqli_fetch_array($q)) {
$s1=$row["language"];
echo "<option>
$s1
</option>";
}
echo "<br>"
?>
</select>
<br /> <br />
<input type='submit' value='Submit' />
</form>
<?php
$lang=#$_POST['language'];
$_SESSION["lang1"]=$lang;
/*
if($lang){
$sql = "SELECT name,language FROM movies WHERE language='$lang'";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result)>0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div class="query"> <img src="images\tiles\bb.jpg"> ';
echo "<h3> " . $row["name"]."</h3>". "<br>";
echo "</div>";
}
} else {
echo "0 results";
}
}
*/
$page=#$_GET['page'];
if($page==""|| $page=="1")
{
$page1=0;
}
else{
$page1=($page*3)-3;
}
$sql = "SELECT name,language FROM movies WHERE language='". $_SESSION['lang1']."' limit $page1,3 ";
$result = mysqli_query($conn,$sql);
$a= mysqli_num_rows($result);
while ($list=mysqli_fetch_array($result))
{
echo $list['name'] . " : " . $list['language'] . "<br />";
}
$sql = "SELECT name,language FROM movies WHERE language='$lang'";
$result = mysqli_query($conn,$sql);
$a= mysqli_num_rows($result);
$numrows=$a;
$rowsperpage=3;
$totalpages= ceil($numrows/$rowsperpage);
echo "</br>";
for($b=1;$b<=$totalpages;$b++)
{
?><?php echo $b." ";?> <?php
}
?>
I get proper output on movies.php which is the first 3 rows from database,but when i click on the dynamically created pagination link like movies.php?page=2 then there is no output on this page.This code works fine if i manually set the
$_SESSION["lang1"]="English"; then i get proper output but when i take input from the form it doesnt work.
I am trying to make a loop that gets the name some other info about a product from a sql table - MySQL table
Then Creates a page that looks like that - Webpage
So Far I have this code that does show it but I cant figure out a way how to update the name of the dropdown menu so when I press submit It writes into another SQL table the name of the product and then how many of those products did the customer selected .
<?php
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";
echo " ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";
echo "<td>\n";
echo " ##dropdown## ";
echo "<select id=$value>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
echo "</td>\n";
echo "</tr>\n";
}
}
}
$conn->close();
?>
<html>
<body>
<form method="POST" action="#" >
<input type="submit" name="Submit" value="Submit" /><br>
</form> </body>
</html>
I know I will most likely need a second php script for the capture of the post so help with that will be greatly appreciated too .
////////////////////////////////////////////////
So up to here I got it somehow - It loops and shows all the product . When I press submit it adds only the last product in the loop and it doesn't care of the drop down menu - Just adds a "2" .
The table where the script writes is simple - 4 columns OrderID1,productid1,ProductName1 orderedqnt1
Thanks in advance .
Index.php
<html>
<body>
<form method="POST" action="insert.php" >
<?php
session_start(); // session start for Variables to add to the sql in Insert.php
include("global.php"); // Stores the session Variables
## Conection part
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";
echo " ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";
echo "<td>\n";
/* echo " ##dropdown## "; */
echo "<select id=" . $value . " name='dropdown'>\n";
echo "<option value=''>-</option>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
/* echo "</td>\n"; */
/* Echo ":::::value variable = "."$value"; */
echo "</tr>\n" . "<br>";
print_r ($value);
$_SESSION['GrabIDses']=mysqli_real_escape_string($conn,$row['ProductID']); //Grabs the ID of the product in Session Variable
$_SESSION['GrabNameses']=mysqli_real_escape_string($conn,$row['ProductName']); //Grabs the Name of the product in Session Variable
$_SESSION['GrabSKUses']=mysqli_real_escape_string($conn,$row['SKU']); //Grabs the SKU of the product in Session Variable
$_SESSION['Ordered']=mysqli_real_escape_string($conn,$value); //Grabs the Ordered Quantity for the product in Session Variable ????????????????
/* $GrabID = mysqli_real_escape_string($conn,$row['ProductID']);
$GrabName = mysqli_real_escape_string($conn,$row['ProductName']);
$GrabSKU = mysqli_real_escape_string($conn,$row['SKU']);
echo "----------------------"."$_SESSION['GrabSKUses']"."<br>"."$_SESSION['GrabIDses']"."<br>"."----------------------"; */
}
}
}
$conn->close();
echo "<br>";
?>
<input type="submit" name="Submit" value="Submit" /><br>
</form>
</body>
Insert.php
<?php
session_start(); // session start
$getvalue = $_SESSION['GrabIDses']; // session get
$getvalue1 = $_SESSION['GrabNameses']; // session get
$getvalue2 = $_SESSION['GrabSKUses']; // session get
$ordered11 = $_SESSION['Ordered']; // session get
echo $getvalue;
echo "||";
echo $getvalue1;
echo "||";
echo $getvalue2;
echo "||"."<br>";
print_r($_SESSION);
## Connection Part
if(isset($_POST['dropdown'])) {
echo("You order was completed" . "<br>");
$sql = "INSERT INTO testorder (productid1,ProductName1,orderedqnt1) VALUES ('$getvalue', '$getvalue1','$ordered11')";
if (mysqli_query($conn, $sql))
{ echo "New record created successfully"; }
else
{ echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
mysqli_close($conn);
}
else {
echo" dhur";
}
?>
The < select>-Boxes have to be inside the form and inside the web page at all.
Enter a name-attribute in the < select>-Tag, to make the data available in the saving script vie $_POST
I am not sure, what you want to do, so I don't know if $row['ProductID'] is a reasonable name.
<html>
<body>
<form method="POST" action="#" >
<?php
$servername = "localhost";
$username = "root";
$password ="";
$dbname = "company";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";
echo " ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";
echo "<td>\n";
echo " ##dropdown## ";
echo "<select id='$value' name='{$row['ProductID']}'>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
echo "</td>\n";
/* Echo ":::::value variable = "."$value"; */
echo "</tr>\n";
}
}
}
$conn->close();
?>
<input type="submit" name="Submit" value="Submit" /><br>
</form>
</body>
</html>
<body>
<form method="POST" action="#" >
<?php
$servername = "localhost";
$username = "root";
$password ="";
$dbname = "company";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";
echo " ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";
echo "<td>\n";
echo " ##dropdown## ";
echo "<select id='$value' name='{$row['ProductID']}'>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
echo "</td>\n";
/* Echo ":::::value variable = "."$value"; */
echo "</tr>\n";
}
}
}
$conn->close();
?>
<input type="submit" name="Submit" value="Submit" /><br>
</form>
</body>
</html>
echo "<select id=$value>\n";
needs a name. So change to
echo "<select id=" . $value . " name='dropdown'>\n";
Then you need to make a second page, use
if(isset($_POST['dropdown'])) {
Then insert or update table with the info. I think you can get this part down quite easily :).
Edit:
while ( $row = mysqli_fetch_assoc($result) ) {
$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
You're using 2 while loops. That's 1 to many.
I created a drop down list that has been populated by the database and now I'm having trouble retrieving the data. Normally, I would know how to retrieve the value of the drop down list if I had to manually name the data, but in this case, I'm not quite sure how I would name it.
Here is my current code:
<h1>Generate Reports</h1>
<form enctype="multipart/form-data" action="http://localhost/yiiFolder/index.php/create" method="post">
<table>
<tr>
<td><strong>Materials</strong></td>
<?php
mysql_connect('host', 'root', 'password');
mysql_select_db ("db");
$sql = "SELECT material_name FROM materials";
$result = mysql_query($sql);
echo "<td><select name='materials'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['material_name'] . "'>" .
$row['material_name'] . "</option>";
}
echo "</select></td></tr> ";
$sql2 = "SELECT location_name From locations";
$result2 = mysql_query($sql2);
?>
<td><strong>Locations</strong></td>
<?php
echo "<td><select name='locations'>";
while ($row2 = mysql_fetch_array($result2))
{
echo "<option value='" . $row2['location_name'] . "'>" .
$row2['location_name'] . "</option>";
}
echo "</select></td></tr>";
?>
<tr>
<td><button name="submit" type=submit>Generate</button></td>
</tr>
</table>
</form>
<?php
$material = $row['material_name'];
$locations = $row2['location_name'];
$generate = $_POST['submit'];
if(isset($generate))
{
echo $material;
echo $locations;
}
?>
You're trying to capture value before the submit button is hit. Also, as Hanky pointed out you're using the wrong names while referring to select data. You should do this instead
if(isset($_POST['submit'])) // this code will run after the button is clicked
{
$material = $_POST['materials']; // and not material_name
$locations = $_POST['locations']; // and not location_name
echo $material;
echo $locations;
}
PS: You're following a very unsecure way of developing a web application. At the very least you need to switch to PDO and always escape the data.
I've been doing some work on my website control panel for a game I'm working on. But I can't seem to get the "Ban!" button to be "Unban!" if the result $row['banned'] is FALSE. I get it to output TRUE : FALSE depending on what it says in the table.
Any help getting this fixed would be greatly appreciated. I have struggled with this for a few days now and I felt like giving up once or twice but this has to be completed to help the admins on my game have it easier to check banned accounts and control the options.
p.s "connect.php" only has a few variables that are used and the mysql connect string.
<?php
require('connect.php');
if(isset($_POST['ban'])){
$id = $_POST['ban_rec_id'];
$query = "UPDATE accounts SET banned=1 WHERE id=$id";
$result = mysql_query($query);
}else if(isset($_POST['unban'])){
$id = $_POST['unban_rec_id'];
$query = "UPDATE accounts SET banned=0 WHERE id=$id";
$result = mysql_query($query);
}
$query = "SELECT id, uuid, name, REPLACE(REPLACE(banned,'0','FALSE'),'1','TRUE') AS banned FROM accounts ORDER BY id ASC";
$result = mysql_query($query);
echo "<center>
<table>
<tr>
<th>Acccount Id</th>
<th>Username</th>
<th>In-Game Name</th>
<th>Banned</th>";
if($ban === true){
echo "<th>Ban</th>";
}
echo "</tr>";
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$username = $row['uuid'];
$gamename = $row['name'];
$banned = $row['banned'];
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $username . "</td>";
echo "<td>" . $gamename . "</td>";
echo "<td>" . $banned . "</td>";
if($ban === true){
if($row['banned'] == FALSE){
echo "<td>"?>
<form id="ban" method="post" action="">
<input type="hidden" name="ban_rec_id" value="<?php print $id; ?>"/>
<input class="button-small" type="submit" name="ban" value="Ban!"/>
</form>
<?php "</td>";
} else {
echo "<td>"?>
<form id="unban" method="post" action="">
<input type="hidden" name="unban_rec_id" value="<?php print $id; ?>"/>
<input class="button-small" type="submit" name="unban" value="Unban!"/>
</form>
<?php "</td>";
}
}
echo "</tr>";
}
echo "</table></center>";
mysql_close($link);
?>
Try use string for FALSE instead since looks like you might have assigned it with String rather than Boolean value in your error-prone REPLACE(REPLACE(banned,'0','FALSE'),'1','TRUE'):
if($row['banned'] == 'FALSE')
So what actually is the problem with that? The code seems to be fine, in case the value in 'banned' column is false or true.
But you should check the right value type in your columns. If it is string (varchar, text etc) that is saying 'FALSE' or 'TRUE' you should use
'FALSE' instead of FALSE