/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] creating links from mysql output

Hey All,

I want to be able to take the output of a table of data and link to a particular field of it so that if the user wants to edit the data they can. Each record has a unique ID called refnum. This is a keyed primary field in the db. By selecting this the user needs to able to edit that particular record and then resend it back into the database by using a sql “alter table” command.

here is the table creation page:

[code=php]
<?php
$result = $db->query($query);
$num_results = $result->num_rows;
echo ‘<p>Number of Records found: ‘.$num_results.'</p>’;
for ($i=0; $i < $num_results; $i++)
{
$row = $result->fetch_assoc();
//echo ‘<p><strong>Record #’ . ($i+1) .'</p>’;
echo ‘<table border=”2″>’;
echo ‘<tr>’;
echo ‘<tbody bgcolor=”lightgreen”>’;
echo ‘<td><strong>Record #</td>’;
echo ‘<td><strong>Name</td>’;
echo ‘<td><strong>ID</td>’;
echo ‘<td><strong>Server</td>’;
echo ‘<td><strong>Event</td>’;
echo ‘<td><strong>Database</td>’;
echo ‘<td><strong>Timestamp</td>’;
echo ‘<td><strong>Completed Date</td>’;
echo ‘<td><strong>Reference</td>’;
echo ‘</tr>’;
echo ‘<tr>’;
echo ‘<tbody bgcolor=”lightyellow”>’;
echo ‘<td>’ . ($i+1) .'</td>’;
echo ‘<td>’.stripslashes($row[‘name’]).'</td>’;
echo ‘<td>’.stripslashes($row[‘id’]).'</td>’;
echo ‘<td>’.stripslashes($row[‘server’]).'</td>’;
echo ‘<td>’.stripslashes($row[‘event’]).'</td>’;
echo ‘<td>’.stripslashes($row[‘dbname’]).'</td>’;
echo ‘<td>’.stripslashes($row[‘datetime’]).'</td>’;
echo ‘<td>’.stripslashes($row[‘wdate’]).'</td>’;
echo ‘<td> ‘.stripslashes($row[‘refnum’]).'</td>’;
echo ‘</tbody></tr></table>’;
echo ‘<strong>Description: <br /></strong>’;
echo ‘<textarea rows=”15″ cols=”100″ “12”>’;
echo str_replace(“<br />”,””,stripslashes($row[‘description’]));
echo ‘</textarea>’;
echo ‘<br />’;
echo ‘<br />’;
echo ‘<hr style=”width: 100%; height: 2px;”>’;
}
?>[/code]

How do I create the array ( I am assuming this would be the best way) and what would be the best way to edit the page via this method? tia!

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderMar 21.2007 — Why not just wrap the table in a form and echo your values pulled from the database in to an input field.
Copy linkTweet thisAlerts:
@Major_HootersauthorMar 21.2007 — Sorry, I dont understand what your saying.

I have the output in table form with each table being a different data set from the db. When the table outputs the data I would like to have the refnum highlighted (<a href>) so that the user can click on it and then be sent to a page to edit the info and resubmitted back into the db.

Please clarify what you are saying and I will be happy to try anything that will allow this functionality into the db. thanks again!!!
Copy linkTweet thisAlerts:
@MrCoderMar 21.2007 — Im saying, why not just do it all within one page.

By wraping the table in a form and then changing lines like so..
[code=php]
echo '<td>'.stripslashes($row['name']).'</td>';
// would become
echo '<td><input name="name" type="text" value="'.stripslashes($row['name']).'" /></td>';
[/code]


.. you could then post the edited values from this form and update the database as you like.

This is how I do almost all my editing of data.

Populate, Display, Submit, Update.
Copy linkTweet thisAlerts:
@Major_HootersauthorMar 21.2007 — OK, I think I see what you are saying.

The way you have it the data would be editable from the output. Right? So how would you handle the "re-inputing" of the data?

I guess that's where the wrapping it up in a form would be the ticket. But how would it know which data set to edit?
Copy linkTweet thisAlerts:
@wernergillwald0Mar 21.2007 — The PHP Code as above has the demanded feature but it's mistaken with HTTP -- there one should work on .
Copy linkTweet thisAlerts:
@MrCoderMar 21.2007 — OK, I think I see what you are saying.

The way you have it the data would be editable from the output. Right? So how would you handle the "re-inputing" of the data?

I guess that's where the wrapping it up in a form would be the ticket. But how would it know which data set to edit?[/QUOTE]


Pass a hidden type input with a row ID value within each form, one for each table.
Copy linkTweet thisAlerts:
@PlainSenseMar 21.2007 — So basically, you just want to edit a record - right?

