homepage Welcome to WebmasterWorld Guest from 50.16.112.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
edit trouble
Gilead



 
Msg#: 4385300 posted 4:35 pm on Nov 9, 2011 (gmt 0)

This is my thought process: if the edit form is submitted, then update the database, if not, display the form to update.

<?php

// connect to the database
include('config.php');
$table_name="users";
$id=mysql_real_escape_string($_GET['id']);
$query = "SELECT * FROM $table_name WHERE contactid='$id'";
$result = mysql_query($query) or die("Cannot check for existing ID " . mysql_error());
$form_submit="";

if ($row = mysql_fetch_array($result)) //this is where I wanted to check for the value of submit=Update but I got a boolen error{
$contactid = htmlentities($row['contactid']);
$account_number = htmlentities($row['account_number']);
$organization_title = htmlentities($row['organization_title']);
$account_name = htmlentities($row['account_name']);
$organizationDBA = htmlentities($row['organizationDBA']);
$network_representative = htmlentities($row['network_representative']);
$network = htmlentities($row['network']);
$member_login = htmlentities($row['member_login']);
$title = htmlentities($row['title']);
$salutation = htmlentities($row['salutation']);
$firstname = htmlentities($row['firstname']);
$lastname = htmlentities($row['lastname']);
$communication_method = htmlentities($row['communication_method']);
$phone = htmlentities($row['phone']);
$mobile = htmlentities($row['mobile']);
$otherphone= htmlentities($row['otherphone']);
$fax = htmlentities($row['fax']);
$email_opt_out= htmlentities($row['email_opt_out']);
$email = htmlentities($row['email']);
}
?>
//display the form
<form action="editmember.php" method="post">
<div>
<p><strong>ContactID:</strong> <input type="text" name="contactid" value="<?php echo $contactid; ?>"/></p>
<p><strong>Account Number:</strong> <input type="text" name="accountnumber" value="<?php echo $account_number; ?>"/></p>
<p><strong>Organization Title:</strong> <input type="text" name="organizationtitle" value="<?php echo $organization_title; ?>"/></p>
<p><strong>Account Name:</strong> <input type="text" name="accountname" value="<?php echo $account_name; ?>"/></p>
<p><strong>Organization DBA:</strong> <input type="text" name="dba" value="<?php echo $organizationDBA; ?>"/></p>
<p><strong>Network Representative:</strong> <input type="text" name="networkrep" value="<?php echo $network_representative; ?>"/></p>
<p><strong>Network</strong> <input type="text" name="network" value="<?php echo $network; ?>"/></p>
<p><strong>Login:</strong> <input type="text" name="login" value="<?php echo $member_login; ?>"/></p>
<p><strong>Title:</strong> <input type="text" name="title" value="<?php echo $title; ?>"/></p>
<p><strong>Salutation:</strong> <input type="text" name="salutation" value="<?php echo $salutation ?>"/></p>
<p><strong>First Name:</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/></p>
<p><strong>Last Name:</strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>"/></p>
<p><strong>Communication Method:</strong> <input type="text" name="commethod" value="<?php echo $communication_method; ?>"/></p>
<p><strong>Phone:</strong> <input type="text" name="phone" value="<?php echo $phone; ?>"/></p>
<p><strong>Mobile:</strong><input type="text" name="mobile" value="<?php echo $mobile; ?>"/></p>
<p><strong>Other Phone:</strong> <input type="text" name="other" value="<?php echo $otherphone; ?>"/></p>
<p><strong>Fax:</strong> <input type="text" name="fax" value="<?php echo $fax; ?>"/></p>
<p><strong>Email Opt Out:</strong> <input type="text" name="emailopt" value="<?php echo $email_opt_out; ?>"/></p>
<p><strong>Email</strong> <input type="text" name="email" value="<?php echo $email; ?>"/></p>
<input type="submit" name="submit" value="Update">
</div>
</form>
<?php

