•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 329,043 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,551 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 545 | Replies: 48 | Solved
![]() |
•
•
Join Date: Jan 2008
Posts: 60
Reputation:
Rep Power: 1
Solved Threads: 0
My current code:
<?php
error_reporting(E_ALL);
$record = $_POST['record'];
echo "Reference: $record<br><BR>";
$host = "localhost";
$user = "xxx";
$pass = "xxx";
$db = "phonebook";
//Connecting to MYSQL
MySQL_connect("$host","$user","$pass");
//Select the database we want to use
mysql_select_db($db) or die("Could not find database");
// Check to see if the link from the previous page was clicked
if(isset($_REQUEST['id'])) {
// Check that the id value in the URL is a number
if(is_numeric($_REQUEST['id'])) {
$id=$_REQUEST['id'];
mysql_query("SELECT * FROM people WHERE id = '$id'") or die(mysql_error);
if(mysql_num_rows > 0) {
$row=mysql_fetch_row($result);
} else {
echo 'No Record';
}
}
}
// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_POST['Submit']){
// Get parameters from form.
$id=$_REQUEST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$phone_num=$_POST['phone_num'];
$ext=$_POST['ext'];
$title=$_POST['title'];
$dept=$_POST['dept'];
// Do update statement.
mysql_query("update phonebook set First Name='$fname', Last Name='$lname', Phone Number='$phone_num', Extension='$ext', Title='$title', Department='$dept' where id='$id'");
// Re-direct this page to select.php.
header("location:phoneupdate.php");
exit;
}
// ************* End update part *************
// *** Select data to show on text fields in form. ***
// Get id parameter (GET method) from select.php
$id=$_POST['id'];
// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from people where id='$id'");
// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);
// Close database connection.
mysql_close();
?>
<!-- END OF PHP CODES AND START HTML TAGS -->
<html>
<body>
<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>?id=$id">
<p>First Name :
<!-- name of this text field is "fname" -->
<input name="fname" type="text" id="fname" value="<? echo $row[1]; ?>"/>
<br />
Last Name :
<!-- name of this text field is "lname" -->
<input name="lname" type="text" id="lname" value="<? echo $row[2]; ?>"/>
<br />
Phone Number :
<!-- name of this text field is "phone_num" -->
<input name="phone_num" type="text" id="phone_num" value="<? echo $row[3]; ?>"/>
</p>
Extension :
<!-- name of this text field is "ext" -->
<input name="ext" type="text" id="ext" value="<? echo $row[4]; ?>"/>
<br />
Title :
<!-- name of this text field is "title" -->
<input name="title" type="text" id="title" value="<? echo $row[5]; ?>"/>
<br />
Department :
<!-- name of this text field is "dept" -->
<input name="dept" type="text" id="dept" value="<? echo $row[6]; ?>"/>
<br />
Fax Number :
<!-- name of this text field is "fax" -->
<input name="fax" type="text" id="fax" value="<? echo $row[7]; ?>"/>
<br />
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html> Last edited by rickarro : 3 Days Ago at 5:22 pm.
this part of your code is incorrect, you don't have any POST data from the previous page so $_POST['id'] will return a null value, remove this:
as it is basically duplicating this part (move this to below the comment stating the end of the update if you wish):
Also, can you list the column names from the database.
// *** Select data to show on text fields in form. ***
// Get id parameter (GET method) from select.php
$id=$_POST['id'];
// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from people where id='$id'");
// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);as it is basically duplicating this part (move this to below the comment stating the end of the update if you wish):
// Check to see if the link from the previous page was clicked
if(isset($_REQUEST['id'])) {
// Check that the id value in the URL is a number
if(is_numeric($_REQUEST['id'])) {
$id=$_REQUEST['id'];
mysql_query("SELECT * FROM people WHERE id = '$id'") or die(mysql_error);
if(mysql_num_rows > 0) {
$row=mysql_fetch_row($result);
} else {
echo 'No Record';
}
}
}Also, can you list the column names from the database.
Last edited by xan : 3 Days Ago at 5:30 pm.
•
•
Join Date: Jan 2008
Posts: 60
Reputation:
Rep Power: 1
Solved Threads: 0
Here is my table structure from mysql:
-- Table structure for table `people`
--
CREATE TABLE `people` (
`id` int(11) NOT NULL auto_increment,
`fname` varchar(30) NOT NULL default '',
`lname` varchar(50) NOT NULL default '',
`phone_num` varchar(12) NOT NULL default '',
`ext` varchar(3) NOT NULL default '',
`title` varchar(255) NOT NULL default '',
`dept` varchar(255) NOT NULL default '',
`fax` varchar(12) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
and here is the code after the changes you just told me to change.
Sorry this is so messed up man!
-- Table structure for table `people`
--
CREATE TABLE `people` (
`id` int(11) NOT NULL auto_increment,
`fname` varchar(30) NOT NULL default '',
`lname` varchar(50) NOT NULL default '',
`phone_num` varchar(12) NOT NULL default '',
`ext` varchar(3) NOT NULL default '',
`title` varchar(255) NOT NULL default '',
`dept` varchar(255) NOT NULL default '',
`fax` varchar(12) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
and here is the code after the changes you just told me to change.
<?php
error_reporting(E_ALL);
$record = $_POST['record'];
echo "Reference: $record<br><BR>";
$host = "localhost";
$user = "xxx";
$pass = "xxx";
$db = "phonebook";
//Connecting to MYSQL
MySQL_connect("$host","$user","$pass");
//Select the database we want to use
mysql_select_db($db) or die("Could not find database");
// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_POST['Submit']){
// Get parameters from form.
$id=$_REQUEST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$phone_num=$_POST['phone_num'];
$ext=$_POST['ext'];
$title=$_POST['title'];
$dept=$_POST['dept'];
$fax=$_POST['fax'];
// Do update statement.
mysql_query("update phonebook set First Name='$fname', Last Name='$lname', Phone Number='$phone_num', Extension='$ext', Title='$title', Department='$dept', Fax='$fax' where id='$id'");
// Re-direct this page to select.php.
header("location:phoneupdate.php");
exit;
}
// Check to see if the link from the previous page was clicked
if(isset($_REQUEST['id'])) {
// Check that the id value in the URL is a number
if(is_numeric($_REQUEST['id'])) {
$id=$_REQUEST['id'];
mysql_query("SELECT * FROM people WHERE id = '$id'") or die(mysql_error);
if(mysql_num_rows > 0) {
$row=mysql_fetch_row($result);
} else {
echo 'No Record';
}
}
}
// ************* End update part *************
// Close database connection.
mysql_close();
?>
<!-- END OF PHP CODES AND START HTML TAGS -->
<html>
<body>
<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>?id=$id">
<p>First Name :
<!-- name of this text field is "fname" -->
<input name="fname" type="text" id="fname" value="<? echo $row[1]; ?>"/>
<br />
Last Name :
<!-- name of this text field is "lname" -->
<input name="lname" type="text" id="lname" value="<? echo $row[2]; ?>"/>
<br />
Phone Number :
<!-- name of this text field is "phone_num" -->
<input name="phone_num" type="text" id="phone_num" value="<? echo $row[3]; ?>"/>
</p>
Extension :
<!-- name of this text field is "ext" -->
<input name="ext" type="text" id="ext" value="<? echo $row[4]; ?>"/>
<br />
Title :
<!-- name of this text field is "title" -->
<input name="title" type="text" id="title" value="<? echo $row[5]; ?>"/>
<br />
Department :
<!-- name of this text field is "dept" -->
<input name="dept" type="text" id="dept" value="<? echo $row[6]; ?>"/>
<br />
Fax Number :
<!-- name of this text field is "fax" -->
<input name="fax" type="text" id="fax" value="<? echo $row[7]; ?>"/>
<br />
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>Sorry this is so messed up man!
change
to
mysql_query("update phonebook set First Name='$fname', Last Name='$lname', Phone Number='$phone_num', Extension='$ext', Title='$title', Department='$dept', Fax='$fax' where id='$id'");to
mysql_query("update phonebook set `fname`='$fname', `lname`='$lname', `phone_num`='$phone_num', `ext`='$ext', `title`='$title', `dept`='$dept', `fax`='$fax' where `id`='$id'"); Right, I think this will sort it, I have run the script on my pc with no errors.
I have made a couple of changes to the script, these are in bold.
I have made a couple of changes to the script, these are in bold.
<?php
error_reporting(E_ALL);
$host = "localhost";
$user = "xxx";
$pass = "xxx";
$db = "phonebook";
//Connecting to MYSQL
mysql_connect("$host","$user","$pass");
//Select the database we want to use
mysql_select_db($db) or die("Could not find database");
// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if(isset($_POST['Submit'])){
// Get parameters from form.
$id=$_REQUEST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$phone_num=$_POST['phone_num'];
$ext=$_POST['ext'];
$title=$_POST['title'];
$dept=$_POST['dept'];
$fax=$_POST['fax'];
// Do update statement.
mysql_query("UPDATE people SET `fname`='$fname', `lname`='$lname', `phone_num`='$phone_num', `ext`='$ext', `title`='$title', `dept`='$dept', `fax`='$fax' where `id`='$id'")or die(mysql_error());
// Re-direct this page to select.php.
header("location:phoneupdate.php");
exit;
}
// Check to see if the link from the previous page was clicked
if(isset($_REQUEST['id'])) {
// Check that the id value in the URL is a number
if(is_numeric($_REQUEST['id'])) {
$id=$_REQUEST['id'];
$result=mysql_query("SELECT * FROM people WHERE id = '$id'") or die(mysql_error);
if(mysql_num_rows($result) > 0) {
$row=mysql_fetch_row($result);
} else {
echo 'No Record';
}
}
}
// ************* End update part *************
// Close database connection.
mysql_close();
?>
<!-- END OF PHP CODES AND START HTML TAGS -->
<html>
<body>
Reference: <?=$id?><BR>
<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>?id=<?=$id?>">
<p>First Name :
<!-- name of this text field is "fname" -->
<input name="fname" type="text" id="fname" value="<? echo $row[1]; ?>"/>
<br />
Last Name :
<!-- name of this text field is "lname" -->
<input name="lname" type="text" id="lname" value="<? echo $row[2]; ?>"/>
<br />
Phone Number :
<!-- name of this text field is "phone_num" -->
<input name="phone_num" type="text" id="phone_num" value="<? echo $row[3]; ?>"/>
</p>
Extension :
<!-- name of this text field is "ext" -->
<input name="ext" type="text" id="ext" value="<? echo $row[4]; ?>"/>
<br />
Title :
<!-- name of this text field is "title" -->
<input name="title" type="text" id="title" value="<? echo $row[5]; ?>"/>
<br />
Department :
<!-- name of this text field is "dept" -->
<input name="dept" type="text" id="dept" value="<? echo $row[6]; ?>"/>
<br />
Fax Number :
<!-- name of this text field is "fax" -->
<input name="fax" type="text" id="fax" value="<? echo $row[7]; ?>"/>
<br />
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html> Last edited by xan : 3 Days Ago at 6:52 pm.
•
•
Join Date: Jan 2008
Posts: 60
Reputation:
Rep Power: 1
Solved Threads: 0
Still no change. I really don't understand. I've got 5 forms that access this data. 1 to view the phonebook, 1 to delete a record, 1 to insert a new record, and 2 we're working on to edit a record. All of them work great except for the 2 that edit. So I know the data is there and it is accessable. I just don't understand what is different with this one on my system, especially if you've got it working on yours. Here is what it looks like when I run the script.
Reference:
First Name : <? echo $row[1]; ?>
Last Name : <? echo $row[2]; ?>
Phone Number : <? echo $row[3]; ?>
Extension : <? echo $row[4]; ?>
Title : <? echo $row[5]; ?>
Department : <? echo $row[6]; ?>
Fax Number : <? echo $row[7]; ?>
SUBMIT
I can type in the box but get a page error when submit is pressed. I'm at a loss. Any ideas?
Reference:
First Name : <? echo $row[1]; ?>
Last Name : <? echo $row[2]; ?>
Phone Number : <? echo $row[3]; ?>
Extension : <? echo $row[4]; ?>
Title : <? echo $row[5]; ?>
Department : <? echo $row[6]; ?>
Fax Number : <? echo $row[7]; ?>
SUBMIT
I can type in the box but get a page error when submit is pressed. I'm at a loss. Any ideas?
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
Other Threads in the PHP Forum


Linear Mode