MySQL Forums
Forum List  »  PHP

problem in extracting data from the mysql using php
Posted by: sarah alegado
Date: June 24, 2005 03:05AM

I'm a beginner in php/mysql .
i did a basic extracting of data from the mysql structurally almost identical to add.php function with the difference that it pre-fills the form with the data that is already in the database; the record number is indicated by the variable $id which is passed to it by the edit link in listcontacts.php); otherwise does the same job as add.php but updates the data instead of inserting it. I have two tables: user and branches table. The one i extracted data is the user table but i have difficulty in creating a query that will select branches in the branch table if the user will edit the branch field and will save it in the user table.below is my initial code.
[php]<?php

session_start();

echo "<script>alert('$_GET[id]')</script>";

// get agent id

$id = $_GET['id'];

if (!empty($id))
{
$_SESSION['recordId']=$id;

} else
{
$id = $_SESSION['recordId'];
}



// if the script has been called without ID or the user hit "Cancel" just return to listing

if (empty($id) || isset($_POST['cancel'])) {

Header("Location: listagents.php");

exit;

}



include('dbconnect.php');

// run this only, once the user has hit the "OK" button

if (isset($_POST['ok'])) {

// assign form inputs
$id = $_POST['id'];

$password = $_POST['password'];

$branch_location = $_POST['branch_location'];

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$email = $_POST['email'];
$reg = $_POST['reg'];
$stat = $_POST['stat'];





// validate inputs

if ( !empty($password) && !empty($branch_location) ) {

// add member to database

$query = "UPDATE user SET _password='".$password."', branch_location='".$branch_location."',_fname='".$firstname."',_lname='".$lastname."',_emailadd='".$email."',_logtime='".$reg."' WHERE _id='$id' AND _status='1'";

// $result = $database->query($query);
$result = mysql_query($query, $connection);

// $database->disconnect();

mysql_close();


Header("Location: listagents.php");

exit;

}

else {

$error = true; // input validation failed
$err=mysql_error();
print $err;
exit();


}

}

else { // read member data from database

$query = "SELECT * FROM user WHERE _id='".$id."'";

// $result = $database->query($query);
$result = mysql_query($query, $connection);

// $contact = $result->fetchRow(DB_FETCHMODE_ASSOC,0);
//$agents=mysql_num_rows($result);
while ($agents = mysql_fetch_array($result)) {

// echo "<p>",$row['id'],": ",$row['manufacturer'];



// $database->disconnect();
mysql_close();



$id = $agents['_id'];

$password = $agents['_password'];

$branch_location= $agents['branch_location'];

$firstname = $agents['_fname'];

$lastname = $agents['_lastname'];

$email = $agents['_emailadd'];
$reg=$agents['_logtime'];
$status=$agents['_status'];
}

}

?>

<html>

<head>

<title>Edit Contact</title>

</head>

<body>

<a href="addagents.php">Add a Contact</a> | <a href="listagents.php">My Contacts</a>

<h1>Edit Contact</h1>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php

if ( $error && empty($id) ) {

echo '<span style="color:red">Error! Please enter a user id.</span><br>',"\n";

}

?>

USER ID:

<input name="id" type="text" value="<?php echo $id; ?>">

<br>

<?php

if ( $error && empty($password) ) {

echo '<span style="color:red">Error! Please enter a password.</span><br>',"\n";

}

?>

Password:

<input name="password" type="text" value="<?php echo $password; ?>">

<br>



<?php

if ( $error && empty($branch) ) {

echo '<span style="color:red">Error! Please enter a branch.</span><br>',"\n";

}

?>
Branch:

<input name="password" type="text" value="<?php echo $branch; ?>">

<br>
<?php

if ( $error && empty($firstname) ) {

echo '<span style="color:red">Error! Please enter a firstname.</span><br>',"\n";

}

?>

First Name:

<input name="firstname" type="text" value="<?php echo $firstname; ?>">

<br>
<?php

if ( $error && empty($lastname) ) {

echo '<span style="color:red">Error! Please enter a lastname.</span><br>',"\n";

}

?>

Last Name:

<input name="lastname" type="text" value="<?php echo $lastname; ?>">
<br>
<?php

if ( $error && empty($email) ) {

echo '<span style="color:red">Error! Please enter a email.</span><br>',"\n";

}

?>

E-mail:

<input name="email" type="text" value="<?php echo $email; ?>">

<br>

<input type="hidden" name="reg" value="<?php echo $reg; ?>">
<input type="hidden" name="stat" value="<?php echo $status; ?>">

<input type="submit" name="ok" value=" OK ">

<input type="submit" name="cancel" value="Cancel">

</form>

</body>

</html>


[/php]

Options: ReplyQuote


Subject
Written By
Posted
problem in extracting data from the mysql using php
June 24, 2005 03:05AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.