No error shown when updating and deleting - php

I have those codes that help to update and delete a row from a table:
html:
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<form action="update_del.php" method="post">
<input class="imgClass_update" type="submit" name="submit1" value="" />
<input class="imgClass_dell" type="submit" name="submit2" value=""/>
</form>
</td>
</tr>
<tr>
PHP:
<?php
require_once('../include/global.php');
$id=$_POST['id'];
if(isset($_POST['submit1']))
{
$name=$_POST['med_name'];
$info=$_POST['info'];
$sql = "UPDATE med SET med_name='$name',
info='$info'
WHERE id='$id'";
$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
if($result){
header("location:med.php");
}
else
{
header("location:update_false.php");
}
}
if(isset($_POST['submit2']))
{
$sql = "DELETE FROM med WHERE id='$id'";
$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
if(mysqli_affected_rows($con) == 1)
{
header("location:med.php");
}
else
{
header("location:update_false.php");
}
}
?>
Nothing is updated nor deleted. And no errors are shown. If someone could help me, I think the problem is too easy but can't see it where.

You are placing <form> tag at wrong place.
Therefore, your other elements: id, med_name and info are not children of form.
And they are not getting posted.
You should start <form> before table and end after table.
Corrected Code:
<form action="update_del.php" method="post">
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
</tr>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<input class="imgClass_update" type="submit" name="submit1" value="Update" />
<input class="imgClass_dell" type="submit" name="submit2" value="Delete"/>
</td>
</tr>
</table>
</form>

just do this -
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<form action="update_del.php" method="post">
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<input class="imgClass_update" type="submit" name="submit1" value="" />
<input class="imgClass_dell" type="submit" name="submit2" value=""/>
</td>
</tr>
</form>

Related

How to edit data from form field?

when i edit data from form field it only select/fetch one word though form field has many words/ or complete sentence where is wrong?
<?php
//getting id from url
$id = $_GET['id'];
//selecting data associated with this particular id
$result = mysql_query("SELECT * FROM list WHERE id=$id");
while($res = mysql_fetch_array($result))
{
$Test_name = $res['Test_name'];
}
?>
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Test Name</td>
<td><input type="text" name="Test_name" value=<?php echo
$Test_name;?>></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo
$_GET['id'];?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr
</table>
</form>
You can use TextArea for long value edit.
Hope below example will help you.
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Test Name</td>
<td><textarea name="Test_name" value=<?php echo
$Test_name;?>><?php echo
$Test_name;?></textarea></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo
$_GET['id'];?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr
</table>
</form>
Try this.
Use textarea for long value edit.
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Test Name</td>
<td><textarea name="Test_name" value=<?php echo
$Test_name;?>><?php echo
$Test_name;?></textarea></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo
$_GET['id'];?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr
</table>
</form>

Why won't any of my query buttons work on my php 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.

Edit Database in PHP-Site

I'm very new to php and databases. So I need you to help me out please.
I want to edit the data of my database online in my php site. But the form is empty and I don't know why.
I don't know if you need more information so this is the code of the table with the form. If you need more let me know.
<table>
<?php
$con=mysqli_connect("x","y","z","xyz");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Transparente");
while($row = mysqli_fetch_array($result))
mysqli_close($con);
?>
<form method="post" action="edit_data.php">
<input type="text" name="id" value="<? echo "$row[id]"?>">
<tr>
<td>Firma</td>
<td>
<input type="text" name="name"
size="40" value="<? echo "$row[Name]"?>">
</td>
</tr>
<tr>
<td>Wer</td>
<td>
<input type="text" name="wer" size="40"
value="<? echo "$row[Wer]"?>">
</td>
</tr>
<tr>
<td>Erhalten</td>
<td>
<input type="text" name="erhalten" size="40"
value="<? echo "$row[Erhalten]"?>">
</td>
</tr>
<tr>
<td>Digital</td>
<td>
<input type="text" name="digital" size="40"
value="<? echo "$row[Digital]"?>">
</td>
</tr>
<tr>
<td>Betrag in Euro</td>
<td>
<input type="text" name="betrag" size="40"
value="<? echo "$row[Betrag]"?>">
</td>
</tr>
<tr>
<td>Bezahlt am</td>
<td>
<input type="text" name="bezahlt" size="40"
value="<? echo "$row[Bezahlt]"?>">
</td>
</tr>
<tr>
<td>Anmerkung</td>
<td>
<input type="text" name="anmerkung" size="40"
value="<? echo "$row[Anmerkung]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
Try below code
1)If data base contain more rows it shows multiple form
2)You need to show one form you need to restrict in query using where class
<?php
$con=mysqli_connect("x","y","z","xyz");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Transparente");
while($row = mysqli_fetch_array($result))
{
?>
<table>
<form method="post" action="edit_data.php">
<input type="text" name="id" value="<?php echo $row['id'];?>">
<tr>
<td>Firma</td>
<td>
<input type="text" name="name"
size="40" value="<?php echo $row['Name'];?>">
</td>
</tr>
<tr>
<td>Wer</td>
<td>
<input type="text" name="wer" size="40"
value="<?php echo $row['Wer']?>">
</td>
</tr>
<tr>
<td>Erhalten</td>
<td>
<input type="text" name="erhalten" size="40"
value="<?php echo $row['Erhalten']?>">
</td>
</tr>
<tr>
<td>Digital</td>
<td>
<input type="text" name="digital" size="40"
value="<?php echo $row['Digital']?>">
</td>
</tr>
<tr>
<td>Betrag in Euro</td>
<td>
<input type="text" name="betrag" size="40"
value="<?php echo $row['Betrag']?>">
</td>
</tr>
<tr>
<td>Bezahlt am</td>
<td>
<input type="text" name="bezahlt" size="40"
value="<?php echo "$row[Bezahlt]"?>">
</td>
</tr>
<tr>
<td>Anmerkung</td>
<td>
<input type="text" name="anmerkung" size="40"
value="<?php echo $row['Anmerkung'];?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
<?php } ?>
Edit:
change query like below
$result = mysqli_query($con,"SELECT * FROM Transparente where id={$_REQUEST['id']}");
<table>
<?php
$con=mysqli_connect("x","y","z","xyz");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Transparente");
while($row = mysqli_fetch_array($result))
?>
<form method="post" action="edit_data.php">
<input type="text" name="id" value="<? echo "$row[id]"?>">
<tr>
<td>Firma</td>
<td>
<input type="text" name="name"
size="40" value="<? echo "$row[Name]"?>">
</td>
</tr>
<tr>
<td>Wer</td>
<td>
<input type="text" name="wer" size="40"
value="<? echo "$row[Wer]"?>">
</td>
</tr>
<tr>
<td>Erhalten</td>
<td>
<input type="text" name="erhalten" size="40"
value="<? echo "$row[Erhalten]"?>">
</td>
</tr>
<tr>
<td>Digital</td>
<td>
<input type="text" name="digital" size="40"
value="<? echo "$row[Digital]"?>">
</td>
</tr>
<tr>
<td>Betrag in Euro</td>
<td>
<input type="text" name="betrag" size="40"
value="<? echo "$row[Betrag]"?>">
</td>
</tr>
<tr>
<td>Bezahlt am</td>
<td>
<input type="text" name="bezahlt" size="40"
value="<? echo "$row[Bezahlt]"?>">
</td>
</tr>
<tr>
<td>Anmerkung</td>
<td>
<input type="text" name="anmerkung" size="40"
value="<? echo "$row[Anmerkung]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
<?php mysqli_close($con);?>
// close connection at the end of the code
You are not opening the while loop.Try this it will show you the values in the form
<table>
<?php
$con=mysqli_connect("x","y","z","xyz");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Transparente");
while($row = mysqli_fetch_array($result))
{
mysqli_close($con);
?>
<form method="post" action="edit_data.php">
<input type="text" name="id" value="<? echo "$row[id]"?>">
<tr>
<td>Firma</td>
<td>
<input type="text" name="name"
size="40" value="<? echo "$row[Name]"?>">
</td>
</tr>
<tr>
<td>Wer</td>
<td>
<input type="text" name="wer" size="40"
value="<? echo "$row[Wer]"?>">
</td>
</tr>
<tr>
<td>Erhalten</td>
<td>
<input type="text" name="erhalten" size="40"
value="<? echo "$row[Erhalten]"?>">
</td>
</tr>
<tr>
<td>Digital</td>
<td>
<input type="text" name="digital" size="40"
value="<? echo "$row[Digital]"?>">
</td>
</tr>
<tr>
<td>Betrag in Euro</td>
<td>
<input type="text" name="betrag" size="40"
value="<? echo "$row[Betrag]"?>">
</td>
</tr>
<tr>
<td>Bezahlt am</td>
<td>
<input type="text" name="bezahlt" size="40"
value="<? echo "$row[Bezahlt]"?>">
</td>
</tr>
<tr>
<td>Anmerkung</td>
<td>
<input type="text" name="anmerkung" size="40"
value="<? echo "$row[Anmerkung]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
<?php }?>

storing data in an array in php with multiple submission and eco self

