I found this code online and now im trying to make it work. I get an error when I press Edit from the first page.
Notice: Undefined variable: id in edit_form2.php on line 19
I'm not sure why this variable isn't included in the code, where should it go? or does it have something to do with my table?
Database = album
Table = data_employees
1. id primaray key & a_i
2. name
3. address
When I press the edit button the next page should load the existing data from the database. Now it just turns up empty + the error code I wrote. What should I do?
edit.php
<td align="center">DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"dbinc.php";//database connection
$order = "SELECT * FROM data_employees";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[name]</td>");
echo ("<td>$row[employees_number]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>Edit</td></tr>");
}
?>
</table>
edit_form2.php
<?php
include "dbinc.php";//database connection
$order = "SELECT * FROM data_employees
where employees_number='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
edit_data2.php
<?php
//edit_data.php
include "dbinc.php";
$name = $_POST["name"];
$address = $_POST["address"];
$id = $_POST["id"];
$order = "UPDATE data_employees
SET name='$name',
address='$address'
WHERE
employees_number='$id'";
mysql_query($order);
header("location:edit.php");
?>
$id variable must be declared before use; like-
$id = mysql_real_escape_string($_GET['id']);
This line is wrong
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
try
<input type="hidden" name="id" value="<?php echo $row['employees_number']?>">
So quite likely as the input field does not get built properly the browser is ignoring that field completely.
edit.php
replace
echo ("<tr><td>$row[name]</td>");
echo ("<td>$row[employees_number]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>Edit</td></tr>");
with
echo "<tr><td>".$row['name']."</td>";
echo "<td>".$row['employees_number']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td>Edit</td></tr>";
edit_form.php
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
should be
<?php
$id = mysql_real_escape_string($_GET['id']);include "dbinc.php";//database connection
$order = "SELECT * FROM data_employees
where employees_number='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" name=form action="edit_data2.php">
<input type="hidden" name="id" value="<?php echo $row['employees_number']; ?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo $row['name']; ?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo $row['address']; ?>">
</td>
</tr>
<tr>
and like Mukesh Soni said, you need to grab te id from the url
Related
<?php
$query1 = "select * from users where user_id=" . $_GET['user_id'];
$result = mysqli_query($conn, $query1);
$res = mysqli_fetch_array($result);
if (!$res) {
die(mysqli_error($conn));
} else {
echo "succeess";
}
?>
<form method="post" id="editform" action="Updateuser.php"><br/>
<tr><td><b>User ID</b></td><td>
<input type="text" name="user_id" style="width:450px;height:30px" value="<? php echo $res['user_id'] ?>" /></td></tr>
<tr>
<td><b>Name</b></td>
<td>
<input type="text" name="name" style="width:450px;height:30px" value="<?php echo $res['name'] ?>">
</td>
</tr>
<tr>
<td><b>Role</b></td>
<td>
<input type="text" name="role" style="width:450px;height:30px" value="<?php echo $res['role'] ?>">
</td>
</tr>
<tr>
<td><b>Password</b></td>
<td>
<input type=" text" name="password" style="width:450px;height:30px" value="<?php echo $res['password'] ?>">
</td>
</tr>
<tr>
<td align=right>
<input type="submit" name="submit value" id="updatebutton" value="Update Record">
</td>
</tr>
whenever the code runs it says unknown colomn in where clause, i am sending user_id from the users page to this page. It is displaying user_id in the address bar but it is not fetching the whole record from the database and keeps on displaying that error. Note: My table has that colomn "user_id"
You have to change this:
$query1 = "select * from users where user_id=" . $_GET['user_id'];
to
$query1 = "select * from users where user_id='". $_GET['user_id']."'";
I am trying to take a user input and update that specific item in my database. For some reason I cannot seem to get the item to get updated.
<?php
include('../inclass/db_connect.php');
$id = $_GET["id"];
$sql = "SELECT id, teamName, coach, yearFormed, numStanleyCups, numPlayers, teamValue, totWins FROM hockeyTeams WHERE id=$id";
$result = $pdo->prepare($sql);
$result->execute();
$row = $result->fetch();
?>
<h1 align="center">Edit Team - <?php echo $row['teamName'] ?></h1>
<form method="post">
<table align="center" border="1">
<tr>
<td>Team Name:<input type="text" name="teamName" value="<?php echo $row['teamName'] ?>" required /></td>
</tr>
<tr>
<td>Coach Name:<input type="text" name="coach" placeholder="Coach Name" value="<?php echo $row['coach'] ?>" required /></td>
</tr>
<tr>
<td>Year Team Formed:<input type="date" name="yearFormed" value="<?php echo $row['yearFormed'] ?>" required /></td>
</tr>
<tr>
<td>Number of Stanley Cups Won:<select name="numStanleyCups"><option value="" selected><?php echo $row['numStanleyCups'] ?></option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option></select></td>
</tr>
<tr>
<td>Number of Players:<input type="text" name="numPlayers" value="<?php echo $row['numPlayers'] ?>"/></td>
</tr>
<tr>
<td>Team Value: <input type="text" name="teamValue" value="<?php echo $row['teamValue'] ?>"/></td>
</tr>
<tr>
<td>Total Team Wins: <input type="text" name="totWins" value="<?php echo $row['totWins'] ?>" /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-edit"><strong>Edit Team</strong></button></td> </tr>
</table>
</form>
<?php
if(isset($_POST['btn-edit']))
{
$sql = "UPDATE hockeyTeams SET teamName = '".$_POST['teamName']."', coach = '".$_POST['coach']."', yearFormed = '".$_POST['yearFormed']."', numStanleyCups = '".$_POST['numStanleyCups']."', numPlayers = '".$_POST['numPlayers']."', teamValue = '".$_POST['teamValue']."', totWins = '".$_POST['totWins']."' WHERE id = ".$_GET['id'];
$result = $pdo->prepare($sql);
$result->execute();
}
When I took out the top portion of the PHP the update function oddly enough worked once and than broke. I cant seem to put together a reasoning of why the update wouldnt be working other than having something to do with me pulling in the data at the top of the file.
I couldnt seem to find any posts that were finding a good answer to what was going wrong, I checked to make sure all my '" are correct and working so I am stuck on why it wont update!
Any help is greatly appreciated. Thanks!
The problem is that you're displaying the form before you perform the update. So you're showing the old values from the database. Move the code that performs the update to the top.
And since you're using PDO, you should use $pdo->bindParam() for your parameters, instead of concatenating strings into the SQL.
<?php
include('../inclass/db_connect.php');
$id = $_GET["id"];
if(isset($_POST['btn-edit']))
{
$sql = "UPDATE hockeyTeams SET teamName = '".$_POST['teamName']."', coach = '".$_POST['coach']."', yearFormed = '".$_POST['yearFormed']."', numStanleyCups = '".$_POST['numStanleyCups']."', numPlayers = '".$_POST['numPlayers']."', teamValue = '".$_POST['teamValue']."', totWins = '".$_POST['totWins']."' WHERE id = ".$_GET['id'];
$result = $pdo->prepare($sql);
$result->execute();
}
$sql = "SELECT id, teamName, coach, yearFormed, numStanleyCups, numPlayers, teamValue, totWins FROM hockeyTeams WHERE id=$id";
$result = $pdo->prepare($sql);
$result->execute();
$row = $result->fetch();
?>
<h1 align="center">Edit Team - <?php echo $row['teamName'] ?></h1>
<form method="post">
<table align="center" border="1">
<tr>
<td>Team Name:<input type="text" name="teamName" value="<?php echo $row['teamName'] ?>" required /></td>
</tr>
<tr>
<td>Coach Name:<input type="text" name="coach" placeholder="Coach Name" value="<?php echo $row['coach'] ?>" required /></td>
</tr>
<tr>
<td>Year Team Formed:<input type="date" name="yearFormed" value="<?php echo $row['yearFormed'] ?>" required /></td>
</tr>
<tr>
<td>Number of Stanley Cups Won:<select name="numStanleyCups"><option value="" selected><?php echo $row['numStanleyCups'] ?></option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option></select></td>
</tr>
<tr>
<td>Number of Players:<input type="text" name="numPlayers" value="<?php echo $row['numPlayers'] ?>"/></td>
</tr>
<tr>
<td>Team Value: <input type="text" name="teamValue" value="<?php echo $row['teamValue'] ?>"/></td>
</tr>
<tr>
<td>Total Team Wins: <input type="text" name="totWins" value="<?php echo $row['totWins'] ?>" /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-edit"><strong>Edit Team</strong></button></td> </tr>
</table>
</form>
I'm trying to lay out my MySQL data in a table on the page. Instead of directing the user towards other forms to perform the Add, Update and Delete queries, I instead opted to have the queries within the table in the form of buttons, Saving time and effort as rows can be added, updated or deleted right then and there. However now with my page, form and tables set up I tried establishing the queries that the buttons would have set to them. And after testing it out, nothing works, when I click any of the buttons, the fields just return to whatever they originally were before I changed them.
Admin_Album_Page.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'ConnectorCode.php';
?>
<!DOCTYPE HTML>
<html>
<head>
<title> Albums </title>
</head>
<body>
<?php
if(isset($_POST['update'])) {
$UpdateQuery = "UPDATE tbl_Albums SET Album_Name='{$_POST['albumname']}',
Number_Of_Tracks='{$_POST['numberoftracks']}', Genre='{$_POST['genre']}',
Artist_id='{$_POST['artistid']}' WHERE Album_id='{$_POST['hidden']}'";
mysqli_query ($conn, $UpdateQuery);
}
if(isset($_POST['delete'])) {
$DeleteQuery = "DELETE FROM tbl_Albums WHERE Album_id='$_POST[hidden]'";
mysqli_query ($conn, $DeleteQuery);
}
if(isset($_POST['add'])) {
$AddQuery = "INSERT INTO tbl_Album (Album_Name, Number_Of_Tracks, Genre, Artist_id)
VALUES ('$_POST[uartistname]', '$_POST[unumberoftracks]',
'$_POST[ugenre]',
'$_POST[uartist]')";
mysqli_query ($conn, $AddQuery);
}
?>
<?php
$result = mysqli_query($conn, "SELECT*FROM tbl_Albums");
?> <table border="1">
<tr>
<th>Album ID</th>
<th>Album Name</th>
<th>Number of Tracks</th>
<th>Genre</th>
<th>Artist ID</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<form method = "POST">
<tr>
<td><input type="number" name="albumid" value="<?php echo $row ['Album_id']; ?>" /></td>
<td><input type="text" name="albumname" value="<?php echo $row['Album_Name']; ?>" /></td>
<td><input type="number" name="numberoftracks" value="<?php echo $row['Number_Of_Tracks']; ?>" /></td>
<td><input type="text" name="genre" value="<?php echo $row['Genre']; ?>"/></td>
<td><input type="number" name="artistid" value="<?php echo $row['Artist_id']; ?>" /></td>
<td><input type="hidden" name="hidden" value="<?php echo $row['Album_id']; ?>"/></td>
<td><input type="submit" name="update" value="Update" /></td>
<td><input type="submit" name="delete" value="Delete"/></td>
</tr>
</form>
<?php
}
?>
<form method="POST">
<tr>
<td></td>
<td><input type="text" name="ualbumname" /></td>
<td><input type="text" name="unumberoftracks" /></td>
<td><input type="text" name="ugenre" /></td>
<td><input type="text" name="uartistid" /></td>
<td><input type="submit" name="add" value="Add"/></td>
</tr>
</form>
</table>
<?php
mysqli_close($conn);
?>
<p>Return to main page</p>
Where am I going wrong? I'm trying my utmost best to call on my declared rows and use them within the query but all I get is a refreshed page with no change to the data from the rows
If all your query is perfect then this will run perfectly.
Update Block
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE tbl_Albums SET Album_Name='{$_POST['albumname']}',
Number_Of_Tracks='{$_POST['numberoftracks']}', Genre='{$_POST['genre']}',
Artist_id='{$_POST['artistid']}' WHERE Album_id='{$_POST['hidden']}'";
mysqli_query($conn, $UpdateQuery);
}
Delete Block
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM tbl_Albums WHERE Album_id='$_POST[hidden]'";
mysqli_query($conn, $DeleteQuery);
}
Add Block
if(isset($_POST['add'])){
$uartistname = $_POST['uartistname'];
$unumberoftracks = $_POST['unumberoftracks'];
$ugenre = $_POST['ugenre'];
$uartist = $_POST['uartist'];
$AddQuery = "INSERT INTO tbl_Album (Album_Name, Number_Of_Tracks, Genre, Artist_id) VALUES('$uartistname', $unumberoftracks, '$ugenre', $uartist)";
mysqli_query($conn, $AddQuery);
}
Dynamic HTML Block
$result = mysqli_query($conn, "SELECT*FROM tbl_Albums");?>
<table border="1">
<tr>
<th>Album ID</th>
<th>Album Name</th>
<th>Number of Tracks</th>
<th>Genre</th>
<th>Artist ID</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<form method="POST" action="">
<td><input type="number" name="albumid" value="<?php echo $row['Album_id']; ?>" /></td>
<td><input type="text" name="albumname" value="<?php echo $row['Album_Name']; ?>" /></td>
<td><input type="number" name="numberoftracks" value="<?php echo $row['Number_Of_Tracks']; ?>" /></td>
<td><input type="text" name="genre" value="<?php echo $row['Genre']; ?>"/></td>
<td><input type="number" name="artistid" value="<?php echo $row['Artist_id']; ?>" /></td>
<td><input type="hidden" name="hidden" value="<?php echo $row['Album_id']; ?>"/></td>
<td><input type="submit" name="update" value="Update" /></td>
<td><input type="submit" name="delete" value="Delete"/></td>
</form>
</tr>
<?php }?>
<tr>
<form method="POST" action="">
<td></td>
<td><input type="text" name="ualbumname" /></td>
<td><input type="text" name="unumberoftracks" /></td>
<td><input type="text" name="ugenre" /></td>
<td><input type="text" name="uartistid" /></td>
<td><input type="submit" name="add" value="Add"/></td>
</form>
</tr>
</table>
This is just the re-representation of the Code. Let me know if anything wrong with this.
I have the following list of variables and I am trying to pass these to the sql UPDATE statement.
But the update statement is not working, I realised that the ID value is not detected by the sql statement.
How should I go about doing it?
The form:
<table class="table table-striped table-bordered table-hover" id="dataTables-example" data-pagination="true">
<form name="form1" method="post" action="processUpdateAccount.php">
<thead>
<tr>
<th>Name</th>
<th>Account</th>
<th>Email Address</th>
<th>Contact</th>
<th>Country</th>
<th>Town</th>
<th>Address</th>
<th>Postal Code</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr class="gradeU">
<td>
<input name="userName" type="text" id="userName" value="<? echo $rowsUsers['name']; ?>" required>
</td>
<td>
<input name="userAccount" type="text" id="userAccount" value="<? echo $rowsUsers['account']; ?>" required>
</td>
<td>
<input name="userEmail" type="text" id="userEmail" value="<? echo $rowsUsers['email']; ?>" required>
</td>
<td>
<input name="userPhone" type="text" id="userPhone" value="<? echo $rowsUsers['phone']; ?>" required>
</td>
<td>
<input name="userCountry" type="text" id="userCountry" value="<? echo $rowsUsers['country']; ?>" required>
</td>
<td>
<input name="userTown" type="text" id="userTown" value="<? echo $rowsUsers['town']; ?>" required>
</td>
<td>
<input name="userAddress" type="text" id="userAddress" value="<? echo $rowsUsers['address']; ?>" required>
</td>
<td>
<input name="userPostalCode" type="text" id="userPostalCode" value="<? echo $rowsUsers['postalCode']; ?>" required>
</td>
<!-- link to updateAccount.php and send value of ID -->
<td>
<input type="submit" name="Submit" value="Update">
</td>
<input name="userId" type="hidden" id="userId" value="<? echo $rows['Id'];?>">
<input name="userPassword" type="hidden" id="userPassword" value="<? echo $rows['password'];?>">
</form>
</tr>
</tbody>
</table>
processUpdateAccount.php
if (isset($_POST['userId']) > 0)
{
// query db
$Id = $_POST['userId'];
$name = $_POST['userName'];
$account = $_POST['userAccount'];
$email = $_POST['userEmail'];
$phone = $_POST['userPhone'];
$country = $_POST['userCountry'];
$town = $_POST['userTown'];
$address = $_POST['userAddress'];
$postalCode = $_POST['userPostalCode'];
$password = $_POST['userPassword'];
$sql="UPDATE users SET email='$email', password='$password', account='$account', phone='$phone', country='$country', town='$town', address='$address', postalCode='$postalCode', name='$name' WHERE id='$Id'";
$result=mysql_query($sql);
$result = mysql_query("UPDATE users SET name='testing' WHERE Id='28'");
if($result)
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=manageAccounts.php?msg=1">';
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=updateAccount.php?Id=' . $Id .'&msg=2">';
}
}
You are fetching everything else from $rowsUsers and id from $rows so that might be the very typo which keeps your id empty.
echo $rows['Id'];
Most probably is
echo $rowUsers['Id'];
Use PDO - it's much safer than the approach you're taking. This page is a good introduction. In particular look at the section on Prepared Statements. So in your case, after connecting to the database, you'd have something like:
$data = array('name'=>$name,'account'=>$account, [...] , 'id' => $id);
$sth = $db->("UPDATE users SET name = :name, account = :account WHERE id = :id");
$sth->execute($data);
Im trying to make an update form using php and updating the sql database but it seems not to work here. I tried updating the form and then it should say that its updated but it doesn't want to work. I need help please. Thanks
<?php
include 'connections/connection.php';
$updateinfo = #$_POST['update'];
$get_info = mysql_query("SELECT * FROM customer");
$id = #$_REQUEST['id'];
$get_row = mysql_fetch_assoc($get_info);
$db_cname = $get_row['customer_name'];
$db_cemail = $get_row['customer_email'];
$db_caddress = $get_row['customer_address'];
$db_cphone = $get_row['customer_phone'];
if ($updateinfo) {
$cname = strip_tags(#$_POST['cname']);
$cemail = strip_tags(#$_POST['cemail']);
$caddress = strip_tags(#$_POST['caddress']);
$cphone = strip_tags(#$_POST['cphone']);
//Submit the form to the database
$info_submit_query = mysql_query("UPDATE customer SET customer_name='$cname', customer_email='$cemail', customer_address='$caddress', customer_phone='$cphone' WHERE id='$id'") or die("Error! " . mysql_error());
echo "updated!";
}
else
{
echo "error " . mysql_error();
}
?>
<head>
<title>Customer Modification</title>
</head>
<body>
<table border="0" cellpadding="10" cellspacing="5" width="30%" style="margin-top:100px;" align="center" bgcolor="#FFB13C">
<form action="edit.php" method="post">
<tr>
<td><b>Customer Name:*</b></td>
<td><input type="text" size="50" name="cname" value="<?php echo $db_cname; ?>"></td>
</tr>
<tr>
<td><b>Customer Email:*</b></td>
<td><input type="text" size="50" name="cemail" value="<?php echo $db_cemail; ?>"></td>
</tr>
<tr>
<td><b>Customer Address:*</b></td>
<td><input type="text" size="50" name="caddress" value="<?php echo $db_caddress; ?>"></td>
</tr>
<tr>
<td><b>Customer Phone:*</b></td>
<td><input type="text" size="50" name="cphone" value="<?php echo $db_cphone; ?>"></td>
</tr>
<tr>
<td><input type="reset" name="reset" value="Reset"></td>
<td><input type="submit" name="update" value="Edit"></td>
</tr>
</form>
</table>
</body>