Redisplay a form with updated data - php

I'm fairly new to php and have a problem with redisplaying a form with upadated data;
The story so far:
I have Register and Logon pages which identify a 'user' by email address; on LOGIN the user is taken to theri own 'member' page which will display the personal data held on that person. They are then invited to edit that data if they wish. This will take them to an 'Update' page and on completion the database is updated via a (hidden) 'update+ac' page and then back to their own 'master' page. The problem is that I cannot get the 'member' page to then redisplay the updated data in a form nor can I get the form itself to redisplay.
Here is the relevent code for the 'member' page: (I have not coded any security or validation as yet - ths is all on localhost - I want ot get the coding right first)
<?php
include_once 'login.php';
include_once 'functions.php';
if (isset($_SESSION['user']))
{
$user = $_SESSION['user'];
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die("Unable to connect: " . mysql_error());
$data = "SELECT * FROM `names` WHERE email='$user'";
$result=mysql_query($data) or die(mysql_error());
while($row=mysql_fetch_array($result)){
}
?>
<table border='1px' style="background-color:#F0F8FF; font-weight: bold;" >
<caption>Personal Record</caption>
<tr>
<th>ID</th>
<td><?php
echo $row['id'];
?></td>
</tr>
<tr>
<th>Name</th>
<td><?php
echo $row['name'];
?></td>
</tr>
<tr>
<th>E-Mail</th>
<td><?php
echo $row['email'];
?></td>
</tr>
<tr>
<th>Main Telephone</th>
<td><?php
echo $row['maintel'];
?></td>
</tr>
<tr>
<th>Mobile Telephone</th>
<td><?php
echo $row['mobtel'];
?></td>
</tr>
<tr>
<th>Organisation</th>
<td><?php
echo $row['organisation'];
?></td>
</tr>
<tr>
<th>Group Leader</th>
<td><?php
echo $row['group_leader'];
?></td>
</tr>
<tr>
<th>Supervisor</th>
<td><?php
echo $row['supervisor'];
?></td>
</tr>
<tr>
<th>Volunteer</th>
<td><?php
echo $row['volunteer'];
?></td>
</tr>
<tr>
<th>Assessor</th>
<td><?php
echo $row['assessor'];
}
?></td>
</tr>
</table>
<p><br />
<form method="post" action="update.php">
<input name="Submit1" type="submit" value="Edit" style="width: 67px" /></form>
</p>
<p> </p>
The following is the code for the 'update.php' page:
<?php
include_once 'login.php';
include_once 'functions.php';
session_start();
// Connect to server and select database.
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die("Unable to connect: " . mysql_error());
// get value of id that sent from address bar
if (isset($_SESSION['user']))
{
$user = $_SESSION['user'];
// Retrieve data from database
$sql="SELECT * FROM names WHERE email='$user'";
$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>
<td align="center"> </td>
<td align="center"> </td>
<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>E-Mail</strong></td>
<td align="center"><strong>Main Tel</strong></td>
<td align="center"><strong>Mob Tel</strong></td>
<td align="center"><strong>Organisation</strong></td>
<td align="center"><strong>Group Leader</strong></td>
<td align="center"><strong>Supervisor</strong></td>
<td align="center"><strong>Volunteer</strong></td>
<td align="center"><strong>Assessor</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="40"></td>
<td align="center"><input name="maintel" type="text" id="maintel" value="<? echo $rows['maintel']; ?>" size="15"></td>
<td align="center"><input name="mobtel" type="text" id="mobtel" value="<? echo $rows['mobtel']; ?>"></td>
<td align="center"><input name="organisation" type="text" id="organisation" value="<? echo $rows['organisation']; ?>"></td>
<td align="center"><input name="group_leader" type="text" id="group_leader" value="<? echo $rows['group_leader']; ?>"></td>
<td align="center"><input name="supervisor" type="text" id="supervisor" value="<? echo $rows['supervisor']; ?>"></td>
<td align="center"><input name="volunteer" type="text" id="volunteer" value="<? echo $rows['volunteer']; ?>"></td>
<td align="center"><input name="assessor" type="text" id="assessor" value="<? echo $rows['assessor']; ?>"></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>
<?
// close connection
mysql_close();
?>
Finally, here is the code for the 'upate_ac.php' page:
<?php
include_once 'login.php';
include_once 'functions.php';
// Connect to server and select database.
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die("Unable to connect: " . mysql_error());
// update data in mysql database
$id = $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];
$maintel = $_POST['maintel'];
$mobtel = $_POST['mobtel'];
$organisation = $_POST['organisation'];
$group_leader = $_POST['group_leader'];
$supervisor = $_POST['supervisor'];
$volunteer = $_POST['volunteer'];
$assessor = $_POST['assessor'];
$sql="UPDATE `names` SET id='$id', `name`='$name', `email`='$email', `maintel`='$maintel', `mobtel`='$mobtel', `organisation`='$organisation', `group_leader`='$group_leader', `supervisor`='$supervisor', `volunteer`='$volunteer', `assessor`='$assessor' WHERE `id`='$id''";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='member.php?view=$user'>View result</a>";
}
else {
echo "ERROR";
}
?>
What happens is that I get 'Successfull' and 'View Result'. When I click 'View Result' I get taken to the 'member' page but there is no form displayed.
I would appreciate any help.

