Help - Search - Members - Calendar
Full Version: Edit and Update a certain record
Weborum Webmaster Forum > Web Page Design > PHP
Joe
My code;

PHP
<?php
session_start
();

if(!isset(
$_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}

include
'config.php';

$edit = $_GET['edit'];
if(isset(
$_GET['edit'])) {

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM LinkCounter WHERE ID = '$edit'";
$result = mysql_query ($requete,$db);

while (
$row = mysql_fetch_assoc($result))
{
$ID = $row['ID'];
$name = $row['name'];
$address = $row['address'];
$description = $row['description'];
$count = $row['count'];

$name = stripslashes($row['name']);
$address = stripslashes($row['address']);
$description = stripslashes($row['description']);

?>
<form method="post" action="<?php $_SERVER['SCRIPT_NAME'] ?>">

<p><label for="name">Site Name: <br /><input type="text" name="name" id="name" size="40" value="'.$name.'" tabindex="1" /></label></p>
<p><label for="address">Site URL: <br /><input type="text" name="address" id="address" size="40" value="'.$address.'" tabindex="2" /></label></p>
<p><label for="count">Count: <br /><input type="text" name="count" id="count" size="10" value="'.$count.'" tabindex="3" /></label></p>
<p><label for="desc">Short Description: <br />
<textarea name="description" rows="4" cols="50" id="description" tabindex="4">'.$description.'</textarea></label></p>

<p><input type="submit" name="submit" value="Update" /> <input type="reset" name="reset" value="Reset" /></p>
</form>
</div>
<?
}
}
?>

<?php
if(isset($_POST['submit'])) {

include
'config.php';

$ID = $_SESSION['ID'];
$name = $_POST['name'];
$address = $_POST['address'];
$count = $_POST['count'];
$description = $_POST['description'];

$name = safeAddSlashes($name);
$address = safeAddSlashes($address);
$description = safeAddSlashes($description);

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "UPDATE linkcounter SET name = '$name', address = '$address', count = '$count', description = '$description' WHERE ID = '$ID'";
$result = mysql_query ($requete,$db);
mysql_free_result($result);
header('Location: '.$domain.'editlink.php');
exit();
}
?>



Now I know this is very very very wrong but I am uncertain how to make the code work how I want ... separatly this code will work, but to save having masses of files I wanted to try and compress my code.

The top part is grabbing the ID of the link to be edited and the bottom part updates the edited product.

How would I go about making these two scripts work together in perfect harmony? Maybe I've just had a few too many timo.gif tonight but I can't even imagine how I can get this working...

Cheers
aviansuicide
General form for a form/processing singular file:

PHP

<?
if($_POST["submit"])
{
/* Processing */
}
else
{
/* Form */
}
?>



Thus:
PHP

<?php
session_start
();

if(&
#33;isset($_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}

include
'config.php';

$edit = $_GET['edit'];
if(isset(
$_GET['edit'])) {

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM LinkCounter WHERE ID = '$edit'";
$result = mysql_query ($requete,$db);

while (
$row = mysql_fetch_assoc($result))
{
$ID = $row['ID'];
$name = $row['name'];
$address = $row['address'];
$description = $row['description'];
$count = $row['count'];

$name = stripslashes($row['name']);
$address = stripslashes($row['address']);
$description = stripslashes($row['description']);

?>
<form method="post" action="<?php $_SERVER['SCRIPT_NAME'] ?>">

<p><label for="name">Site Name: <br /><input type="text" name="name" id="name" size="40" value="'.$name.'" tabindex="1" /></label></p>
<p><label for="address">Site URL: <br /><input type="text" name="address" id="address" size="40" value="'.$address.'" tabindex="2" /></label></p>
<p><label for="count">Count: <br /><input type="text" name="count" id="count" size="10" value="'.$count.'" tabindex="3" /></label></p>
<p><label for="desc">Short Description: <br />
<textarea name="description" rows="4" cols="50" id="description" tabindex="4">'.$description.'</textarea></label></p>

<p><input type="submit" name="submit" value="Update" /> <input type="reset" name="reset" value="Reset" /></p>
</form>
</div>
<?
}
}
?>

<?php
if(isset($_POST['submit'])) {

include
'config.php';

$ID = $_SESSION['ID'];
$name = $_POST['name'];
$address = $_POST['address'];
$count = $_POST['count'];
$description = $_POST['description'];

$name = safeAddSlashes($name);
$address = safeAddSlashes($address);
$description = safeAddSlashes($description);

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "UPDATE linkcounter SET name = '$name', address = '$address', count = '$count', description = '$description' WHERE ID = '$ID'";
$result = mysql_query ($requete,$db);
mysql_free_result($result);
header('Location: '.$domain.'editlink.php');
exit();
}
?>



gets re-written as:

PHP

<?
session_start
();

if(&
#33;isset($_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}
include
'config.php';

if(
$_POST["submit"])
{
$ID = $_SESSION['ID'];
$name = $_POST['name'];
$address = $_POST['address'];
$count = $_POST['count'];
$description = $_POST['description'];

$name = safeAddSlashes($name);
$address = safeAddSlashes($address);
$description = safeAddSlashes($description);

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "UPDATE linkcounter SET name = '$name', address = '$address', count = '$count', description = '$description' WHERE ID = '$ID'";
$result = mysql_query ($requete,$db);
mysql_free_result($result);
header('Location: '.$domain.'editlink.php');
exit();

}
else
{
$edit = $_GET['edit'];
if(isset(
$_GET['edit'])) {

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM LinkCounter WHERE ID = '$edit'";
$result = mysql_query ($requete,$db);

while (
$row = mysql_fetch_assoc($result))
{
$ID = $row['ID'];
$name = $row['name'];
$address = $row['address'];
$description = $row['description'];
$count = $row['count'];

$name = stripslashes($row['name']);
$address = stripslashes($row['address']);
$description = stripslashes($row['description']);

?>
<form method="post" action="<?php $_SERVER['SCRIPT_NAME'] ?>">

<p><label for="name">Site Name: <br /><input type="text" name="name" id="name" size="40" value="'.$name.'" tabindex="1" /></label></p>
<p><label for="address">Site URL: <br /><input type="text" name="address" id="address" size="40" value="'.$address.'" tabindex="2" /></label></p>
<p><label for="count">Count: <br /><input type="text" name="count" id="count" size="10" value="'.$count.'" tabindex="3" /></label></p>
<p><label for="desc">Short Description: <br />
<textarea name="description" rows="4" cols="50" id="description" tabindex="4">'.$description.'</textarea></label></p>

<p><input type="submit" name="submit" value="Update" /> <input type="reset" name="reset" value="Reset" /></p>
</form>
</div>
<?
}
}
}
?>


It should also be noted that,since your only selecting a single row,a while loop is not needed..thus:

PHP

<?
session_start
();

if(&
#33;isset($_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}
include
'config.php';
if(
$_POST["submit"])
{
$ID = $_SESSION['ID'];
$name = $_POST['name'];
$address = $_POST['address'];
$count = $_POST['count'];
$description = $_POST['description'];

$name = safeAddSlashes($name);
$address = safeAddSlashes($address);
$description = safeAddSlashes($description);

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "UPDATE linkcounter SET name = '$name', address = '$address', count = '$count', description = '$description' WHERE ID = '$ID'";
$result = mysql_query ($requete,$db);
mysql_free_result($result);
header('Location: '.$domain.'editlink.php');
exit();

}
else
{
$edit = $_GET['edit'];
if(isset(
$_GET['edit'])) {

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM LinkCounter WHERE ID = '$edit'";
$result = mysql_query ($requete,$db);

$row = mysql_fetch_assoc($result);

$ID = $row['ID'];
$name = $row['name'];
$address = $row['address'];
$description = $row['description'];
$count = $row['count'];

$name = stripslashes($row['name']);
$address = stripslashes($row['address']);
$description = stripslashes($row['description']);

?>
<form method="post" action="<?php $_SERVER['SCRIPT_NAME'] ?>">

<p><label for="name">Site Name: <br /><input type="text" name="name" id="name" size="40" value="'.$name.'" tabindex="1" /></label></p>
<p><label for="address">Site URL: <br /><input type="text" name="address" id="address" size="40" value="'.$address.'" tabindex="2" /></label></p>
<p><label for="count">Count: <br /><input type="text" name="count" id="count" size="10" value="'.$count.'" tabindex="3" /></label></p>
<p><label for="desc">Short Description: <br />
<textarea name="description" rows="4" cols="50" id="description" tabindex="4">'.$description.'</textarea></label></p>

<p><input type="submit" name="submit" value="Update" /> <input type="reset" name="reset" value="Reset" /></p>
</form>
</div>
<?
}
}
?>



*yawn*

Edit: whoevers responsible for admining this forum should fix the issue with ! points in php code...it's annoying.
Joe
Thanks Will smile.gif

Much appreciated mate ...
aviansuicide
QUOTE(joe2kiss @ Mar 6 2005, 04:59 PM)
Thanks Will smile.gif

Much appreciated mate ...
[right][snapback]24124[/snapback][/right]


One thing joe....shouldn't the '. .'in the middle of the forms(to include variables) be <?=$var?> instead?you escaped php so the quotes don't matter.
leo
QUOTE
Edit: whoevers responsible for admining this forum should fix the issue with ! points in php code...it's annoying.[/


sure, but remember i'm not a scripting person so am not entirely sure what you mean laugh.gif, could you expand smile.gif
aviansuicide
Hm..if you want to zip up the forum files(the php ones,that is) and send them to me...I can look through and find where the problem is,and fix it for you.The forum is parsing !'s inside php code as their entity equivilant..which breaks the code.

for example

PHP

<?

if(&#33;isset($var))
{

}
?>



SHOULD be:

if(!isset($var))
{

}
leo
ahhh, see what you mean.

Well i added a mod to display the php coloring, i imagine it's something to do with that, i'll see if i can find it, i'll also have a quick look on my local test forum which doesn't have the mod installed smile.gif
Joe
Here it is;

PHP
<?php
ob_start
();
session_start();

if(&
#33;isset($_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}

include
'config.php';

if(
$_POST["submit"]) {

$ID = $_SESSION['ID'];
$name = $_POST['name'];
$address = $_POST['address'];
$count = $_POST['count'];
$description = $_POST['description'];

$name = safeAddSlashes($name);
$address = safeAddSlashes($address);
$description = safeAddSlashes($description);

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "UPDATE linkcounter SET name = '$name', address = '$address', count = '$count', description = '$description' WHERE ID = '$ID'";
$result = mysql_query ($requete,$db);
mysql_free_result($result);
header('Location: '.$domain.'links-edit.php');
exit();
}
else
{

$edit = $_GET['edit'];
if(isset(
$_GET['edit'])) {

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM linkcounter WHERE ID = '$edit'";
$result = mysql_query ($requete,$db);

$row = mysql_fetch_assoc($result);

$ID = $row['ID'];
$name = $row['name'];
$address = $row['address'];
$description = $row['description'];
$count = $row['count'];

$name = stripslashes($row['name']);
$address = stripslashes($row['address']);
$description = stripslashes($row['description']);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>

    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Joseph Skidmore" />
    <meta name="Copyright" content="Copyright (c) 2005 Joseph Skidmore" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="MSSmartTagsPreventParsing" content="true" />

    <title>Joe2Torials Content Management System Blog - edit record</title>
    
    <link href="index.css" rel="stylesheet" type="text/css" />

</head>
<body>

<div id="wrapper">

<div id="header"></div>

<div id="content">
    <div id="leftcolumn">
        <h1>Links - edit record</h1>
<div id="editlink">
<form method="post" action="<?php $_SERVER['SCRIPT_NAME'] ?>">

<p>Editing record number <strong><?=$ID?></strong>.</p>

<p><label for="name">Site Name: <br /><input type="text" name="name" id="name" size="40" value="<?=$name?>" tabindex="1" /></label></p>
<p><label for="address">Site URL: <br /><input type="text" name="address" id="address" size="40" value="<?=$address?>" tabindex="2" /></label></p>
<p><label for="count">Count: <br /><input type="text" name="count" id="count" size="10" value="<?=$count?>" tabindex="3" /></label></p>
<p><label for="desc">Short Description: <br />
<textarea name="description" rows="4" cols="50" id="description" tabindex="4"><?=$description?></textarea></label></p>

<p><input type="submit" name="submit" value="Update" /> <input type="reset" name="reset" value="Reset" /></p>
</form>
</div>
<?
}
}
?>
    </div>

<?php include 'sidebarfooter.php'; ?>
<?php ob_end_flush
(); ?>
aviansuicide
PHP

<?php
ob_start
();
session_start();

if(&
#33;isset($_SESSION['loggedin'])) {
   
header('Location: '.$domain.'index.php?error=1');
   exit();
}

include
'config.php';

if(
$_POST["submit"]) {

$ID = $_POST['ID'];
$name = $_POST['name'];
$address = $_POST['address'];
$count = $_POST['count'];
$description = $_POST['description'];

$name = safeAddSlashes($name);
$address = safeAddSlashes($address);
$description = safeAddSlashes($description);

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "UPDATE linkcounter SET name = '$name', address = '$address', count = '$count', description = '$description' WHERE ID = '$ID'";
$result = mysql_query ($requete,$db);
mysql_free_result($result);
header('Location: '.$domain.'links-edit.php');
exit();
}
else
{

$edit = $_GET['edit'];
if(isset(
$_GET['edit'])) {

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM linkcounter WHERE ID = '$edit'";
$result = mysql_query ($requete,$db);

$row = mysql_fetch_assoc($result);

$ID = $row['ID'];
$name = $row['name'];
$address = $row['address'];
$description = $row['description'];
$count = $row['count'];

$name = stripslashes($row['name']);
$address = stripslashes($row['address']);
$description = stripslashes($row['description']);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>

    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Joseph Skidmore" />
    <meta name="Copyright" content="Copyright (c) 2005 Joseph Skidmore" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="MSSmartTagsPreventParsing" content="true" />

    <title>Joe2Torials Content Management System Blog - edit record</title>
    
    <link href="index.css" rel="stylesheet" type="text/css" />

</head>
<body>

<div id="wrapper">

<div id="header"></div>

<div id="content">
    <div id="leftcolumn">
        <h1>Links - edit record</h1>
<div id="editlink">
<form method="post" action="<?=$_SERVER['PHP_SELF'] ?>">

<p>Editing record number <strong><?=$ID?></strong>.</p>

<p><label for="name">Site Name: <br /><input type="text" name="name" id="name" size="40" value="<?=$name?>" tabindex="1" /></label></p>
<p><label for="address">Site URL: <br /><input type="text" name="address" id="address" size="40" value="<?=$address?>" tabindex="2" /></label></p>
<p><label for="count">Count: <br /><input type="text" name="count" id="count" size="10" value="<?=$count?>" tabindex="3" /></label></p>
<p><label for="desc">Short Description: <br />
<textarea name="description" rows="4" cols="50" id="description" tabindex="4"><?=$description?></textarea></label></p>
<input type="hidden" name="ID" value="<?=$ID?>"/>

<p><input type="submit" name="submit" value="Update" /> <input type="reset" name="reset" value="Reset" /></p>
</form>
</div>
<?
}
}
?>
    </div>

<?php include 'sidebarfooter.php'; ?>
<?php ob_end_flush
(); ?>



I wonder if you realize the $_SESSION["ID"] isn't being set?
aviansuicide
QUOTE(leo @ Mar 6 2005, 05:48 PM)
ahhh, see what you mean.

Well i added a  mod to display the php coloring, i imagine it's something to do with that, i'll see if i can find it, i'll also have a quick look on my local test forum which doesn't have the mod installed smile.gif
[right][snapback]24133[/snapback][/right]


Indeed.It's definately the mod.
Joe
It works perfectly now thanks Will biggrin.gif specool.gif
leo
here you go will
aviansuicide
find these lines:

$txt = @html_entity_decode($txt, ENT_QUOTES);
$txt = str_replace("$", "$", $txt);
$txt = highlight_string($txt, 1);


add the bold:

$txt = @html_entity_decode($txt, ENT_QUOTES);
$txt = str_replace("$", "$", $txt);
$text = str_replace('put...the...entity...here','!',$txt);
$txt = highlight_string($txt, 1);

where I have the put...the...entity...here... put & # 3 3 ; without the spaces.
leo
ok found it and just about to replace it, just wanna confirm that it should be $text and not $txt
aviansuicide
sorry..typo...it should be $txt,yes.
leo
cool, cheers that did the trick smile.gif

i resubmitted joe's first post and the ! is there, cheers will smile.gif
aviansuicide
QUOTE(leo @ Mar 6 2005, 06:56 PM)
cool, cheers that did the trick  smile.gif

i resubmitted joe's first post and the !  is there, cheers will smile.gif
[right][snapback]24154[/snapback][/right]

PHP

<? echo 'No Problem Mate!'; ?>



XD
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.