why edit.php dont work? - php

I have a problem with the editing data on uploaded to MySQL.
Here is my edit.php
<html>
<head>
</head>
<body>
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?
include "dbconfig.php";//database connection
error_reporting(E_ALL);
$query = "SELECT * FROM users where id='$id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
?>
<form method="post" action="updatepengguna.php">
<input type="hidden" name="id" value="<?php echo "$row[id]"?>">
<tr>
<td>Username</td>
<td>
<input type="text" name="username"
size="20" value="<?php echo "$row[username]"?>">
</td>
</tr>
<tr>
<td>Nama Lengkap</td>
<td>
<input type="text" name="namalengkap" size="40"
value="<?php echo "$row[namalengkap]"?>">
</td>
</tr>
<tr>
<td>NIK</td>
<td>
<input type="text" name="nik"
size="20" value="<?php echo "$row[nik]"?>">
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="text" name="password"
size="20" value="<?php echo "$row[password]"?>">
</td>
</tr>
<tr>
<td>Level</td>
<td>
<input type="text" name="level"
size="20" value="<?php echo "$row[level]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
Here I have updatepengguna.php
<?
include "dbconfig.php";
$id = $_POST['id'];
$username = $_POST["username"];
$namalengkap = $_POST["namalengkap"];
$nik= $_POST["nik"];
$password= $_POST["password"];
$level= $_POST["level"];
$query = "UPDATE users SET username='$username', namalengkap='$namalengkap', nik='$nik', password='$password', level='$level' WHERE id='$id'";
mysql_query($query);
echo mysql_error();
header("location:daftarpengguna.php");
?>
I get this error:
Notice: Undefined variable: id in C:\xampp\htdocs\aes\editpengguna.php on line 20
What's the problem?

Okay so lets go through the code and your error message you have 20 lines of code:
<html>
<head>
</head>
<body>
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?
include "dbconfig.php";//database connection
error_reporting(E_ALL);
$query = "SELECT * FROM users where id='$id'";
I presume include "dbconfig.php"; has three lines of code.
So line 20 is
$query = "SELECT * FROM users where id='$id'";
So your error message
Notice: Undefined variable: id in C:\xampp\htdocs\aes\editpengguna.php on line 20
is telling you that $id is not defined. Which if you look at the previous 20 lines of code it is not.
Maybe you meant to do
$id = (int)$_GET['id'];
earlier in your code? Possibly POST but that is the error.
You also should look into using PDO or mysqli as your driver. The mysql_ functions are deprecated and don't support prepared/paramaterized queries. You could use http://php.net/manual/en/function.mysql-real-escape-string.php. Since you are learning I'd recommend learning with a live and up to date driver though.

Related

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.

SQL not updating Row

I have tried to get this working for a couple hours now, i think its related to the fact that i have a $_GET['ID']; on the first script but im not sure:
Script 1 (FORM):
<?php
require_once('db_access.php');
$editID = $_GET['id'];
$query = mysql_query("SELECT * from routes where id = '".$editID."'");
$row = mysql_fetch_assoc($query);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>Route Edit Data</td>
</tr>
<tr>
<td>
<table>
<form method="post" action="complete_edit.php">
<tr>
<td>ID #</td>
<td>
<input type="hidden" name="formid" value="<?php echo $row['id'] ?>">
</td>
</tr>
<tr>
<td>Route Name</td>
<td>
<input type="text" name="route_title" size="40"
value="<?php echo $row['route_title']?>">
</td>
</tr>
<tr>
<td>Total Price</td>
<td>
<input type="text" name="total_price" size="40"
value="<?php echo $row['total_price']?>">
</td>
</tr>
<tr>
<td>Down Payment</td>
<td>
<input type="text" name="down_payment" size="40"
value="<?php echo $row['down_payment']?>">
</td>
</tr>
<tr>
<td>Weekly Net</td>
<td>
<input type="text" name="weekly_net" size="40"
value="<?php echo $row['weekly_net']?>">
</td>
</tr>
<tr>
<td>Location</td>
<td>
<input type="text" name="location" size="40"
value="<?php echo $row['location']?>">
</td>
</tr>
<tr>
<td>Remarks</td>
<td>
<input type="text" name="remarks" size="40"
value="<?php echo $row['remarks']?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
SCRIPT 2(PROCESSING):
<?php
$id = $_POSt['formid'];
$editroute = $_POST['route_title'];
$editprice = $_POST['total_price'];
$editdownpay = $_POST['down_payment'];
$editweeklynet = $_POST['weekly_net'];
$editlocation = $_POST['location'];
$editremarks = $_POST['remarks'];
$query = "UPDATE routes SET id = '$id', route_title = '$editroute', total_price = '$editprice', down_payment = '$editdownpay', weekly_net = '$editweeklynet', location = '$editlocation', remarks = '$editremarks' WHERE id = '$id'";
header('Location:index.php');
?>
The first lot of code is where my form is placed and the second is where the processing happens
Thanks for your help people :)
Alex
$id = $_POSt['formid'];
$_POSt is not $_POST
Im so silly after reading the comments to the question here i realised that i had no mysql_query string :)
Thanks guys for giving me my mind haha :)
Alex

Update failed after edit the data [no error]

I have made a edit form...but when i press the edit button..it works...when after complete the edit if i press the update button...it goes to main page but no field updated.
edit.page
<body>
<table align="center">
<tr>
<td align="center">Edit data</td>
</tr>
<tr>
<td>
<table border="2">
<th>SL</th>
<th>name</th>
<th>address</th>
<th>action</th>
<?php
include"dbc.php";//database conncetion
$order = "select * from tbl_record";
$result = mysqli_query($con,$order);
while ($row=mysqli_fetch_array($result)){
echo ("<tr><td>$row[employees_number]</td>");
echo ("<td>$row[name]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>Edit</td></tr>");
}
mysqli_close($con);
?>
</table>
</td>
</tr>
</table>
</body>
</html>
edit form
<body>
<table border=2>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include "dbc.php";//database connection
$id = $_GET["id"];
$order = "SELECT * FROM tbl_record where employees_number='$id'";
$result = mysqli_query($con,$order);
$row = mysqli_fetch_array($result);
?>
<form method="post" action="edit_data.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>
</table>
</td>
</tr>
</table>
</body>
update page
<?php
//edit_data.php
include "dbc.php";
if (isset($_POST['submit']))
{
$id = $_GET['id'];
$name = $_POST["name"];
$address = $_POST["address"];
mysqli_query("UPDATE tbl_record SET name='$name', address='$address' WHERE employees_number='$id'")
or die(mysqli_error());
}
header("location:edit.php");
?>
As per your comment "i am getting this error now mysqli_query() expects at least 2 parameters, 1 given ...for update page"
Add your connection parameter to the query:
mysqli_query("UPDATE
to that it reads as
mysqli_query($con, "UPDATE ...
Plus, your submit button should read as:
<input type="submit" name="submit" value="Edit">
The name="submit value" in it now, doesn't match the conditional statement for it
if (isset($_POST['submit']))

avoiding duplicate entries and caching entered form values

i have some code that controls duplicate entries in particular the USER ID. it checks in the database at submit and if that USER ID exists already it gives that notification. now the problem is when i submit and if that USER ID entered already exists in the database, all the other entries on the form are cleared, prompting me to re_enter all the other details again. i find this annoying and retrogressive. i want some help on how better i can do it such that only the USER ID text box returns empty, keeping other details safe/unchanged or indeed alternatively keeping/buffering/caching all details previously entered so that i can also review the duplicate USER ID before changing it.
new_user.php
<h1 align="center">Create New User</h1>
<p align="center" style="color:#F00"><?php if(isset($_GET['dup'])){ echo "That User ID Already Exists!"; } ?> </p>
<form id="form1" method="post" action="add_user.php">
<table width="100%">
<tr>
<td width="204"><div align="right">User ID:</div></td>
<td width="286">
<input type="text" name="user_id" id="user_id" />
</td>
</tr>
<tr>
<td><div align="right">Full Names:</div></td>
<td>
<input type="text" name="fname" id="fname" />
</td>
</tr>
<tr>
<td><div align="right">Gender:</div></td>
<td><select id="sex" name="sex">
<option selected="selected" value="male">Male</option>
<option name="female">Female</option>
</select></td>
</tr>
<tr>
<td><div align="right">NRC Number:</div></td>
<td>
<input type="number" name="nrcno" id="nrcno" min="1" />
</td>
</tr>
<tr>
<td><div align="right">Phone Number:</div></td>
<td>
<input type="number" name="cellno" id="cellno" />
</td>
</tr>
<tr>
<td><div align="right">Email Address:</div></td>
<td>
<input type="email" name="emailad" id="emailad" />
</td>
</tr>
<tr>
<td><div align="right">Position Held:</div></td>
<td>
<input type="text" name="posh" id="posh" />
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="create" id="create" value="Add User" /></td>
</tr>
</table>
</form>
add_user.php
<?php
$user_id=$_POST['user_id'];
$fname = $_POST['fname'];
$sex= $_POST['sex'];
$name= $_POST['name'];
$nrcno = $_POST['nrcno'];
$cellno= $_POST['cellno'];
$emailad = $_POST['emailad'];
$posh = $_POST['posh'];
require("get_func.php");
checkID($id);
include("connect.php");
mysql_select_db("ceec", $con);
$query = "INSERT INTO user VALUES ('$user_id', '$fname', '$sex','$name', '$nrcno', '$cellno', '$emailad', '$posh')";
if (mysql_query($query)){
header("Location: success.php");
}
else {echo "Nada" . mysql_error(); }
mysql_close($con);
?>
get_func.php
<?php
function checkID($id){
include_once("connect.php");
mysql_select_db("ceec",$con);
$query = "SELECT * FROM user WHERE user_id = '$id'";
$result= mysql_query($query);
if($row = mysql_fetch_array($result))
{
header("Location: new_user.php?dup=true");
break;
}
else {}
}
?>
<input type="text" name="user_id" id="user_id"
<?php if(isset($_POST['user_id'])){echo htmlentities($_POST['user_id'];} ?>/>

MySql php data display in tables

I am doing a simple set of PHP scripts to edit and return MySQL records from a web site.
Everything works fine but there is a cosmetic that I just cannot seem to correct.
I presume being very rusty I am missing something obvious - I have tried everything I can think of though.
The content of field ADDTEXT can be fairly large and i would like to word wrap it all into the table cell. This script truncates it when a single line length is exceeded.
And yes I know I should be using mysqli_... but I am deaeling with that !
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>EDIT NEWS ITEM</td>
</tr>
<tr>
<td>
<table style="width:100%">
<tr>
<?
$id=$_GET['id'];
include "D***************.uk\public_html\html\ConnectDB.php";//database connection
$order = "SELECT * FROM st¬¬¬¬¬¬¬ where TYPE = '".$id."'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<? echo "$row[TITLE]"?>">
<tr>
<td>Item Title</td>
<td>
<input type="text" name="title"
value="<? echo "$row[TITLE]"?>">
</td>
</tr>
<tr>
<td>Item Text</td>
<td>
<input type="text" name="text"
value="<? echo "$row[ADDTEXT]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
Change it to textarea field.
<textarea name="text"><?php echo $row['ADDTEXT'];?></textarea>
Try it
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>EDIT NEWS ITEM</td>
</tr>
<tr>
<td>
<table style="width:100%">
<tr><td>
<?
$id=$_GET['id'];
include "D***************.uk\public_html\html\ConnectDB.php";//database connection
$order = "SELECT * FROM st¬¬¬¬¬¬¬ where TYPE = '".$id."'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo $row['TITLE']?>">
<tr>
<td>Item Title</td>
<td>
<input type="text" name="title"
value="<?php echo $row['TITLE']?>">
</td>
</tr>
<tr>
<td>Item Text</td>
<td>
<input type="text" name="text"
value="<?php echo $row['ADDTEXT']?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>

Categories