Maybe this will give you some ideas:
[code=php]<?
$conn = mysql_connect("localhost","username","password");
mysql_select_db("database",$conn) or die( "Unable to select database");


if(!isset($cmd))
{
//display all the events
$result = mysql_query("select * from humor order by id");

//run the while loop that grabs all the items for display
while($r = mysql_fetch_array($result))
{
//grab the event title and the id of the events
$title=$r["title"];//take out the title
$article=$r["article"];//take out the article
$id=$r["id"];//take out the id
$author=$r["author"];//take out the title

echo "

<table width='95%' cellspacing=0 cellpadding=3>";
<tr><td width='20%' align=right><font face=arial size=2><b>Id #</b>:&nbsp;&nbsp;</font></td><td width='80%'><font face=arial size=2>$id</font></td></tr>";
<tr><td width='20%' align=right><font face=arial size=2><b>Title</b>:&nbsp;&nbsp;</font></td><td width='80%'><font face=arial size=2>$title</font></td></tr>";


<tr><td width='20%' align=right valign=top><font face=arial size=2>&nbsp;</font></td><td width='80%'><font face=arial size=2><a href='edithumor.php?cmd=edit&id=$id'>Edit</a></font></td></tr>";
<tr><td colspan=2 style='border-top:1px solid #666666'><font face=arial size=2>&nbsp;</font></td></tr>";
</table>

";

}
}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM humor WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);

?>

<center>
<table width="95%" cellspacing=0 cellpadding=3 border=0>
<tr>
<td>
<form action="edithumor.php" method="post">

<input type=hidden name="id" value="<?php echo $myrow['id'] ?>">

Title:<br />
<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow['title'] ?>" SIZE=50><br /><br />

Author:<br />
<INPUT TYPE="TEXT" NAME="author" VALUE="<?php echo $myrow['author'] ?>" SIZE=50><br /><br />

Article:<br /><TEXTAREA NAME="article" ROWS=30 COLS=70><?php echo $myrow['article'] ?></TEXTAREA><br /><br />


<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="submit">
</form>
</td>
</tr>
</table>
</center>

<? } ?>


<?

if ($_POST["$submit"])
{
$title = $_POST['title'];
$author = $_POST['author'];
$article = $_POST['article'];


$sql = "UPDATE humor SET title='$title', author='$author', article='$article' WHERE id=$id";

$result = mysql_query($sql);
echo "

<br /><br /><p align='center'><span style='color:red'>Thank you! Information updated.</span><br /><br /><a href='edithumor.php'>Edit Another Humorous Article</a></p>

";

}
}