The session_start() is missing at the start of the page.
Moreover in update_ac.php you are putting the link as
<a href='member.php?view=$user'>View result</a>".
So either you should use it to get the user field in memeber page as $_GET['view'] and then use it to execute queries further as $_SESSION is not available.

Related

I can list th database record, but i cannot update the record(MYSQL,XAMPP,PHP)

I don't know the problem whether it is about my.ini. Can somebody can help me?? I can list the record, but i cannot update the record.
list_property.php
<title>List Property</title>
</head>
<?php
// Connect to server and select database.
mysql_connect("localhost", "root", "123456")or die("cannot connect");
mysql_select_db("album")or die("cannot select DB");
$sql="SELECT * FROM property";
$result=mysql_query($sql);
?>
<body>
<table width="1200" border="1" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="1200" border="1" cellspacing="1" cellpadding="3">
<tr>
<td colspan="50"><strong>List the property</strong> </td>
</tr>
<tr>
<td align="center"><strong>Update_date</strong></td>
<td align="center"><strong>Usage</strong></td>
<td align="center"><strong>Region</strong></td>
<td align="center"><strong>Street</strong></td>
<td align="center"><strong>Building</strong></td>
<td align="center"><strong>Layers</strong></td>
<td align="center"><strong>Unit</strong></td>
<td align="center"><strong>Construction_area</strong></td>
<td align="center"><strong>Saleable_area</strong></td>
<td align="center"><strong>Price</strong></td>
<td align="center"><strong>Rent</strong></td>
<td align="center"><strong>Contant_person</strong></td>
<td align="center"><strong>Contant_Num</strong></td>
<td align="center"><strong>Layout</strong></td>
<td align="center"><strong>Decoration</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['Update_date']; ?></td>
<td><?php echo $rows['Usage']; ?></td>
<td><?php echo $rows['Region']; ?></td>
<td><?php echo $rows['Street']; ?></td>
<td><?php echo $rows['Building']; ?></td>
<td><?php echo $rows['Layers']; ?></td>
<td><?php echo $rows['Unit']; ?></td>
<td><?php echo $rows['Construction_area']; ?></td>
<td><?php echo $rows['Saleable_area']; ?></td>
<td><?php echo $rows['Price']; ?></td>
<td><?php echo $rows['Rent']; ?></td>
<td><?php echo $rows['Contant_person']; ?></td>
<td><?php echo $rows['Contant_Num']; ?></td>
<td><?php echo $rows['Layout']; ?></td>
<td><?php echo $rows['Decoration']; ?></td>
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
</body>
</html>
update.php
<title>Update Property</title>
</head>
<?php
// Connect to server and select database.
mysql_connect("localhost", "root", "123456")or die("cannot connect");
mysql_select_db("album") or die("cannot select DB");
// get value of id that sent from address bar
$P_ID=$_GET['P_ID'];
// Retrieve data from database
$sql="SELECT * FROM album.property WHERE P_ID = '$P_ID'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<body>
<table width="1200" 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="6"><strong>Update Property Details</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<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>Usage</strong></td>
<td align="center"><strong>Region</strong></td>
<td align="center"><strong>Street</strong></td>
<td align="center"><strong>Building</strong></td>
<td align="center"><strong>Layers</strong></td>
<td align="center"><strong>Unit</strong></td>
<td align="center"><strong>Construction_area</strong></td>
<td align="center"><strong>Saleable_area</strong></td>
<td align="center"><strong>Price</strong></td>
<td align="center"><strong>Rent</strong></td>
<td align="center"><strong>Contant_person</strong></td>
<td align="center"><strong>Contant_Num</strong></td>
<td align="center"><strong>Layout</strong></td>
<td align="center"><strong>Decoration</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="Usage" type="text" P_ID="Usage" value="<?php echo $rows['Usage']; ?>"size= "15"/>
</td>
<td align="center">
<input name="Region" type="text" P_ID="Region" value="<?php echo $rows['Region']; ?>" size="15"/>
</td>
<td align="center">
<input name="Street" type="text" P_ID="Street" value="<?php echo $rows['Street']; ?>" size="15"/>
</td>
<td align="center">
<input name="Building" type="text" P_ID="Building" value="<?php echo $rows['Building']; ?>" size="15"/>
</td>
<td align="center">
<input name="Layers" type="text" P_ID="Layers" value="<?php echo $rows['Layers']; ?>" size="15"/>
</td>
<td align="center">
<input name="Unit" type="text" P_ID="Unit" value="<?php echo $rows['Unit']; ?>" size="15"/>
</td>
<td align="center">
<input name="Construction_area" type="text" P_ID="Construction_area" value="<?php echo $rows['Construction_area']; ?>" size="15"/>
</td>
<td align="center">
<input name="Saleable_area" type="text" P_ID="Saleable_area" value="<?php echo $rows['Saleable_area']; ?>" size="15"/>
</td>
<td align="center">
<input name="Price" type="text" P_ID="Price" value="<?php echo $rows['Price']; ?>" size="15"/>
</td>
<td align="center">
<input name="Rent" type="text" P_ID="Rent" value="<?php echo $rows['Rent']; ?>" size="15"/>
</td>
<td align="center">
<input name="Contant_person" type="text" P_ID="Contant_person" value="<?php echo $rows['Contant_person']; ?>" size="15"/>
</td>
<td align="center">
<input name="Contant_Num" type="text" P_ID="Contant_Num" value="<?php echo $rows['Contant_Num']; ?>" size="15"/>
</td>
<td align="center">
<input name="Layout" type="text" P_ID="Layout" value="<?php echo $rows['Layout']; ?>" size="15"/>
</td>
<td align="center">
<input name="Decoration" type="text" P_ID="Decoration" value="<?php echo $rows['Decoration']; ?>" size="15"/>
</td>
<tr>
</table>
<input name="P_ID" type="hidden" P_ID="P_ID" value="<?php echo $rows['P_ID']; ?>"/>
<input type="submit" name="Submit" value="Submit" /></td>
<td align="center"> </td>
</td>
</form>
</tr>
</table>
</body>
</html>
update_ac.php
<?php
// Connect to server and select database.
mysql_connect("localhost", "root", "123456")or die("cannot connect");
mysql_select_db("album")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE property SET Usage='".$_POST['Usage']."', Region='".$_POST['Region']."' , Street='".$_POST['Street']."' , Building='".$_POST['Building']."' , Layers='".$_POST['Layers']."' , Unit='".$_POST['Unit']."' , Construction_area='".$_POST['Construction_area']."' , Saleable_area='".$_POST['Saleable_area']."' , Price='".$_POST['Price']."' , Rent='".$_POST['Rent']."' , Contant_person='".$_POST['Contant_person']."' , Contant_Num='".$_POST['Contant_Num']."' , Layout='".$_POST['Layout']."' , Decoration='".$_POST['Decoration']."' WHERE P_ID='".$_POST['P_ID']."'";
$result=mysql_query($sql) or die ("Cannot Update Property......");
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_property.php'>View Property</a>";
}
else {
echo "ERROR";
}
?>
After changing the error processing as suggested: The query is:
UPDATE property SET Usage='0', Region='Wah Chai' , Street='Tai Hang Road' , Building='Happy Valley' , Layers='a' , Unit='A' , Construction_area='1085 square feet' , Saleable_area='872 square feet' , Price='16800000' , Rent='48000' , Contant_person='Mr Yu' , Contant_Num='99968977' , Layout='3 bedrooms, including one suit' , Decoration='Yes' WHERE P_ID='1'
And the error is
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
'Usage='0', Region='Wah Chai' , Street='Tai Hang Road' , Building='Happy Valley'
at line 1
It would help if you processed the error correctly. Then you would know what the error was rather than just that an error happened.
// update data in mysql database
$sql="UPDATE property SET Usage='".$_POST['Usage'].
"', Region='".$_POST['Region'].
"' , Street='".$_POST['Street'].
"' , Building='".$_POST['Building'].
"' , Layers='".$_POST['Layers'].
"' , Unit='".$_POST['Unit'].
"' , Construction_area='".$_POST['Construction_area'].
"' , Saleable_area='".$_POST['Saleable_area'].
"' , Price='".$_POST['Price'].
"' , Rent='".$_POST['Rent'].
"' , Contant_person='".$_POST['Contant_person'].
"' , Contant_Num='".$_POST['Contant_Num'].
"' , Layout='".$_POST['Layout'].
"' , Decoration='".$_POST['Decoration'].
"' WHERE P_ID='".$_POST['P_ID'].
"'";
$result=mysql_query($sql);
if ( ! $result ) {
echo "SQL\n $sql\nGenerated this error\n";
echo 'Database error: ' . mysql_errno() . ' : ' . mysql_error();
}
This mysql_* extension has been deprecated and therefore any code you write with it will at some point in time no longer work as the extension will soon be removed from new releases of PHP.

Multirow Update Query with HTML table form

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; ?>

Updating data in table PHP mysql error

I am trying to update a data in a table which has 5 fields.
The fields are the following: proj_name, cust_name, address, cost and details.
Here is my code for the update (update_orde.php):
<?php
include("connect.php");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM project 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_orde_suc.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td colspan="5"><strong>Update Data</strong> </td>
</tr>
<tr>
<td align="center"><strong>Project Name</strong></td>
<td align="center"><strong>Customer</strong></td>
<td align="center"><strong>Address</strong></td>
<td align="center"><strong>Cost</strong></td>
<td align="center"><strong>Details</strong></td>
</tr>
<tr>
<td><input name="pn" type="text" id="pn" value="<? echo $rows['proj_name']; ?>"></td>
<td align="center"><input name="cn" type="text" id="cn" value="<? echo $rows['cust_name']; ?>" size="15"></td>
<td align="center"><input name="add" type="text" id="add" value="<? echo $rows['address']; ?>" size="15"></td>
<td><input name="cost" type="text" id="cost" value="<? echo $rows['cost']; ?>" size="15"></td>
<td><input name="details" type="text" id="details" value="<? echo $rows['details']; ?>" size="15"></td>
</tr>
<tr>
<td align="center" colspan="5"><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> <input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
// close connection
mysql_close();
?>
and here's the successful insertion into the database (update_orde_suc.php):
<?php
include("connect.php");
$id=$_POST['id'];
$pn = $_POST['proj_name'];
$cn = $_POST['cust_name'];
$add = $_POST['address'];
$cost = $_POST['cost'];
$det = $_POST['details'];
// update data in mysql database
$sql="UPDATE project SET proj_name='$pn', cust_name='$cn',address='$add', cost='$cost', details='$det' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='orders_edit.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
The problem is that when I try to change the data there is an error that says the 3 first indexes of update_orde_suc.php are undefined (cost and details are ok).
The weirdest thing is that I used the exact same code for another table update and it worked just fine, the only thing i did now was to change the names of the variables to correspond to the names of the new table.
You mixed up the variables badly. For example you save the proj_name coming from input as $pn and later when you are inserting it in the database you use $proj_name. You have to stick with the variable names throughout the code to work.

HTML input form array values not passing through to PHP submit

I created a html form to enable users to update data. However the input data (array values) are not passed through to php SUBMIT, thus clicking SUBMIT does not update the table. When I go into the SUBMIT portion of the script and change the SET to specific numbers or text, the table is updated. Meaning that the values from the html input data array are not being passed through properly to the SUBMIT portion of the script. Any help appreciated.
<?php
//Mysql connection and initial select query placed above
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<form name="Contacts" method="post" action="">
<tr>
<td>
<table width="400" 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"><?php $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="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
$id=$_POST['id'];
$name=$_POST['name'];
for($i=0;$i<$num;$i++){
$sql1="UPDATE contacts SET name= ".$name[$i]." WHERE id= ".$id[$i]."";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:updated.php");
}
mysql_close();
?>
Thanks!
You are missing single quotes around your $name[$i] in the SQL statement. If id is not always numeric, you will also need to surround $id[$i] in single quotes.
$sql1="UPDATE contacts SET name= '".$name[$i]."' WHERE id= ".$id[$i]."";
//------------------------------^^^-----------^^^
Some error checking in your mysql_query() call would make this clearer. See below.
And you must filter these against SQL injection before passing them to the query.
for($i=0;$i<$num;$i++) {
// Call mysql_real_escape_string() to sanitize these...
$id[$i] = mysql_real_escape_string($id[$i]);
$name[$i] = mysql_real_escape_string($name[$i]);
$sql1="UPDATE contacts SET name= '".$name[$i]."' WHERE id= ".$id[$i]."";
$result1 = mysql_query($sql1);
// Error checking:
if (!$result1) {
echo mysql_error();
}
}
My Mistake. I didn't look at the form enough. You are assigning an array here. PHP is easy to debug-
Right here:
$id=$_POST['id'];
$name=$_POST['name'];
After those lines use var_dump($id) or print_r($id) to check out the contents in your variables.
Thanks very much for the prompt responses and the assistance provided. After implementing the recommended changes, the final working script (using PHP 5.2) is as shown below (for anyone who might need it).
<?php
Mysql connection, initial query and then close Mysql connection
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="Contacts" 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"><?php $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 height="75" colspan="4" align="center"><input type="submit" name="Submit" value=" save for later "></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
mysql_connect($dbhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$name=$_POST['name'];
for($i=0;$i<$num;$i++) {
// sanitize...
$id[$i] = mysql_real_escape_string($id[$i]);
$name[$i] = mysql_real_escape_string($name[$i]);
$sql1="UPDATE test_mysql SET name= '".$name[$i]."' WHERE id= ".$id[$i]."";
$result1 = mysql_query($sql1);
// Error checking:
if (!$result1) {
echo mysql_error();
}
}
if($result1){
header("location:updated.php");
}
}
mysql_close();
?>

Updating mysql table using checkbox

i try to update a mysql table with multiple check box, but my code doesn't seem to work, so any help is welcome. My code is:
<strong>Update <strong class="highlight">multiple</strong> <strong class="highlight">rows</strong> <strong class="highlight">in</strong> <strong class="highlight">mysql</strong></strong><br>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="db_test"; // Database name
$tbl_name="test_table"; // Table name
// Connect to server and select databse.
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);
// Count table <strong class="highlight">rows</strong>
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Operation</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td>
<td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
<td align="center"><input name="description<? echo $rows['id']; ?>" type="text" id="description" value="<? echo $rows['description']; ?>"></td>
<td align="center"><input name="phone_number<? echo $rows['id']; ?>" type="text" id="phone_number" value="<? echo $rows['phone_number']; ?>"></td>
<td align="center"><input name="operation<? echo $rows['id']; ?>" type="text" id="operation" value="<? echo $rows['operation']; ?>"></td>
<td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1"
<?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit)
{
foreach($_POST['id'] as $id)
{
$onoff = 0;
if (isset($_POST["ONOFF".$id]))
{
$onoff = 1;
}
$sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:test_update2.php");
}
mysql_close();
?>
Thanks for you help.
Regards.
Useif($_POST['Submit']) instead of if($Submit)
Second thing is that you should use id="name" only once, and you have id="name" on every row.
As far as I understood, after the user form submission you want to redirect the user to test_update2.php and the problem was that you couldn't simply do that since the headers were already been send. You can't ever use header() method after HTML, because you can't ever get control of the headers after you output some HTML.
I fixed your code and tested it out and it was working perfectly.
EDITED:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="db_test"; // Database name
$tbl_name="test_table"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])) {
foreach($_POST['id'] as $id) {
$onoff = 0;
if (isset($_POST["ONOFF".$id])) {
$onoff = 1;
}
if($onoff == 1) {
$sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'";
} else {
$sql1="UPDATE ".$tbl_name." SET ONOFF='".$onoff."' WHERE id='".$id."'";
}
$result1=mysql_query($sql1);
}
}
//get data from DB
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Count table <strong class="highlight">rows</strong>
$count=mysql_num_rows($result);
?>
<strong>Update <strong class="highlight">multiple</strong> <strong class="highlight">rows</strong> <strong class="highlight">in</strong> <strong class="highlight">mysql</strong></strong><br>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Operation</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td>
<td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
<td align="center"><input name="description<? echo $rows['id']; ?>" type="text" id="description" value="<? echo $rows['description']; ?>"></td>
<td align="center"><input name="phone_number<? echo $rows['id']; ?>" type="text" id="phone_number" value="<? echo $rows['phone_number']; ?>"></td>
<td align="center"><input name="operation<? echo $rows['id']; ?>" type="text" id="operation" value="<? echo $rows['operation']; ?>"></td>
<td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1"
<?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
//close mysql connection
mysql_close();
?>

Categories