jseaman12
asked on
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
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
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 >';
}
?>
<?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
{
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
}
?>
ASKER
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_or ders") 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>
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",
mysql_select_db("thstew_or
$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>
This should work:
<?php
//Select info from MySQL
mysql_connect("localhost", "thstew_jseaman", "morgan22") or die(mysql_error());
mysql_select_db("thstew_or ders") 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>
<?php
//Select info from MySQL
mysql_connect("localhost",
mysql_select_db("thstew_or
$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>
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_or ders") 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>
<?php
//Select info from MySQL
mysql_connect("localhost",
mysql_select_db("thstew_or
$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>
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_or ders") 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_qu ery("SELEC T 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>
<?php
//Select info from MySQL
mysql_connect("localhost",
mysql_select_db("thstew_or
if(isset($_POST['po']))
{
mysql_query("UPDATE oetable_backup SET po = '$_POST[po]' WHERE po='245980'");
}
$result = mysql_fetch_array(mysql_qu
?>
<form action="edit.php" method="POST">
PO:<input type="text" name="po" value="<?php echo $result[0]; ?>" />
<input type="submit" value="Update DB" />
</form>
ASKER
This worked. Thank You very much.
-Jeff
-Jeff
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<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.