...

View Full Version : Assigning field values from SQL queries



mike94
01-10-2012, 01:57 PM
So far I have a drop down box that shows a list of client names from the database... but I cant figure out how to make the clients name, email and password pop up to be changed.

The admin area is made from one index and multiple includes using domain extensions, therefore clicking links reloads the same page but with a different include.

This is what I am aiming for:
1) admin needs to change clients stored data
2) admin chooses client from drop down list
3) on submission the selected clients data is shown in a form
4) the admin can edit the data in the fields
5) the admin submits this form, and the clients info is updated.

I am stuck on step 3.

Thanks for any help in advance! (I am still self teaching)

I hope that makes sense, here's the code so far:


<?php
$conn = mysql_connect("localhost", "root", "");

if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("report")) {
echo "Unable to select report DB: " . mysql_error();
exit;
}

$sql = ("SELECT * FROM clients ORDER BY client DESC");

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}



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

include("config.php");

$client_name = ("SELECT client FROM clients WHERE client = '$client'");
$client_email = ("SELECT email FROM clients WHERE client = '$client'");
$client_password = ("SELECT password FROM clients WHERE client = '$client'");

$result1 = mysql_query($client_name);
$result2 = mysql_query($client_email);
$result3 = mysql_query($client_password);

?>
<div class="primary">
<form action="" method="post" target="_self" id="registerform">

<div>
<p>Client creation form</p>
</div>

<div>
<label>Client Name <span class="required">*</span></label>
<?php echo('<input name="client" type="text" id="client" value="'.$result1.'" />'); ?>
</div>

<div>
<label>Primary Email Address <span class="required">*</span></label>
<?php echo('<input name="email" type="text" id="email" value="'.$result2.'" />'); ?>
</div>

<div>
<label>Primary Password <span class="required">*</span></label>
<?php echo('<input name="password" type="password" id="password" value="'.$result3.'" />'); ?>
</div>

<div>
<input type="submit" value="Update Client" class="button" name="register2" id="register2">
</div>


</form>
<a style="font-size: 25px;" href="?d=1">Cancel and return</a>
</div>
<?php
;}
?>
<html>
<a style="font-size: 25px;" href="?d=1">Cancel and return</a><br /><br />
<section id="register" class="clearfix">

<div class="primary">
<form action="" method="post" target="_self" id="registerform">

<div>
<p>Please select the client you wish to edit:</p>
</div>

<div>
<label>Client Name<span class="required">*</span></label>
<?php
echo("<select name='client' id='client'>");
while ($row = mysql_fetch_assoc($result)) {
echo('<option>'.$row["client"].'</option>');
$client = $row["client"];
}
echo("</select>");
?>
</div>
<div>
<input type="submit" value="Edit Client" class="button" name="goedit" id="goedit">
</div>
</form>
<br />
<a style="font-size: 25px;" href="?d=1">Cancel and return</a>
</div>
</section>
</html>

abduraooft
01-10-2012, 02:06 PM
$client_name = ("SELECT client FROM clients WHERE client = '$client'");
$client_email = ("SELECT email FROM clients WHERE client = '$client'");
$client_password = ("SELECT password FROM clients WHERE client = '$client'");
That should be something like

$sql = "SELECT client,email,password FROM clients WHERE client = '$client'");
$result=mysql_query($sql) or die(mysqll_error());
and then you need to fetch the values from the result set using a function like mysql_fetch_assoc( (http://php.net/mysql_fetch_assoc)) or similar.

mike94
01-10-2012, 02:12 PM
That should be something like

$sql = "SELECT client,email,password FROM clients WHERE client = '$client'");
$result=mysql_query($sql) or die(mysqll_error());
and then you need to fetch the values from the result set using a function like mysql_fetch_assoc( (http://php.net/mysql_fetch_assoc)) or similar.

Thank you for your reply.

I genuinly knew this but I was then unsure how to seperate the client, email and password into seperate fields as values without having them assigned as seperate variables?

abduraooft
01-10-2012, 02:18 PM
Did you see the examples in the link embedded in my above post?

mike94
01-10-2012, 02:20 PM
Did you see the examples in the link embedded in my above post?

AHH sorry! Its like -5c here and ive been up 18 hours! haha no excuse...



while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}


Thank you :)

P.S. I gave you your 2,000th thank ;) HAHA

mike94
01-10-2012, 02:25 PM
How can I get the $client from the first form (the client selection drop down box?)


WHERE client = '$client'");

abduraooft
01-10-2012, 02:30 PM
First form? Please explain.

mike94
01-10-2012, 04:48 PM
First form? Please explain.

The first form is the drop down box to select the client to edit, then the second is the form that appears with the clients details ready for editing.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum