/    Sign up×
Community /Pin to ProfileBookmark

Updating post issues.

Heya, i’m kinda having trouble atm. Here’s what i’m doing. After using my search.php to find a customer’s last name, I click on the “select” button and it gets more information on that customer. It displays properly.

What i’m having trouble is editing/updating. So when I want to change the email, I would edit it, but after clicking update, it won’t change. Then I change the code to use this “$customerID = (int) $_POST[‘customerID’];”, and it’s giving me errors. “Undefined index: customerD.”. The GET method has no problem with customerID. It’s after the while loop post, in the Update query. Nearly finished with this code, trying to fix this problem! Any help is appreciated.

[code] <?php
if (isset($_GET))
if (empty($_GET[‘customerID’])) {
die(“No customer ID received!”);
}
$customerID = (int) $_GET[‘customerID’];

$db = mysql_connect(“localhost”, “user”, “pw”) or die (‘I cannot connect to the database because: ‘ . mysql_error());
$mydb=mysql_select_db(“dbname”);

$customerID = mysql_real_escape_string($customerID);

$sql = “SELECT * FROM customers WHERE customerID =”. $customerID.””;
$result=mysql_query($sql);

while ($row = mysql_fetch_array($result)){
$lastName = $row[‘lastName’];
$firstName = $row[‘firstName’];
$address = $row[‘address’];
$city = $row[‘city’];
$state = $row[‘state’];
$postalCode = $row[‘postalCode’];
$countryCode = $row[‘countryCode’];
$phone = $row[‘phone’];
$email = $row[’email’];
$password = $row[‘password’];

if (isset($_POST)) {
“UPDATE customers SET lastName=’$lastName’,firstName=’$firstName’, address=’$address’, city = ‘$city’, state = ‘$state’, postalCode = ‘$postalCode’, countryCode = ‘$countryCode’, phone = ‘$phone’, email = ‘$email’, password = ‘$password’ WHERE customerID =”. $customerID.””;
$customerID = (int) $_POST[‘customerID’];
}
?>
<h1>View/Update Customer</h1>
<form action=”index.php” method=”post” id=”customer_form”>
<input type=”hidden” name=”action” value=”update_customer” />

<label>First Name:</label>
<input type=”text” name=”firstName” value=”<? echo $row[‘firstName’]; ?>” />
<br />

<label>Last Name:</label>
<input type=”text” name=”lastName” value=”<? echo $row[‘lastName’]; ?>”/>
<br />
<label>Address:</label>
<input type=”text” name=”address”value=”<? echo $row[‘address’]; ?>” />
<br />

<label>City:</label>
<input type=”text” name=”city”value=”<? echo $row[‘city’]; ?>” />
<br />

<label>State:</label>
<input type=”text” name=”state”value=”<? echo $row[‘state’]; ?>” />
<br />

<label>Postal Code:</label>
<input type=”text” name=”postalCode”value=”<? echo $row[‘postalCode’]; ?>” />
<br />

<label>Country Code:</label>
<input type=”text” name=”countryCode”value=”<? echo $row[‘countryCode’]; ?>” />
<br />

<label>Phone:</label>
<input type=”text” name=”phone”value=”<? echo $row[‘phone’]; ?>” />
<br />

<label>Email:</label>
<input type=”text” name=”email”value=”<? echo $row[’email’]; ?>” />
<br />

<label>Password:</label>
<input type=”text” name=”password”value=”<? echo $row[‘password’]; ?>” />
<br />

<label>&nbsp;</label>
<input type=”submit” value=”Update Customer” />
<br /> <br />
</form>
<p><a href=”index.php?action=list_customers”>Search Customers</a></p>

}