else if ($form_submit == "Update") {
mysql_query("UPDATE $table_name SET contactid='$contactid',account_number='$account_number',organization_title='$organization_title', account_name='$account_name', organizationDBA='$organizationDBA', network_representative='$network_representative', network='$network', member_login='$member_login', member_password='$member_password', title='$title', salutation='$salutation', firstname='$firstname', lastname='$lastname',communication_method='$communication_method', phone='$phone', mobile='$mobile', otherphone='$otherphone', fax='$fax', email_opt_out='$email_opt_out', email='$email' WHERE contactid='$id'")
or die(mysql_error());
mysql_query($query) or die("Cannot update record with id $id ID " . mysql_error());
echo 'Member Updated!';
}
It wasn't working- still not working. Never got to the updating part, just died at the cannot update record. Which is why I thought about checking the submit value. Is there a better way to go about it? maybe a isset?

Thanks!

 

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4385300 posted 5:37 pm on Nov 9, 2011 (gmt 0)

do you get anything output from mysql_error()?

Gilead



 
Msg#: 4385300 posted 7:20 pm on Nov 9, 2011 (gmt 0)

nothing at all. Which made me think it might have worked, but after checking it in phpmyadmin, no changes were there.

Gilead



 
Msg#: 4385300 posted 7:32 pm on Nov 9, 2011 (gmt 0)

I decided to try a different route: a two page method.
editmember.php
<?php

// connect to the database
include('config.php');
$table_name="users";
$id=mysql_real_escape_string($_GET['id']);
$query = "SELECT * FROM $table_name WHERE contactid='$id'";
$result = mysql_query($query) or die("Cannot check for existing ID " . mysql_error());

if ($row = mysql_fetch_array($result)) {
$contactid = htmlentities($row['contactid']);
$account_number = htmlentities($row['account_number']);
$organization_title = htmlentities($row['organization_title']);
$account_name = htmlentities($row['account_name']);
$organizationDBA = htmlentities($row['organizationDBA']);
$network_representative = htmlentities($row['network_representative']);
$network = htmlentities($row['network']);
$member_login = htmlentities($row['member_login']);
$title = htmlentities($row['title']);
$salutation = htmlentities($row['salutation']);
$firstname = htmlentities($row['firstname']);
$lastname = htmlentities($row['lastname']);
$communication_method = htmlentities($row['communication_method']);
$phone = htmlentities($row['phone']);
$mobile = htmlentities($row['mobile']);
$otherphone= htmlentities($row['otherphone']);
$fax = htmlentities($row['fax']);
$email_opt_out= htmlentities($row['email_opt_out']);
$email = htmlentities($row['email']);
}
?>

<form action="edit.php" method="post">
<div>
<p><strong>ContactID:</strong> <input type="text" name="contactid" value="<?php echo $contactid; ?>"/></p>
<p><strong>Account Number:</strong> <input type="text" name="accountnumber" value="<?php echo $account_number; ?>"/></p>
<p><strong>Organization Title:</strong> <input type="text" name="organizationtitle" value="<?php echo $organization_title; ?>"/></p>
<p><strong>Account Name:</strong> <input type="text" name="accountname" value="<?php echo $account_name; ?>"/></p>
<p><strong>Organization DBA:</strong> <input type="text" name="dba" value="<?php echo $organizationDBA; ?>"/></p>
<p><strong>Network Representative:</strong> <input type="text" name="networkrep" value="<?php echo $network_representative; ?>"/></p>
<p><strong>Network</strong> <input type="text" name="network" value="<?php echo $network; ?>"/></p>
<p><strong>Login:</strong> <input type="text" name="login" value="<?php echo $member_login; ?>"/></p>
<p><strong>Title:</strong> <input type="text" name="title" value="<?php echo $title; ?>"/></p>
<p><strong>Salutation:</strong> <input type="text" name="salutation" value="<?php echo $salutation ?>"/></p>
<p><strong>First Name:</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/></p>
<p><strong>Last Name:</strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>"/></p>
<p><strong>Communication Method:</strong> <input type="text" name="commethod" value="<?php echo $communication_method; ?>"/></p>
<p><strong>Phone:</strong> <input type="text" name="phone" value="<?php echo $phone; ?>"/></p>
<p><strong>Mobile:</strong><input type="text" name="mobile" value="<?php echo $mobile; ?>"/></p>
<p><strong>Other Phone:</strong> <input type="text" name="other" value="<?php echo $otherphone; ?>"/></p>
<p><strong>Fax:</strong> <input type="text" name="fax" value="<?php echo $fax; ?>"/></p>
<p><strong>Email Opt Out:</strong> <input type="text" name="emailopt" value="<?php echo $email_opt_out; ?>"/></p>
<p><strong>Email</strong> <input type="text" name="email" value="<?php echo $email; ?>"/></p>
<input type="submit" name="submit" value="Update">
</div>
</form>

