I am having an issue editing mysql database through a html table.
when I run a simple while loop it works, I can pull in the data and update the database:
Working Code
<?php
while ($row = mysqli_fetch_array($res))
echo "$row[id]. $row[Key_Role] .$row[Incumbant] .$row[Attrition_Risk] .$row[Ready_Now] .$row[lowerYears] .$row[higherYears]<a href='edit.php?edit=$row[id]'>edit</a> <br />";
?>
However, when I run the php through a table, the edit links no longer pull in the data from mysql and I am not able to update information. I have put below the index and edit php files.
Index.php
<?php
include_once('db.php');
if(ISSET($_POST['Key_Role']))
{
$Key_Role = $_POST['Key_Role'];
$Incumbant = $_POST['Incumbant'];
$Attrition_Risk = $_POST['Attrition_Risk'];
$Ready_Now = $_POST['Ready_Now'];
$lowerYears = $_POST['1-2_Years'];
$higherYears = $_POST['3-5_Years'];
$sql = "INSERT INTO tmdata VALUES('','$Key_Role','$Incumbant','$Attrition_Risk','$Ready_Now','$lowerYears','$higherYears')";
$res = mysqli_query($conn, $sql);
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
if ($res)
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
else
echo "Failed";
} else {
echo "please enter a key Role";
}
$res = mysqli_query($conn, "SELECT * FROM tmdata");
?>
<style>
<?php include 'style.css' ?>
</style>
<H1 class="Title">Talent Management System</H1>
<form action="." method="post">
Key Role:<input type="text" name="Key_Role">
Incumbant:<input type="text" name="Incumbant">
Attrition_Risk:<input type="text" name="Attrition_Risk">
Ready_Now:<input type="text" name="Ready_Now">
1-2_Years:<input type="text" name="1-2_Years">
3-5_Years:<input type="text" name="3-5_Years">
<input type ="submit" value="Enter">
</form>
<h1> List Of Key Roles</h1>
<?php
/*
while ($row = mysqli_fetch_array($res))
echo "$row[id]. $row[Key_Role] .$row[Incumbant] .$row[Attrition_Risk] .$row[Ready_Now] .$row[lowerYears] .$row[higherYears]<a href='edit.php?edit=$row[id]'>edit</a> <br />";
*/
?>
<table>
<tr>
<th>id</th>
<th>Key_Role</th>
<th>Incumbant</th>
<th>Attrition_Risk</th>
<th>Ready_Now</th>
<th>1-2_Years</th>
<th>3-5_Years</th>
<th>Edit</th>
</tr>
<?php while ($row = mysqli_fetch_array($res)):;?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['Key_Role'];?></td>
<td><?php echo $row['Incumbant'];?></td>
<td><?php echo $row['Attrition_Risk'];?></td>
<td><?php echo $row['Ready_Now'];?></td>
<td><?php echo $row['lowerYears'];?></td>
<td><?php echo $row['higherYears'];?></td>
<td><a href='edit.php?edit=$row["id"]'>edit</a></td>
</tr>
<?php endwhile;?>
</table>
Edit.php
<?php
include_once('db.php');
if (isset($_GET["edit"]))
{
$id = $_GET["edit"];
$res = mysqli_query($conn, "SELECT * FROM tmdata WHERE id='".$id."'");
$row = mysqli_fetch_array($res);
}
if( isset($_POST['newKey_Role']) || isset($_POST['newIncumbant']) || isset($_POST['newAttrition_Risk']) || isset($_POST['newReady_Now']) || isset($_POST['newLowerYears']) || isset($_POST['newHigherYears']) )
{
$newKey_Role = $_POST['newKey_Role'];
$newIncumbant = $_POST['newIncumbant'];
$newAttrition_Risk = $_POST['newAttrition_Risk'];
$newReady_Now = $_POST['newReady_Now'];
$newLowerYears = $_POST['newLowerYears'];
$newHigherYears = $_POST['newHigherYears'];
$id = $_POST['id'];
$sql = "UPDATE tmdata SET Key_Role='$newKey_Role', Incumbant='$newIncumbant', Attrition_Risk='$newAttrition_Risk', Ready_Now='$newReady_Now', lowerYears='$newLowerYears', higherYears='$newHigherYears' WHERE id='".$id."'";
$res = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
?>
<form action="edit.php" method="post">
Key Role:<input type="text" name="newKey_Role" value ="<?php echo $row[1]; ?>"></input> <br />
Incumbant:<input type="text" name="newIncumbant" value ="<?php echo $row[2]; ?>"></input> <br />
Attrition_Risk:<input type="text" name="newAttrition_Risk" value ="<?php echo $row[3]; ?>"></input> <br />
Ready_Now:<input type="text" name="newReady_Now" value ="<?php echo $row[4]; ?>"></input> <br />
1-2 Years:<input type="text" name="newLowerYears" value ="<?php echo $row[5]; ?>"></input> <br />
3-5 Years:<input type="text" name="newHigherYears" value ="<?php echo $row[6]; ?>"></input> <br />
<input type="hidden" name="id" value ="<?php echo $row[0]; ?>">
<input type ="submit" value="Update">
</form>
<style>
<?php include 'style.css' ?>
</style>
It looks as though the ID is not pulling across to the edit php. I also tried manually inputting the id in the table:
<td><a href='edit.php?edit=$row[184]'>edit</a></td>
but this also did not work.
Any help would be very much appreciated.
Thank you in advance.
I figured out that you need to put the link in php.
<td><?php echo "<a href='edit.php?edit=$row[id]'>edit</a>"?></td>
That worked in the end.
Related
So I've been doing this school project and need to make it so I can edit whatever is in the table. Whenever i click on "Edit" it redirects me correctly but in the form it says there is an undefined variable eventhough that variable is used pretty much everywhere.
Here is some code of the table:
<table style='margin-left:auto ; margin-right:auto;'>
<tr>
<th>#</th>
<th>Name</th>
<th>Zeit</th>
<th>Datum</th>
<th>Titel</th>
<th>Inhalt</th>
<th>Ort</th>
</tr>
<?php
if($stmt=$db->prepare("SELECT * FROM terminkalender")) {
$stmt->execute();
$stmt->store_result();
$zeilen = $stmt->num_rows();
$stmt->close();
}else {
$zeilen = 0;
}
if($zeilen > 0) {
//nur wenn Einträge, dann ausgeben
if($stmt = $db->prepare("SELECT * FROM terminkalender ORDER BY zeit,datum DESC")) {
$stmt->execute();
$stmt->bind_result($id,$name,$zeit,$datum,$ort,$titel,$inhalt);
$stmt->store_result();
//Ausgabe starten
while($stmt->fetch()){
echo "<tr>";
?>
<td><?php echo $id ;?></td>
<td><?php echo htmlspecialchars($name) ;?></td>
<td><?php echo htmlspecialchars($datum) ;?></td>
<td><?php echo htmlspecialchars($zeit) ;?></td>
<td><?php echo htmlspecialchars($ort) ;?></td>
<td><?php echo htmlspecialchars($titel) ;?></td>
<td><?php echo htmlspecialchars($inhalt); ?></td>
<td><a href='edit.php?id=<?php echo $id;?>'>Edit</a></td>
<td><a href='delete.php?id=<?php echo $id;?>'>Delete</a></td>
<?php
echo "</tr>" ;
}
}
}
?>
</table>
and here for the edit.php file:
<?php
include("./config/connect.inc.php");
$id = $_GET['id']; // get id through get string
$result=mysqli_query($db,"SELECT * FROM terminkalender WHERE id=$id");
if(isset($_POST['update'])) {
$name=$_POST['name'];
$datum=$_POST['datum'];
$zeit=$_POST['zeit'];
$ort=$_POST['ort'];
$titel=$_POST['titel'];
$inhalt=$_POST['inhalt'];
$result = "UPDATE terminkalender
SET name='$name',
datum='$datum',
zeit='$zeit',
ort='$ort',
titel='$titel',
inhalt='$inhalt'
WHERE id=$id";
header("location: ausgabe.php");
}
?>
<form name="form" method="POST" action="edit.php">
<input type="text" name="name" value="<?php echo $name; ?>" Required>
<input type="date" name="datum" value="<?php echo $datum; ?>" Required>
<input type="time" name="zeit" value="<?php echo $zeit; ?>" Required>
<input type="text" name="ort" value="<?php echo $ort; ?>" Required>
<input type="text" name="titel" value="<?php echo $titel; ?>" Required>
<input type="text" name="inhalt" value="<?php echo $inhalt; ?>" Required>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
<input type="submit" name="update" value="Update">
</form>
Ẁould be really awesome if anyone could help. Thanks is advance!
Maybe you can try using isset function of php to check if that variable contains a value or not for more details check:-https://www.stechies.com/notice-undefined-variable-in-php/
I am still trying out PHP and with database capability. I'm new to stack overflow as well.
I'm having a problem with updating and deleting a row in my database but for now updating is the concern and surely deletion will follow if I manage to get assisted for this.
I have database "dbSample" and it has 3 columns "id, name, address, email".
I am having problems redirecting correctly to my update classes and it shows an error every time.
Below is the said connection file:(sample/class/dbconnection.php):
<?php
/*
==============================SQL Connection=================================
*/
$connections = mysqli_connect("localhost","root","","dbSample"); //checks database connection
if(mysqli_connect_errno()){
echo '<script type="text/javascript">alert("' . "Database Status: " . mysqli_connect_error() . '")</script>';
}
?>
Below serves as my index php file(sample/dbsample.php):
<?php require 'class/dbconnection.php';?>
<html>
<head>
</head>
<body>
<div>
<?php include 'class/dbupdate.php'; ?>
</div>
</body>
</html>
Below is the said dbupdate file:(sample/class/dbupdate.php):
<?php
/*
==============================SQL Update=================================
*/
$view_query = mysqli_query($connections, "SELECT * FROM tblSample");
echo "<table border = '1'>";
echo "<tr>
<td>Name</td>
<td>Address</td>
<td>Email</td>
<td>Option</td>
</tr>";
while($row = mysqli_fetch_assoc($view_query)){ //make variables to hold the values from the table
$user_id = $row["id"];
$db_name = $row["name"];
$db_address = $row["address"];
$db_email = $row["email"];
//get the value id and pass it
echo "<tr>
<td>$db_name</td>
<td>$db_address</td>
<td>$db_email</td>
<td><a href='class/updatepass.php?id=$user_id'>Update</a></td>
</tr>";
}
echo "</table>";
?>
Below is the said updatepass file:(sample/class/updatepass.php):
<?php
require_once(__DIR__."\dbconnection.php");
$user_id = $_REQUEST["id"];
$get_record = mysqli_query($connections, "SELECT * FROM tblSample WHERE id='$user_id'");
while($row_edit = mysqli_fetch_assoc($get_record)){
$db_name = $row_edit["name"];
$db_address = $row_edit["address"];
$db_email = $row_edit["email"];
}
?>
<form method="POST" action="class/updatenow.php">
<input type="hidden" name="user_id" value="<?php echo $user_id; ?>">
Name: <input type="text" name="new_name" value="<?php echo $db_name; ?>">
<hr />
Address: <input type="text" name="new_address" value="<?php echo $db_address; ?>">
<hr />
Email: <input type="text" name="new_email" value="<?php echo $db_email; ?>">
<hr />
<input type="submit" value="Update">
</form>
Below is the said updatenow file:(sample/class/updatenow.php):
<?php
header('Content-Type: text/plain; charset=utf-8');
require_once(__DIR__."\class\updatepass.php");
require_once(__DIR__."\class\dbconnection.php");
$user_id = $_POST["id"];
$new_name = $_POST["new_name"];
$new_address = $_POST["new_address"];
$new_email = $_POST["new_email"];
mysqli_query($connections, "UPDATE tblSample SET name='$new_name', address='$new_address', email='$new_email' WHERE id='$user_id'");
echo '<script type="text/javascript">alert("' . "Record has been updated" . '")</script>';
header('location: dbsample.php');
?>
Thank you for the help in advance, I will deeply appreciate it.
I have this form and out of this form, I can get 2 values from the radio buttons with post. But, I want to able to send another value winch is in my while loop the $fieldname variable with my form. I just don't know how to do that.
This my code:
$result = mysqli_query($con,"SELECT * FROM Velden");
while($row = mysqli_fetch_array($result)) {
echo "<div>";
echo "<h1>".$row['name']."</h1>";
echo "<h3>".$row['locatie']."</h3>";
echo '<img src="images/'.$row['photo'].'" width="120px" height="120px"/>';
echo "<p>".$row['aanwezig']."</p>";
$namefield = $row['name'];
$players = mysqli_query($con, "SELECT name, user_status FROM veld_user WHERE user_status=1 AND name='$namefield'");
echo "veld: ".$row['name']."<br />";
$number = mysqli_num_rows($players);
echo "Aantal spelers aanwezig: ".$number."<br /><br />";
?>
<form action="" method="post" id="registerForm">
<table class="form imageFrom">
<tr>
<td><input checked type="radio" name="status" value="1"/> aanwezig</td> <?php if (isset($_POST['status']) && $_POST['status']=='1') echo ' STATUS="aanwezig"';?>
<td><input checked type="radio" name="status" value="0"/> afwezig</td><?php if (isset($_POST['status']) && $_POST['status']=='0') echo ' STATUS="afwezig"';?>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit" class="knop"/></td>
</tr>
</table>
</form><?php
echo"</div>";
}
And this is the code were i get the post. And update my database
if(isset($_POST['submit'])){
if (isset($_POST['status']) && $_POST['status']=='1'){
$sql = "UPDATE veld_user SET user_status = 1 WHERE id=".$user->data()->id;}
elseif (isset($_POST['status']) && $_POST['status']=='0'){
$sql = "UPDATE veld_user SET user_status = 0 WHERE id= ".$user->data()->id;}
if (mysqli_query($con, $sql)) {
Session::flash('home', 'update success');
} else {
echo "Error updating record: " . mysqli_error($con);
}}
Btw thnx guys...input hidden made it work
<input type="hidden" name="fieldname" value="<?php echo $namefield?>" />
Im actually trying to add only a specific row from the table product to the table product_add but the last row is being inserted in the table product_add. What i want is that when I click on the button ADD that specific row is being inserted in the table product_add. I think that the code is not considering the if (isset($_REQUEST['submit'])) part.
<?php
include'connect.php';
$image = isset($_GET['image']) ? $_GET['image'] : "";
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$price= isset($_GET['price']) ? $_GET['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<form method="POST" id="form" name="form">
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
extract($row);
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<input id="submit" type="submit" name="submit" value='ADD'/>
</td>
</tr>
<?php
}
?>
</table>
</form>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
basically it a general coding issue
type submit and also name submit is conflicting
first add an attribute in form tag enctype="multipart/form-data" for image or video upload
then
creat a hidden field named 'something'
like
<input type="hidden" name="something" />
and after that use this
isset($_REQUEST['something']
add from in each while
like this updated code
<?php
include'connect.php';
$image = isset($_REQUEST['image']) ? $_REQUEST['image'] : "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "";
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : "";
$price= isset($_REQUEST['price']) ? $_REQUEST['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<form method="POST" action="" >
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="name" value="<?php echo $row['name']; ?>" />
<input type="hidden" name="image" value="<?php echo $row['image']; ?>" />
<input type="hidden" name="price" value="<?php echo $row['price']; ?>" />
<input id="submit" type="submit" name="submit" value='ADD'/>
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
NOTE: mysql_* is deprecated use mysqli_* or PDO
Working on deleting an entry from my database table.
Currently the delete is working. However it deletes the "id + 1"
My script populates a "delete " button right next to an entry populated from mysql table.
When I hit delete next to an entry, it deletes the item +1.
I think it has to do with the while loop, the way I populate items.
Any ideas on how to fix this?
<?php
while($row = mysqli_fetch_array($result)) : ?>
<tr>
</table border = '1'><br><table border = '1'><th>Subject</th><th>Sender</th> <th>Message</th><th>Delete</th>
<?php
echo "<tr><td>";
echo $row['subject'];
echo "<td>";
echo $row['sender'];
echo "<td>";
echo $row['msgText'];
echo "<td>";
?>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php $_SESSION['id'] = $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</tr>
<?php endwhile; ?>
here is the script in delete.php
<?php
session_start();
$id = $_SESSION['id'];
$con=mysqli_connect("localhost","root","","something");
$sql = "DELETE FROM `mailbox` WHERE `id`= '$id'";
$result = mysqli_query($con,$sql);
header('Location: loggedin.php');
?>
<input type="hidden" name="delete_id" value="<?php $_SESSION['id'] = $row['id']; ?>" />
change it to:
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
and in your delete.php, simply replace $_SESSION['id'] to $_POST['delete_id']
P/S: By the way, you've forgot to add session_start(); in your form's php, if you do really want to use it, but not a good idea for this time.
The $_SESSION['id'] gets overridden each time in the loop.
Why not just simply use $_POST["delete_id"] to retrieve row id.
Your Table Structure is wrong
<?php
while($row = mysqli_fetch_array($result)) :
?>
<tr>
<th>Subject</th>
<th>Sender</th>
<th>Message</th>
<th>Delete</th>
</tr>
<tr>
<?php
echo "<td>";
echo $row['subject'];
echo "</td><td>";
echo $row['sender'];
echo "</td><td>";
echo $row['msgText'];
echo "</td>";
?>
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php $_SESSION['id'] = $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
also don't use $_SESSION['id'] while deleting the records. use $_POST['delete_id']