?>[/code]
Copy linkTweet thisAlerts:
@wernergillwald0Mar 23.2007 — 
  • - at www.verticalresponse.com, p.e. are the facilities to change the codes from the old http to the php instead of new http - but it needs practice or skills -
  • Copy linkTweet thisAlerts:
    @Major_HootersauthorMar 23.2007 — Thanks for submitting that!!

    Where is edithumor.php?

    Your right, it does give me some ideas to work this.
    Copy linkTweet thisAlerts:
    @benifactorMar 23.2007 — im thinking you should probably replace edithumor.php with the link to your edit page???
    Copy linkTweet thisAlerts:
    @PlainSenseMar 24.2007 — This is edithumor.php
    Copy linkTweet thisAlerts:
    @Major_HootersauthorMar 27.2007 — It's hard to tell where one part starts and another begins

    you have numerous <? and ?> some connect up and some dont. In some places you refer to humor.php but you said this is humor.php.

    Sorry for being a noob but as a whole what you posted is confusing a little. Could you try again, as I think I see what your trying to do. I want to test this with what I have (db, etc) but cant seem to follow what you posted.

    thanks!
    Copy linkTweet thisAlerts:
    @benifactorMar 27.2007 — it seem the page is linking to itself, therefore you click a link to edit another one instead of pressing refresh.. ect..

    by looking at his code, all of his <? and ?> are in order and where they need to be. he has so many because a script as complicated as you need has many breaks between php and html. if you could give us an example of how this is not working we may be able to help you further.
    Copy linkTweet thisAlerts:
    @Major_HootersauthorApr 09.2007 — Ok, I didn't want to leave this thread hanging so after much trial and more error I finally got it to work. I dont have it edit record on the same page. I use a href with the refnum to pass it to the next page and then I just $_GET['ref'] from the edit page. It works well enough for now, so I thought I would post what I did. THe earlier post of the edit_humor.php helped alot. THanks!!

    [code=php]
    mt17# more edit_record.php
    <head>

    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
    <title>PROSPECT</title>


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <meta content="" name="author">

    </head>
    <body bgcolor="#EBEBEA">

    <!-- ############################################################################# -->

    <?php
    $authUser = $_SERVER['PHP_AUTH_USER'];
    $firstTwo = substr($authUser, 0, 2);
    $lastPart = substr($authUser, 2);
    $upperTwo = strtoupper($firstTwo);
    $user = $upperTwo.$lastPart;

    if(is_numeric($_GET['ref']))
    {
    $ref = $_GET['ref'];
    }

    ?>

    <!-- ############################################################################# -->
    <table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2" >
    <tbody>

    <tr>

    <td style="text-align: center; background-color: rgb(51, 153, 153);"><big style="font-weight: bold;">
    Edit Record Description Page:</big></td>

    </tr>

    </tbody>
    </table>
    <style="text-align: center; background-color: rgb(51, 153, 153);"><style="font-weight:
    bold;">You are logged in as: <?php echo $user ?>

    <!-- ############################################################################# -->
    <?php
    if (!isset($_POST['submit'])) {

    $refnum = $_GET['ref'];

    @ $db = new mysqli('localhost', 'blah', 'blah', 'blah');
    if (mysqli_connect_errno())
    {
    echo 'ERROR: Could not connect to database. Check that the db is running and that the server is up.
    ';
    exit;
    }

    ?>
    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
    <?php
    $query = "select * from sdata where refnum=$ref";

    $result = $db->query($query);
    $num_results = $result->num_rows;
    // echo '<p>Number of Records found: '.$num_results.'</p>';
    for ($i=0; $i < $num_results; $i++)
    {
    $refnum = $_GET['ref'];
    $row = $result->fetch_assoc();
    //echo '<p><strong>Record #' . ($i+1) .'</p>';
    echo '<table border="3">';
    echo '<tr>';
    echo '<tbody bgcolor="lightgreen">';
    //echo '<td><strong>Record #</td>';
    echo '<td><strong>Name</td>';
    echo '<td><strong>ID</td>';
    echo '<td><strong>Server</td>';
    echo '<td><strong>Event</td>';
    echo '<td><strong>Database</td>';
    echo '<td><strong>Timestamp</td>';
    echo '<td><strong>Completed Date</td>';
    echo '<td><strong>Reference</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<tbody bgcolor="lightyellow">';
    //echo '<td>' . ($i+1) .'</td>';
    echo '<td>'.stripslashes($row['name']).'</td>';
    echo '<td>'.stripslashes($row['id']).'</td>';
    echo '<td>'.stripslashes($row['server']).'</td>';
    echo '<td>'.stripslashes($row['event']).'</td>';
    echo '<td>'.stripslashes($row['dbname']).'</td>';
    echo '<td>'.stripslashes($row['datetime']).'</td>';
    echo '<td>'.stripslashes($row['wdate']).'</td>';
    echo '<td>'.stripslashes($row['refnum']).'</td>';
    echo '</tbody></tr></table>';
    echo '<strong>Description: <br /></strong>';
    echo '<textarea name="description" rows="15" cols="100" "12">';
    echo str_replace("<br />","",stripslashes($row['description']));
    echo '</textarea>';
    echo '<br />';
    }
    $result->free();
    $db->close();

    echo '<input type="hidden" name="aref" value="'.$refnum.'">';
    echo '<input name="submit" value=" Commit Edit" type="submit" style="color: rgb(204,0,0)">';
    echo '</form>';
    }

    if ($_POST['submit']) {

    $description = $_POST['description'];
    $refnum = $_POST['aref'];

    @ $db = new mysqli('localhost', 'blah', 'blah', 'blah');
    $sql = "update sdata set description='".$description."' where refnum=" .$refnum;
    $result = $db->query($sql);

    if($result === FALSE)
    {
    die("Query failed: $sql <br>n".$db->error);
    }
    if ($result)
    {
    echo '<br /><strong><center>';
    echo $db->affected_rows.' record confirmed updated and submitted into the data base.
    ';
    echo '<br /></center></strong><br /><br /><br />';
    echo '<div style="text-align: center; height: 137px;"><a href="https://www.logsa.army.mil/"><img style="bord
    er: 0px solid ; width: 720px;height: 128px;" alt="" src="https://mt17.logsa.army.mil/edit_test/images/offici
    al_logsa_banner.jpg"></a><br>';
    }
    }
    $result->free();
    $db->close();

    ?>
    </body>
    [/code]


    Thanks again to all that replied!? :rolleyes: ?
    ×

    Success!

    Help @Major_Hooters 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.25,
    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,
    )...