This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
This is my form in HTML.
<html>
<head>
</head>
<body>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form" name="form" method="POST" action="add_new_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
<tr>
<td width="14%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top"><strong>Detail</strong></td>
<td valign="top">:</td>
<td><textarea name="detail" cols="50" rows="3" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50" /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="email" type="text" id="email" size="50" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
This is add_new_topic.php code.
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="myforum"; // Database name
$tbl_name="fquestions"; // Table name
// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password" , "$db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"$db_name")or die("cannot select DB");
// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO '$tbl_name'('topic',' detail', 'name', 'email', 'datetime')VALUES('topic', 'detail', 'name', 'email', 'datetime')";
$result=mysqli_query($con,$sql);
if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysqli_close($con);
?>
It shows the error :
Notice: Undefined index: topic in
C:\xampp\htdocs\myforum\add_new_topic.php on line 18
Notice: Undefined index: detail in
C:\xampp\htdocs\myforum\add_new_topic.php on line 19
Notice: Undefined index: name in
C:\xampp\htdocs\myforum\add_new_topic.php on line 20
Notice: Undefined index: email in
C:\xampp\htdocs\myforum\add_new_topic.php on line 21 ERROR
HERE,on line 18,19,20 and 21 the following code lies.
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
Now what is the error in this POST method?
before getting post value add a condition if post value is not empty like this:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="myforum"; // Database name
$tbl_name="fquestions"; // Table name
if(isset($_POST) && !empty($_POST)){
// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password" , "$db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"$db_name")or die("cannot select DB");
// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO '$tbl_name'('topic',' detail', 'name', 'email', 'datetime')VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysqli_close($con);
}
?>
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I get this error:
Notice: Undefined index: gender \form.php on line 26.please help..how solve this error..?
how get from form employee gender male or female and insert MySQL database
**my html code**
<form action="form.php" method="post">
<table border="1">
<tr>
<td align="center">Form Input Employees Data</td>
</tr>
<tr>
<td>
<table>
<form method="post" action="form.php">
<tr>
<td>Name</td>
<td><input name="employe_name" type="text" id="employe_name" size="20">
</td>
</tr>
<tr>
<td>Address</td>
<td><input name="employe_add" type="text" id="employe_add" size="40">
</td>
</tr>
<tr>
<td height="23">Gender</td>
<td>Male<input type="radio" name="gender"value="m" />
Female<input type="radio" name="gender"value="f"/>
</td></tr>
<td>
<td align="right"><input type="submit"
name="submit" value="Submit"> add</td>
</tr>
</table>
</td>
</tr>
</table></form>
And
my PHP code is...
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','root','','employe');
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}else{
echo"connection good";
}
if(isset($_POST['gender']))
{
$gender=$_POST['gender'];
echo("your are select" .$gender);
}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];
$gender=$_POST['gender'];
//insert data into mysql
$sql = "INSERT INTO employe_detail(employe_name,employe_add,gender) VALUES('$employe_name','$employe_add','$gender')";
print '<h3>Insert a record</h3>';
if($sql)
{
print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />';
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$mysqli->close();
?>
please help me............
Change your code from
if(isset($_POST['gender']))
{
$gender=$_POST['gender'];
echo("your are select" .$gender);
}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];
$gender=$_POST['gender'];
To
$gender = "";
if(isset($_POST['gender']))
{
$gender=$_POST['gender'];
echo("your are select" .$gender);
}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];
gender will be assigned or will be left blank
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I'm sorry for taking a bit of your time i've been trying to figure out this error for hours
I have the following error:
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 71
Notice: Undefined index: BUSSINESSID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: ID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: BUSSINESSID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: ID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Can't seem to work out whats wrong Here's the code:
<html>
<head>
</head>
<body>
<?php
$user="root";
$pass="";
$database="epages";
$localhost="localhost";
$conn=mysql_connect($localhost,$user,$pass);
mysql_select_db("epages") or die(mysql_error());
//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add") {
$strSQL = "INSERT INTO BUSINESS ";
$strSQL .="(BUSINESSID,BUSINESSNAME,SERVICES) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddBUSINESSID"]."','".$_POST["txtAddName"]."' ";
$strSQL .=",'".$_POST["txtAddService"]."') ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Save [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update") {
$strSQL = "UPDATE BUSINESS SET ";
$strSQL .="BUSINESSID = '".$_POST["txtEditBUSINESSID"]."' ";
$strSQL .=",BUSINESSNAME = '".$_POST["txtEditName"]."' ";
$strSQL .=",SERVICES = '".$_POST["txtEditService"]."' ";
$strSQL .="WHERE BUSINESSID = '".$_POST["hdnEditBUSINESSID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Update [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Delete Condition ***//
if($_GET["Action"] == "Del") {
$strSQL = "DELETE FROM BUSINESS WHERE BUSINESSID ='".$_GET["ID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Delete [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
$strSQL = "SELECT * FROM BUSINESS";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); ?>
<form name="frmMain" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">BUSINESSID</div></th>
<th width="98"> <div align="center">BusinessName</div></th>
<th width="198"> <div align="center">Service</div></th>
<th width="30"> <div align="center">Edit</div></th>
<th width="30"> <div align="center">Delete</div></th>
</tr>
<?php
while($objResult = mysql_fetch_array($objQuery)) {
if($objResult["BUSSINESSID"] == $_GET["ID"] and $_GET["Action"] == "Edit") { ?>
<tr>
<td><div align="center">
<input type="text" name="txtEditBUSINESSID" size="5" value="<?php echo $objResult["BUSINESSID"];?>">
<input type="hidden" name="hdnEditBUSINESSID" size="5" value="<?php echo $objResult["BUSINESSID"];?>">
</div></td>
<td><input type="text" name="txtEditName" size="20" value="<?php echo $objResult["BUSINESSNAME"];?>"></td>
<td><input type="text" name="txtEditService" size="20" value="<?php echo $objResult["SERVICES"];?>"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
<input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<?php echo $_SERVER["PHP_SELF"];?>';">
</div></td>
</tr>
<?php
} else { ?>
<tr>
<td><div align="center"><?php echo $objResult["BUSINESSID"];?></div></td>
<td><?php echo $objResult["BUSINESSNAME"];?></td>
<td><?php echo $objResult["SERVICES"];?></td>
<td align="center">Edit</td>
<td align="center">Delete</td>
</tr>
<?php
}
} ?>
<tr>
<td><div align="center"><input type="text" name="txtAddBUSSINESSID" size="5"></div></td>
<td><input type="text" name="txtAddName" size="20"></td>
<td><input type="text" name="txtAddService" size="20"></td>
<td colspan="2" align="right"><div align="center"><input name="btnAdd" type="button" id="btnAdd" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();"></div></td>
</tr>
</table>
</form>
<?php mysql_close($conn); ?>
</body>
</html>
`
The lines that gives me the errors is this if($_GET["Action"] == "Del")
and this if($objResult["BUSSINESSID"] == $_GET["ID"] and $_GET["Action"] == "Edit").
Thanks in advance if you can help me out.
You need to take whole code using $_POST or $_GET arrays inside if block and check for any of the main $_GET/$_POST variable that is set by (isset() method) if it is set then do that stuff in the if block else don't.
Check whether the index is set in the arrays before using them, like so:
if (isset($_GET["Action"]) {
if($_GET["Action"] == "Del") {
// Do stuff
}
}
As #A.B mentioned in the other answer, you should always do this check.
The reason you're getting this error is because the GET/POST requests don't have a value set for this index on the client side.
I'm trying to implement an update script on my page but it doesn't work.
I have 2 pages, the 'update.php' page and the 'update_ac.php' page that runs the script after hitting 'submit' on the Form, the code is as below:
update.php
On this page i have this error : Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\update.php on line 16
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="764503"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$result=mysql_query("SELECT * FROM produse WHERE id='$id'");
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>titlu</strong></td>
<td align="center"><strong>stare</strong></td>
<td align="center"><strong>pret</strong></td>
<td align="center"><strong>descriere</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="titlu" type="text" id="titlu" value="<? echo $rows['titlu']; ?>">
</td>
<td align="center">
<input name="stare" type="text" id="stare" value="<? echo $rows['stare']; ?>" size="15">
</td>
<td>
<input name="pret" type="text" id="pret" value="<? echo $rows['pret']; ?>" size="15">
</td>
<td>
<input name="descriere" type="text" id="descriere" value="<? echo $rows['descriere']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
update_ac.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="764503"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE produse SET titlu='$_POST[titlu]' ,stare='$_POST[stare]' ,pret='$_POST[pret]' ,descriere='$_POST[descriere]' WHERE id='$_POST[id]'";
$result=mysql_query($sql);
// if successfully updated.
if($result)
{
echo "Successful";
}
else
{
echo "ERROR";
}
?>
mysql_query return FALSE when error occurs instead of returning an Array, this test avoid a second (and bigger) error : if FALSE no problem, if an Array you can fetch it ;oP
if ($result) $rows=mysql_fetch_array($result);
This probably means you have an error in your query. If you have an error in your query, it doesnt create a resource to get your data from.
to find an error: add or die(mysql_error()) to your mysql_query command. so you can find out the error.
You have error in your update sql query in the update_ac.php file. So nothing is probably getting updated. You are using the array indexes wrong in the following statement:
$sql="UPDATE produse SET titlu='$_POST[titlu]' ,stare='$_POST[stare]' ,pret='$_POST[pret]' ,descriere='$_POST[descriere]' WHERE id='$_POST[id]'";
$_POST[titlu] should be $_POST['titlu'], $_POST[stare] should be $_POST'stare'] and so on. So the correct query can be as below:
$sql="UPDATE produse SET titlu='".$_POST['titlu']."' ,stare='".$_POST['stare']."' ,pret='".$_POST['pret']."' ,descriere='".$_POST['descriere']."' WHERE id='".$_POST['id']."'";
I have a problem in my guestbook in php. After I fill up the guestbook, it works fine but when I look to my database, all the data I entered is repeated more than once.
guestbook.php
<table border="0" width="920" bgcolor="#1d1c1b" id="round"><tr><td>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr><br>
<td><strong><h2>SEACO's Guestbook</h2> </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" id="name" size="40" placeholder="Enter your name here" required/></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" placeholder="Enter your email here (Optional)"/></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" id="comment" placeholder="Your comment here" required></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" class="button"/> <input type="reset" name="Submit2" value="Reset" class="button" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
</table><br><center><strong><button class="button">View Guestbook</button> </strong></center>
<br><br></table>
addguestbook.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime)values('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
}
else {
echo "ERROR";
}
mysql_close();
?>
viewguestbook.php
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><font color="white">View Guestbook</font> | <button class="button">Sign Guestbook</button> </strong></td>
</tr>
</table>
<br>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357" id="input"><?php echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td id="input"><?php echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td id="input"><?php echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td id="input"><?php echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<?php
}
mysql_close(); //close database
?>
</table>
<?php
if (!isset($_POST['name'])) {
header ("Location: guestbook.php");
exit();
}
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime)values('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
unset($_POST['name']);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
}
else {
echo "ERROR";
}
mysql_close();
?>
There is no way to execute your query 6 times until you resubmit the form by reloading or any ajax request. If your query is executed then it should return true in $result. you can use exit() inside condition to check if it works and Try to check if form submitted by submit button like this:
<?php
if(isset($_POST['Submit'])){
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="users"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime) values ('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
exit();
}
else {
echo "ERROR";
}
mysql_close();
}
else{
echo "not set";
}
probably stupid question but...
I have a sample for PHP and MySQL, which doesn't work...
there are 3 php files:
list_records.php
update.php
update_ac.php
the problem is that 3rd one doesn't read variables from the 2nd. What am I doing wrong?
Here are my files/code:
list_records.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['email']; ?></td>
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
update.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td> </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']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
update_ac.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET name='TEST', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
u need to get the value in the server side from client side before inserting or updating them into the database.for this we have superglobal variables in php like $_GET,$_POST,$_REQUEST etc.
so include thses in your update_ac.php:
$lastname=$_POST[lastname];
$email=$_POST[email];
$id=$_POST[id];`
you can use $_REQUEST method if you are not sure by which method u passed the values from the form.but try to avoid it as it is less secure.
You need to get them from POST in the update_ac.php file like
$sql="UPDATE $tbl_name
SET name='TEST',
lastname='".$_POST['lastname']."',
email='".$_POST['email']."'
WHERE id=".$_POST['id'];
This is what you need in your update query
......lastname='".$_POST['lastname']."', email='".$_POST['email']."' WHERE id=".$_POST['id']
Whenever a form posts/submits a value to a file mentioned in an action, the file must access the values of form's field's name in $_POST array as $_POST['lastname'], $_POST['email']. etc. (if the method used is POST in form tag)
There are three ways of access submitted values:
GET http://php.net/manual/en/reserved.variables.get.php
POST http://php.net/manual/en/reserved.variables.post.php
REQUEST http://php.net/manual/en/reserved.variables.request.php