<?php

}
?>[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@eval_BadCode_Nov 21.2011 — "$customerID = (int) $_POST['customerID'];", and it's giving me errors. "Undefined index: customerD.". The

"$customerID = (int) $_POST['customerID'];" ..... "Undefined index: customerD."

$_POST['customerID'] .... index: customerD

customerID ... customerD

...:rolleyes:

ID ... D


No worries, I did the same thing too. The only difference is I spent 8 hours looking for a bug back then, I found this one in like 1 second ? at least you asked for help. Mine was $_POST and $POST in case you were wondering.
Copy linkTweet thisAlerts:
@kay19authorNov 21.2011 — "$customerID = (int) $_POST['customerID'];", and it's giving me errors. "Undefined index: customerD.". The

"$customerID = (int) $_POST['customerID'];" ..... "Undefined index: customerD."

$_POST['customerID'] .... index: customerD

customerID ... customerD

...:rolleyes:

ID ... D


No worries, I did the same thing too. The only difference is I spent 8 hours looking for a bug back then, I found this one in like 1 second ? at least you asked for help. Mine was $_POST and $POST in case you were wondering.[/QUOTE]


Hey thanks for posting. Er sorry though, my fault for mispelling, it's indeed actually "undefined index: customerID". Sorry ?. Not sure how I removed the I. Anywho, just still tweaking a bit still.
Copy linkTweet thisAlerts:
@eval_BadCode_Nov 22.2011 — I dont see where $_POST['customer_ID'] would be set. You will need to add that field into your form. Also if you plan to use $_GET along with $_POST you will need to write your form tag like this:

<form action="index.php?customer_ID=555" method="post" id="customer_form">

You have tons of other mistakes in your code... LOTS of them.. LOTS of style issues too. I just don't have enough time to go over them all. Here is some code I pecked out which might help you see how I would approach this. I would ideally just teach the end-user how to use MySQL and get rid of all the code ? we can hope.

[code=php]

<?php

class BadFunctionCallException extends RuntimeException{}


/**
* A simple wrapper to make PDO accessible from multiple scopes, NOTE that this
* class is more of a singleton wrapper, you should never have an instance of DB()
* stored in a variable.
*
* @author eval(BadeCode) | new2net
*/
class DB {
/**
* A singleton instance of PDO
* @var PDO
*/
private static $pdo = NULL;

private function __construct() {
$db_args = sprintf('mysql:host=&#37;s;port=%d;dbname=%s;charset=%s',CONFIG::DATABASE_HOST,CONFIG::DATABASE_PORT,CONFIG::DATABASE_NAME,CONFIG::DATABASE_CHARSET);
try {
self::$pdo = new PDO($db_args,CONFIG::DATABASE_USER,CONFIG::DATABASE_PASS);
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
die("<pre>
An exception was raised when trying to connect to the database.

PLEASE NOTE:
---------------
As with any server, there are weekly server restarts and a
reserved maintenance window.

Weekly Restart:
Reserved Maintenance Window:
---------------

Here are the details of the exception raised:
--------------------
$e
</pre>");
}
}
/**
* Used to get the singleton instance of PDO.
* @return PDO
*/
public static function PDO() {
if(self::$pdo === NULL) new DB();
return self::$pdo;
}

public function __clone() {
throw new BadFunctionCallException('Cloning Database or PDO is not supported in this script');
}

public function __set() {
throw new BadFunctionCallException('__set() is not supported in this script');
}

}

class Closures {
private function __construct() {
}

public static function html ($code , $id="", $class=""){
list($id , $class) = array($id !== ""?' id="$id" ':'',$class !== ''?"class="$class"":">");
list($open, $close) = array("<$code$id$class","</$code>");
return function ($inner = "") use ($open, $close) {
return "$open$inner$close";
};
}


public static function formInput($type) {
$open = "<input type="$type" name="%s" value="%s" />";
return function ($name,$value = "") use ($open) {
return sprintf($open,$name,$value);
};
}
}
?>
<?php
//--- begin procedural code | script

$input = array('lastName','firstName','address',
'city','state','postalCode','countryCode',
'phone','email','password'); //customerID is a special case because its the PK- you dont normally update the PK

$statement = DB::PDO()->prepare('SELECT * FROM customers WHERE customerID = ?');
if(isset($_POST['customerID']) && $statement->execute(array($_POST['customer_ID'])) && $statement->rowCount()) {
$updateQuery = 'UPDATE customers SET ';
$updateInput = array();
foreach($input as $field) {
$updateQuery .= ''.$field.' = ?,';
$updateInput[] = $_POST[$value];
}
$updateQuery = rtrim($updateQuery,',') . ' WHERE customerID = ?';
$updateInput[] = $_POST['customerID'];

$updateStatement = DB::PDO()->prepare($updateQuery);
try {
$updateStatement->execute($updateInput);
$h3 = Closures::html('h3');
echo $h3('The records were updated successfully');
} catch (PDOException $e) {
die($e);
}
} else {
throw new Exception('customerID was not passed as an argument to the script or was not a valid customerID. This field is REQUIRED');
}

list($textInput,$hiddenInput) = array(Closures::formInput('text'),Closures::formInput('hidden'));

foreach(array('h1','label','br') as $element) $$element = Closures::html($element);

echo $h1('View/Update Customer');
?>
<form action="index.php" method="post" id="customer_form">
<?php

$customer = DB::PDO()->query(sprintf('SELECT * FROM customers WHERE customerID = %d',$_POST['customerID']));
$customer = $customer->fetchObject());
}

echo $hiddenInput('action','update_customer');
echo $label('First Name: '),$textInput('firstName',$customer->firstName),$br;
echo $label('Last Name: '),$textInput('lastName',$customer->lastName),$br;
echo $label('Address: '),$textInput('address',$customer->address),$br;
echo $label('City: '),$textInput('city',$customer->city),$br;
//... and so on
?>
<input type="submit" value="Update Customer" />
<br /> <br />
</form>
<p><a href="index.php?action=list_customers">Search Customers</a></p>

[/code]
Copy linkTweet thisAlerts:
@kay19authorNov 22.2011 — thanks for the reply. I indeed have to revamp this code. Thanks for the sample code! I'll rework on this
Copy linkTweet thisAlerts:
@kay19authorNov 23.2011 — Alright, I just finished it 100%. Thanks ?. Here's my code for future reference to others.
[code=php]<?php include '../view/header.php';
include ( "connect.php");?>
<div id="main">
<?php require('../model/database.php');
require('../model/customer_db.php');?>
<?php


if(isset($_GET['customerID']))
{
$customerID = $_GET['customerID'];
if (isset($_POST['submit']))
{
$u = "UPDATE customers SET firstName = '$_POST[fName]', lastName = '$_POST[lName]', address = '$_POST[add]', city = '$_POST[ci]', state= '$_POST[st]', countryCode = '$_POST[co]', postalCode = '$_POST[po]', phone = '$_POST[ph]', email = '$_POST[em]', password = '$_POST[pa]' WHERE customerID = $customerID" ;

mysql_query($u) or die (mysql_error() );


}

$sql = "SELECT * FROM customers WHERE customerID =$customerID";
$result=mysql_query($sql);


//echo "<pre>{$sql}</pre>";
if (mysql_num_rows($result)== 1){
$row = mysql_fetch_assoc($result);
//deleted the whole column list of row

?>

<h1>View/Update Customer</h1>
<form method="post" action ="customer_update.php?customerID=<?php echo $row['customerID']?>">


First Name:<input type ="text" name ="fName" value ="<? echo $row['firstName'] ?>"/> <br />
Last Name:<input type ="text" name ="lName" value ="<? echo $row['lastName'] ?>"/> <br />
address:<input type ="text" name ="add" value ="<?php echo $row['address'] ?>"> <br />
city:<input type ="text" name ="ci" value ="<?php echo $row['city'] ?>"> <br />
state:<input type ="text" name ="st" value ="<?php echo $row['state'] ?>"> <br />
country code:<input type ="text" name ="co" value ="<?php echo $row['countryCode'] ?>"> <br />
postal code:<input type ="text" name ="po" value ="<?php echo $row['postalCode'] ?>"> <br />
phone:<input type ="text" name ="ph" value ="<?php echo $row['phone'] ?>"> <br />
email:<input type ="text" name ="em" value ="<?php echo $row['email'] ?>"> <br />
password:<input type ="text" name ="pa" value ="<?php echo $row['password'] ?>"> <br />

<input type ="submit" name ="submit" value ="Update">
<input type ="hidden" name ="edit" value ="1">


</form>
<p><a href="index.php?action=list_customers">Search Customers</a></p>
<?php
}
}

?>
</div>
<?php include '../view/footer.php'; ?>[/code]
×

Success!

Help @kay19 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...