Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

PHP Edit fields

I want to be able retrieve data results from my mysql tables in a forms text box. I want to do this in PHP Programming.

I have a mysql database, it works. This process is going to be used if the end user needs to edit a field in the database.

Any help would be well appreciated.

Thanks,
Jeff
0
jseaman12
Asked:
jseaman12
  • 3
  • 2
  • 2
  • +1
1 Solution
 
gamebitsCommented:
Do you know how to retrieve data from the db and display it on the screen? If yes all you have to do is display it inside a textbox

<form action="edit.php" method="POST">
USERNAME: <input type="text" name="username" value="<?here echo result from db?>">
</form>

Of course you need to have the script to do the dit on the db and you should also have a primary key set up.
0
 
MasonWolfCommented:
I'm going with the assumption that your database table has a primary key named 'id'. Change it to the appropriate value for your table.

<?php
for($i = 0; $i < count($_POST['init_id']); $i++)
{
   $id = $_POST['init_id'][$i];
   $query = 'UPDATE Table SET ';
   foreach($_POST AS $key=>$values)
   {
       if($key == 'init_id') continue;
       $query .= $key.' = \''.$values[$i].'\',';
   }
   $query = substr($query, 0, -1).' WHERE id = \''.$id.'\' LIMIT 1';
   mysql_query($query);
}
$results = mysql_query("SELECT * FROM Table");
if(mysql_num_rows($results) > 0)
{
    echo '<form method="post" action=""><table><tr>';
    $result = mysql_fetch_assoc($results);
    $cols = count($result);
    foreach($result AS $key => $value)
    {
        echo '<th>'.$key.'</th>';
    }
    echo '</tr>';
    do
    {
    echo '<tr><input type="hidden" name="init_id[]" value="'.$result['id']." />';
    foreach($result AS $key=> $value)
    {
         echo '<td><input type="text" name="'.$key.'[]" value="'.$value.'"/></td>';
    }
    echo '</tr>';
    while($result = mysql_fetch_assoc($results));
    echo '<tr><td align="center" colspan="'.$cols.'"><input type="submit" value="Update Fields" /></td></tr></table></form>';
}
?>
0
 
jseaman12Author Commented:
Can't I just make this script simple?

I get no answer in the textbox? Is this because I need to use my primary_id in the script?
Thanks

<?php
//Select info from MySQL
mysql_connect("localhost", "thstew_jseaman", "morgan22") or die(mysql_error());
mysql_select_db("thstew_orders") or die(mysql_error());

$result = mysql_query("SELECT po FROM oetable_backup where po='245980'");
?>

<form action="edit.php" method="POST">
PO:<input type="text" name="po" value="<echo $result>
</form>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nizsmoDeveloperCommented:
This should work:


<?php
//Select info from MySQL
mysql_connect("localhost", "thstew_jseaman", "morgan22") or die(mysql_error());
mysql_select_db("thstew_orders") or die(mysql_error());

$result = mysql_query("SELECT po FROM oetable_backup where po='245980'");
?>

<form action="edit.php" method="POST">
PO:<input type="text" name="po" value="<?php echo $result; ?>">
</form>
0
 
nizsmoDeveloperCommented:
Sorry my mistake, the following should really work :


<?php
//Select info from MySQL
mysql_connect("localhost", "thstew_jseaman", "morgan22") or die(mysql_error());
mysql_select_db("thstew_orders") or die(mysql_error());

$result = mysql_query("SELECT po FROM oetable_backup where po='245980'");
$row = mysql_fetch_row($result);
?>

<form action="edit.php" method="POST">
PO:<input type="text" name="po" value="<?php echo $row[0]; ?>">
</form>
0
 
MasonWolfCommented:
Please specify these things in the actual question. I read "retrieve data results from my mysql tables in a forms text box" and I assumed you wanted the whole database. (I was actually really pleased with the approach I came up - thought I was pretty clever :) ) Anyway, it wasn't what you wanted. But I think I have one that'll work:

<?php
//Select info from MySQL
mysql_connect("localhost", "thstew_jseaman", "morgan22") or die(mysql_error());
mysql_select_db("thstew_orders") or die(mysql_error());

if(isset($_POST['po']))
{
    mysql_query("UPDATE oetable_backup SET po = '$_POST[po]' WHERE po='245980'");
}
$result = mysql_fetch_array(mysql_query("SELECT po FROM oetable_backup where po='245980'"));
?>

<form action="edit.php" method="POST">
PO:<input type="text" name="po" value="<?php echo $result[0]; ?>" />
<input type="submit" value="Update DB" />
</form>
0
 
jseaman12Author Commented:
This worked. Thank You very much.

-Jeff
0
 
MasonWolfCommented:
I'm glad it worked for you. Please accept my answer and close the question.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now