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']))
Related
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.
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
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>
I have retrieved data from my table and displayed them using the code below.
<?php require_once('../Connections/bidco.php'); ?>
<body>
<table width="671" height="43" border="1" align="center">
<table width="781" height="190" align="center">
<tr>
<td height="61" colspan="4"><div align="center"><strong> Inventory </strong></div></td>
</tr>
<tr>
<td width="77" height="68"><strong>ID</strong></td>
<td width="152"><strong>Item name.</strong> </td>
<td width="253"><strong>unit price</strong> </td>
<td width="253"><strong>Update price</strong></td>
</tr>
<?php
$query=mysql_query("SELECT *FROM manuf ") or die (mysql_error());
while($row=mysql_fetch_array($query))
{
$id=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['itemname']; ?></td>
<td><?php echo $row['unitprice']; ?></td>
<td>change</td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Now this PHP code is supposed to allow me to edit individual rows that have been displayed when i click on 'change' but it it not selecting the row. Any ideas how to solve this?
<?php require_once('../Connections/bidco.php'); ?>
<?php
$id = isset($_GET['id']) ? $_GET['id'] : null;
$query=mysql_query("SELECT * FROM manuf where id='$id' ")or die(mysql_error());
$row=mysql_fetch_array($query);
?>
<form action="updateprice.php" method="post" enctype="multipart/form-data">
<table align="center">
<tr>
<td> <label><strong>Item Name</strong></label></td>
<td> <input type='text' name='itemname' value=" <?php echo $row['itemname']; ?>" />
<input type="hidden" name="id" value="<?php echo $id; ?> " /> <br /></td>
</tr>
<tr>
<td><label><strong>Unit price </strong></label></td>
<td> <input type="text" name="unitprice" value="<?php echo $row['unitprice']; ?> " /><br /></td>
</tr>
<tr>
<td>
<input type="reset" name="Reset" value="CANCEL" />
<br></td>
<td>
<input type="submit" name="Submit2" value="Update" /> </td>
</tr>
</table>
</form>
</body>
</html>
Thank you in advance
Your forget to add you id to your link
<td>change</td>
I think you need
<a href="change.php?id=$row['id']">
Edit - use Arif_suhail_123's answer - I did not notice you have PHP mixed in with HTML
You need to pass rowid to href of Edit Link.
<td>change</td>
You are looking for id in change.php, but you are not sending that in the header. You must change change with
change
Also, I suggest you STOP using mysql_* commands, since the are Depreceted, and will no longer be supported. Use mysqli_* commands instead.
I have the following problem.I have an HTML form with multiple rows. Every row has 4 columns (ID,NAME,LASTNAME,EMAIL). I cannot figure out in my code which is the specific problem that is returned to me:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(name,lastname,email) VALUES ('.('Billly','Blueton1','bb5#phpeasystep.com'),('J' at line 1
I am trying to insert with a single submit query multiple updates. I think I am close to the solution but I am stuck because I am not expert in programming languages. Any help; it would be appreciated.
HERE IS MY CODE :
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("test");
$sql="SELECT * FROM test_mysql";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="" >
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<? $id[]=$rows['id']; ?><?php echo $rows['id'];?>
</td>
<td align="center">
<input name="name[]" type="text" id="name" value='<?php echo $rows['name']; ?>'>
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value='<?php echo $rows['lastname']; ?>'>
</td>
<td align="center">
<input name="email[]" type="text" id="email" value='<?php echo $rows['email']; ?>'>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="submit1" value="ΕΝΗΜΕΡΩΣΗ"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
if(isset($_POST['name'])){
foreach($_POST['name'] as $row=>$Name)
{
$id = intval($rows['id']);
$name = mysql_real_escape_string($Name);
$lastname=mysql_real_escape_string($_POST['lastname'][$row]);
$email = mysql_real_escape_string($_POST['email'][$row]);
$row_data[]="('$name','$lastname','$email')";
$implodeArray = implode(",", $row_data);
}
if(!empty($row_data)){
$query = "UPDATE test_mysql (name,lastname,email) VALUES ('.$implodeArray.') WHERE id='$id'" or die(mysql_error());
$result1 = mysql_query($query)or die(mysql_error());
}
}
?>
You have a lot of problems with your code. You are doing the query wrong, and it looks like you are using some of the variables incorrectly. Also, you do not need or die(mysql_error()) after defining your $query variable. Try this:
<?php
if (isset($_POST['name'])) {
foreach ($_POST['name'] as $i => $name) {
$id = intval($_POST['id'][$i]);
$name = mysql_real_escape_string($name);
$lastname = mysql_real_escape_string($_POST['lastname'][$i]);
$email = mysql_real_escape_string($_POST['email'][$i]);
mysql_query("UPDATE test_mysql SET name='$name', lastname='$lastname', email='$email' WHERE id=$id") or die(mysql_error());
}
}
?>
And change your table in the while loop to this:
<?php while ($row = mysql_fetch_array($result)) : ?>
<tr>
<td align="center">
<?php
$id[] = $rows['id'];
echo $rows['id'];
?>
<input type="hidden" name="id[]" value="<?php echo $rows['id']; ?>" />
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>" />
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" />
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?> /">
</td>
</tr>
<?php endwhile; ?>