Trying to print data from multiple submission, I want the names and roll numbers to get printed/passed to another form. These data will generated from several submissions. In the following code, only the last submission data is getting printed. Where am I going wrong.
`
<?php
$name[] = $_POST['name'];
$rollno[] = $_POST['rollno'];
$arrlength=count($name);
for($x=0;$x<$arrlength;$x++)
{
echo $name[$x]." - - - - - -".$rollno[$x] ;
echo "<br>";
}
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>Name</td>
<td><input type="text" name="name" id="textfield" /></td>
</tr>
<tr>
<td>Roll No.</td>
<td><input type="text" name="rollno" id="textfield2" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Submit" /></td>
</tr>
</table>
</form>
`
Your input types are not array type. Use LIKE THIS
<input type="text" name="name[1]" id="textfield" />
<input type="text" name="name[2]" id="textfield" />
.
.
.
<input type="text" name="name[n]" id="textfield" />
IN PHP CODE
foreach( $_POST['name'] as $v ) {
echo $v;
}
figured it out. here is the revised code.
<?php
$name = $_POST['name'];
$rollno = $_POST['rollno'];
$arrlength=count($name);
for($x=0;$x<$arrlength;$x++)
{
echo $name[$x]."--".$rollno[$x] ;
echo "<br>";
}
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>Name</td>
<td><?php
$arrlength1=count($name);
for($x=0;$x<$arrlength1;$x++)
{
echo "<input type='hidden' name='name[]' value='$name[$x]' /> " ;
echo "<input type='hidden' name='rollno[]' value='$rollno[$x]' /> " ;
}
?>
<input type="text" name="name[]" id="textfield" /></td>
</tr>
<tr>
<td>Roll No.</td>
<td><input type="text" name="rollno[]" id="textfield2" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="button" id="button" value="Submit" /></td>
</tr>
</table>
</form>

Data not loading from MySql into textboxes on button click

i have a little problem here. What i am trying to achieve is User types in unique id into textbox then presses load button, script is looking for this parameter in MySql database. When found match data should be loaded into textbox below.
UPDATED VERSION
<?php
mysql_connect ("localhost", "root","84946dff6e1") or die (mysql_error());
mysql_select_db ("employees");
if(isset($_POST["loadbtn"]))
{
$load = $_POST["loadbtn"];
$sql = mysql_query("SELECT * FROM titles WHERE emp_no = '$load' ");
$details = mysql_fetch_array($sql);
$savedTitle = $details["title"];
}
?>
<form method="post" action="changeTitleView.php">
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center">
<tr>
<td width="150">Employee number</td>
<td><input type="text" name="load" /></td>
<td><input type="submit" name="loadbtn" value="Load" /></td>
</tr>
</table>
</form>
<br />
<br />
<form method="get" action="changeTitleView.php">
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center">
<td width="150">Employee Title</td>
<td><input name="salary" type="text" value="<?php echo $savedTitle; ?>"></td>
</tr>
<td width="150"> </td>
<td>
<input name="add" type="submit" id="add" value="Update">
</td>
</tr>
</table>
</form>
thank you for looking and help :)
Try this
<?php
error_reporting(-1);// show all errors when debugging
// don't use these database commands
//mysql_connect ("localhost", "root","84946dff6e1") or die (mysql_error());
//mysql_select_db ("employees");
// do it this way and don't show us your database credentials.
$conn = new mysqli('localhost', "root", "84946dff6e1", 'employees');
//declare your variables so if POST isn't true you don't have errors later
$load = "some Id";
$savedTitle = "no value yet!"; // use something interesting when testing
//you want the value of the textbox which name is load
if(isset($_POST["load"]))
{
//never trust the user directly
//$load = $_POST["load"];
// do this
$load = $conn->real_escape_string($_POST["load"]);
$result = $conn->query("SELECT * FROM titles WHERE emp_no = '$load' ");
$details = $result->fetch_assoc();
$savedTitle = $details["title"];
// show me error when testing to see if something is wrong with query
echo $conn->error;
}
?>
<form method="post" action="changeTitleView.php">
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center">
<tr>
<td width="150">Employee number</td>
<td><input type="text" name="load" value="<?php echo $load; ?>" /></td>
<td><input type="submit" name="loadbtn" value="Load" /></td>
</tr>
</table>
</form>
<br />
<br />
<form method="get" action="changeTitleView.php">
<table width="400" border="0" cellspacing="1" cellpadding="2" align="center">
<td width="150">Employee Title</td>
<td><input name="salary" type="text" value="<?php echo $savedTitle; ?>"></td>
</tr>
<td width="150"> </td>
<td>
<input name="add" type="submit" id="add" value="Update">
</td>
</tr>
</table>
</form>
You will want to add another if branch to handle the second form which updates the title
And remember that form is GET instead of POST like the first form.
<td><input name="salary" type="text" value="<? echo $empTitle; ?>"></td>
try change that line to this:
<td><input name="salary" type="text" value="<?php echo $empTitle; ?>"></td>

Categories