Form shows up with the values from the database. So far, so good.
edit.php
// connect to the database
include('config.php');
$table_name="users";
$id=mysql_real_escape_string($_GET['id']);
$query = "SELECT * FROM $table_name WHERE contactid='$id'";
$result = mysql_query($query) or die("Cannot check for existing ID " . mysql_error());

if ($row = mysql_fetch_array($result)){
$contactid = $_POST['contactid'];
$account_number = htmlentities($_POST['accountnumber']);
$organization_title = htmlentities($_POST['organizationtitle']);
$account_name = htmlentities($_POST['accountname']);
$organizationDBA = htmlentities($_POST['dba']);
$network_representative = htmlentities($_POST['networkrep']);
$network = htmlentities($_POST['network']);
$member_login = htmlentities($_POST['login']);
$title = htmlentities($_POST['title']);
$salutation = htmlentities($_POST['salutation']);
$firstname = htmlentities($_POST['firstname']);
$lastname = htmlentities($_POST['lastname']);
$communication_method = htmlentities($_POST['commethod']);
$phone = htmlentities($_POST['phone']);
$mobile = htmlentities($_POST['mobile']);
$otherphone= htmlentities($_POST['other']);
$fax = htmlentities($_POST['fax']);
$email_opt_out= htmlentities($_POST['emailopt']);
$email = htmlentities($_POST['email']);

mysql_query("UPDATE $table_name SET contactid='$contactid',account_number='$account_number',organization_title='$organization_title', account_name='$account_name', organizationDBA='$organizationDBA', network_representative='$network_representative', network='$network', member_login='$member_login', title='$title', salutation='$salutation', firstname='$firstname', lastname='$lastname',communication_method='$communication_method', phone='$phone', mobile='$mobile', otherphone='$otherphone', fax='$fax', email_opt_out='$email_opt_out', email='$email' WHERE contactid='$id'")
or die(mysql_error());
echo 'Member Updated!';
}

else{ mysql_query($query) or die("Cannot update record with id $id ID " . mysql_error());

}


?>
Right now, just showing a blank page with no changes saved.
Don't know what I'm doing wrong.
Thanks!

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4385300 posted 12:04 am on Nov 10, 2011 (gmt 0)

I assume that means it is stopping at the die on the update query

why don't you split this up, it should help you find the issue

$q = "UPDATE $table_name SET contactid='$contactid',account_number='$account_number',organization_title='$organization_title', account_name='$account_name', organizationDBA='$organizationDBA', network_representative='$network_representative', network='$network', member_login='$member_login', title='$title', salutation='$salutation', firstname='$firstname', lastname='$lastname',communication_method='$communication_method', phone='$phone', mobile='$mobile', otherphone='$otherphone', fax='$fax', email_opt_out='$email_opt_out', email='$email' WHERE contactid='$id'";
echo '<p>query is: ', $q;
mysql_query($q) or die('<p>query died: ' . mysql_error());

I added an echo after the query is constructed, you could take the query it outputs and try it in phpmyadmin to see if it gives an error too

Gilead



 
Msg#: 4385300 posted 3:16 pm on Nov 10, 2011 (gmt 0)

Figured it out! The $id value was getting lost; I switched it over to contactid and it worked!

Thanks!

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4385300 posted 4:47 pm on Nov 10, 2011 (gmt 0)

Now that you have that out of the way,

if the edit form is submitted, then update the database, if not, display the form to update.


Why not display the form in **any** case?

// If form submitted, update
// query DB for data to store in form fields
// if form submitted, output success OR error message, depending
// output form with values from DB

Gilead



 
Msg#: 4385300 posted 5:07 pm on Nov 10, 2011 (gmt 0)

I decided on a two page method this time.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved