...

View Full Version : Retrieving and Updating Records in Tables



CoolAsCarlito
07-30-2008, 06:45 PM
I have a script called addtitle located at kansasoutlawwrestling.com/addtitle.com and I have it inserting the information into the table but I have another script called edittitle which is located at kansasoutlawwrestling.com/edittitle which is the same script but what happens with edit title is that it will retrieve the values from the table and you can edit the fields and then you'll click a submit button to save changes to the title but don't know how to do that. Because in this book I own it talks about retrieving and updating and don't know how I need to do that.

Fou-Lu
07-30-2008, 08:44 PM
Are you referring to SQL mechanisms? If so, this is what you do.
You have a record you want to edit. The page that redirects you to the edit page would contain a link that has the primary (or candidate) key identifier as a part of the querystring, so that edittitle.php receives id=4 for example. Fetching a records is a simple select statement where the primary key (or candidate) is equal to the provided value. With this you construct a form passing the key value in as a hidden field. If the hidden field exists, run an update command, otherwise an insert. This is kinda hard to explain, so let me try to demonstrate it with some simple code:
listtitles.php <-- Simply lists all of the available titles


<?php
include 'yourconnection.php';
$aTitles = array();
$sTitlesListSQL = "SELECT * FROM `titlesTable`";
$sTitlesList = mysql_query($sTitlesListSQL) || die (mysql_error());
if (mysql_num_rows($sTitlesList) <= 0)
{
die ('No records to display.');
}
while ($row = mysql_fetch_assoc($sTitlesList))
{
printf("<a href=\"modifytitle.php?id=%d\">%s</a>\n", (int)$row['id'], $row['title']);
}
?>

Modifytitle.php


<?php

if (isset($_POST['submit']))
{
$sUpdateSQL = '';
if (isset($_POST['iID']) && !empty($_POST['iID'])
{
// Update
$sUpdateSQL = "UPDATE `titleTable` SET `title` = '" . mysql_real_escape_string($_POST['txtTitle']) . "' WHERE `id` = " . (int)$_POST['iID'];
}
else
{
$sUpdateSQL = "INSERT INTO `titleTable` (`title`) VALUES ('" . mysql_real_escape_string($_POST['txtTitle']) . "')";
}
mysql_query($sUpdateSQL) || die(sprintf("Cannot update database (%d): %s", mysql_errno(), mysql_error()));
}

$sTitle = '';
$iID = null;
$sIDField = '';

if (isset($_GET['id']) && !empty($_GET['id']))
{
$sGetTitle = "SELECT * FROM `titlesTable` WHERE `id`=" . (int)$_GET['id'];
if (mysql_num_rows($sGetTitle) == 1)
{
$row = mysql_fetch_assoc($sGetTitle);
$sTitle = $row['title'];
$iID = (int)$row['id'];
$sIDField = '<input type="hidden" name="iID" value="' . $iID . '" />
}
}

$form = <<<FORM
<form action="modifytitle.php" method="post">
<fieldset>
$sIDField
<input type="text" name="txtTitle" value="$sTitle" />
</fieldset>
<div>
<input type="submit" name="submit" value="Update" />
<input type="reset" name="reset" value="Reset" />
</div>
</form>
FORM;

?>


No idea if that actually works, but it should give you an idea. It constructs a self posting form that if the hidden iID field exists it tries to update record, and if it doesn't it simply inserts it.

Hope that answers what you are looking for.

CoolAsCarlito
07-30-2008, 08:50 PM
Well what I think I want to happen is have when you click on the link a link called Edit title it will bring up the edittitle and edittitle will have a drop down select field that when you select the title you want to edit it will post that titles respected data into the form fields below. Hope that makes sense

CoolAsCarlito
07-30-2008, 09:15 PM
But right now I'm getting this error:

Parse error: parse error, unexpected $ in /home/content/y/a/n/yankeefaninkc/html/edittitle.php on line 105



<?php
// Connects to your Database
$link = mysql_connect("?", "?", "?") or die(mysql_error());
mysql_select_db("?",$link) or die(mysql_error());

if (!mysql_select_db("?", $link)) {
echo 'Could not select database';
exit;
}

//This code runs if the form has been submitted
if (isset($_POST['edittitle'])) {

//This makes sure they did not leave any fields blank
if (!$_POST['titlename'] || !$_POST['type'] || !$_POST['status'] || !$_POST['champion'] || !$_POST['contender1'] || !$_POST['contender2'] || !$_POST['contender3'] ) {
die('You did not complete all of the required fields');
}

// This is where we call all the values from the fields
$query = 'SELECT * FROM titles';
if ($r = mysql_query ($query)) { //Run the query.


// now we insert it into the database
$insert = "INSERT INTO titles (titlename, type, status, champion, contender1, contender2, contender3)
VALUES ('".$_POST['titlename']."','".$_POST['type']."','".$_POST['status']."','".$_POST['champion']."','".$_POST['contender1']."','".$_POST['contender2']."','".$_POST['contender3']."')";
$add_title = mysql_query($insert,$link) or die(mysql_error());
?>


<h1>Title is been changed!</h1>

<?php
}
else
{
?>
<center>
<table border=1 cellpadding=5 cellspacing=0 width=350>
<tr><td>Contender #1:</td><td>
<?php
echo '<select>';
$data = mysql_query("SELECT wrestlingname FROM singles");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?>
</td></tr></table>
<center><table border=1 cellpadding=5 cellspacing=0 width=350>
<font color="#CC0000"><h2><center>Create a Title</h2></center></font>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr><td>Title Name:</td><td>
<input type="text" name="titlename" maxlength="60">
</td></tr>
<tr><td>Type:</td><td>
<select name="type">
<option>Singles</option><option>Tag Team</option><option>Stable</option></select>
</td></tr>
<tr><td>Status:</td><td>
<select name="status">
<option>Active</option><option>Inactive</option></select>
</td></tr>
<tr><td>Champion:</td><td>
<?php
echo '<select>';
$data = mysql_query("SELECT wrestlingname FROM singles");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?>
</td></tr>
<tr><td>Contender #1:</td><td>
<?php
echo '<select>';
$data = mysql_query("SELECT wrestlingname FROM singles");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?></td></tr>
<tr><td>Contender #2:</td><td>
<?php
echo '<select>';
$data = mysql_query("SELECT wrestlingname FROM singles");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?>
<tr><td>Contender #3:</td><td>
<?php
echo '<select>';
$data = mysql_query("SELECT wrestlingname FROM singles");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?>
<tr><th colspan=2><input type="submit" name="createtitle" value="Create Title"></th></tr></table>
</form>
<?php
?>

Fou-Lu
07-30-2008, 09:36 PM
You have an open } from an else clause. Thats not the error I would expect for it to trigger, but that does appear to be the cause, I would expect something more along the lines of an unexpected end of file, not an unexpected $.

CoolAsCarlito
07-30-2008, 09:43 PM
I'm confused by that. Are you not able to pinpoint where my error is?

Fou-Lu
07-30-2008, 09:44 PM
You're error is right here:


<h1>Title is been changed!</h1>

<?php
}
else
{
?>

There is no close for this else statement. It likely belongs at the bottom of the page.

CoolAsCarlito
07-30-2008, 09:50 PM
Now I'm getting this:

Parse error: parse error, unexpected $ in /home/content/y/a/n/yankeefaninkc/html/edittitle.php on line 101

CoolAsCarlito
07-30-2008, 10:35 PM
Okay so I deleted all but the form and restarted putting in the database and all the rest of the actual important php code and then I started getting errors. Anyone see the problem in my code:



<?php
// Connects to your Database
$link = mysql_connect("?", "?", "?") or die(mysql_error());
mysql_select_db("?",$link) or die(mysql_error());

if (!mysql_select_db("?", $link)) {
echo 'Could not select database';
exit;
}

//This code runs if the form has been submitted
if (isset($_POST['edittitle'])) {

//This makes sure they did not leave any fields blank
if (!$_POST['titlename'] || !$_POST['type'] || !$_POST['status'] || !$_POST['champion'] || !$_POST['contender1'] || !$_POST['contender2'] || !$_POST['contender3'] ) {
die('You did not complete all of the required fields');
}

// This is where we call all the values from the fields
$query = 'SELECT * FROM titles';
if ($r = mysql_query ($query)) { //Run the query.


// now we insert it into the database
$insert = "INSERT INTO titles (titlename, type, status, champion, contender1, contender2, contender3)
VALUES ('".$_POST['titlename']."','".$_POST['type']."','".$_POST['status']."','".$_POST['champion']."','".$_POST['contender1']."','".$_POST['contender2']."','".$_POST['contender3']."')";
$add_title = mysql_query($insert,$link) or die(mysql_error());
?>


<h1>Title is been changed!</h1>

CoolAsCarlito
07-30-2008, 11:55 PM
Here's what I have now:

http://kansasoutlawwrestling.com/edittitle.php

So now I just have to have it paste the respective info into the form fields when you select a title.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum