PDA

View Full Version : editting data with php from mysql


Tjobbe
February 17th, 2005, 15:29
I butcherred, i think the correct terminolgy is, the blog script that was posted on this forum, to try and use it to edit my database entries. the ammended code is here:


edit_entry.php

[syntax:f8fdbae251="php"]
<?php
ob_start();

include("dbconnect.php");


$get = mysql_query("SELECT * FROM coname ORDER BY id DESC LIMIT 0, 10;") or die("Mmm pie:" . mysql_error());
$get_number = mysql_num_rows($get);

if( $get_number > "0" ) {
echo "<div>";

while ( $row = mysql_fetch_object($get) ) {
echo "
<p>$row->logo <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->company <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->address1 <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->address2 <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->address3 <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->postcode <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->phone <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->email <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->web <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->inform <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
<p>$row->contact <a href=\"edit_entry.php?id=$row->id\">Edit Entry</a></p>
";
}

echo "</div>";
} else {echo "<i>There are no entries to display.</i>";}


if($_GET["id"]) {

$id = $_GET["id"];

$edit = mysql_query("SELECT * FROM coname WHERE id='".$id."'") or die("Mmm pie:" . mysql_error());

$editrow = mysql_fetch_object($edit);
echo "
<h3>Edit Database:</h3>

<form method=\"post\" action=\"edit.php\">
<input type=\"hidden\" name=\"edit\" value=\"edit\" />
<input type=\"hidden\" name=\"id\" value=\"$editrow->id\" />

logo*<br />
<input type=\"text\" name=\"logo\" value=\"$editrow->logo\" maxlength=\"50\" /><p />

company*<br />
<input type=\"text\" name=\"company\" value=\"$editrow->company\" maxlength=\"50\" /><p />

address1*<br />
<input type=\"text\" name=\"address1\" value=\"$editrow->address1\" maxlength=\"50\" /><p />

address2*<br />
<input type=\"text\" name=\"address2\" value=\"$editrow->address2\" maxlength=\"50\" /><p />

address3*<br />
<input type=\"text\" name=\"address3\" value=\"$editrow->address3\" maxlength=\"50\" /><p />

postcode*<br />
<input type=\"text\" name=\"postcode\" value=\"$editrow->postcode\" maxlength=\"50\" /><p />

phone*<br />
<input type=\"text\" name=\"phone\" value=\"$editrow->phone\" maxlength=\"50\" /><p />

email*<br />
<input type=\"text\" name=\"email\" value=\"$editrow->email\" maxlength=\"50\" /><p />

web*<br />
<input type=\"text\" name=\"web\" value=\"$editrow->web\" maxlength=\"50\" /><p />

inform*<br />
<input type=\"text\" name=\"inform\" value=\"$editrow->inform\" maxlength=\"50\" /><p />

contact*<br />
<input type=\"text\" name=\"contact\" value=\"$editrow->contact\" maxlength=\"50\" /><p />



<input type=\"text\" name=\"endate\" value=\"";echo date('M j, Y');echo "\" /><p />

<input type=\"reset\" value=\"Clear\"> <input type=\"submit\" value=\"Edit Entry\">
</form>

";


}

?>
[/syntax:f8fdbae251]

and
edit.php

[syntax:f8fdbae251="php"]<?php

if( $_POST["edit"] || $_POST["id"] ) {
include("dbconnect.php");

$edit = mysql_query("SELECT * FROM coname WHERE id='".$id."'") or die("Mmm pie:" . mysql_error());

$editrow = mysql_fetch_object($edit);


$logo = $_POST["logo"];
$company = $_POST["company"];
$address1 = $_POST["address1"];
$address2 = $_POST["address2"];
$address3 = $_POST["address3"];
$postcode = $_POST["postcode"];
$phone = $_POST["phone"];
$email = $_POST["email"];
$web = $_POST["web"];
$inform = $_POST["inform"];
$contact = $_POST["contact"];

$update = "UPDATE coname SET logo='".$logo."', company='".$company."', address1='".$address1."', address2='".$address2."', address3='".$address3.", postcode='".$postcode.", phone='".$phone.", email='".$email.", web='".$web.", inform='".$inform.", contact='".$contact."' WHERE id='".$editrow->id."' ";
mysql_query($update) or die("This isn't working well..." . mysql_error());

echo "Success! data updated.! Page refreshing now...";
header("Refresh: 3, URL=../index.php");

}

?>[/syntax:f8fdbae251]




basicly, when it displays the fields to edit, it doesnt specify and ID number, so the link goes nowhere, how do I fix this?

Anoop
February 18th, 2005, 18:59
The id is passing as a hidden field in the form. Can you try replacing the $id in the select query in edit.php with $_POST'['id']. Actually there is no need for that select statement there in edit.php. You can use :

$update = "UPDATE coname SET logo='".$logo."', company='".$company."', address1='".$address1."', address2='".$address2."', address3='".$address3.", postcode='".$postcode.", phone='".$phone.", email='".$email.", web='".$web.", inform='".$inform.", contact='".$contact."' WHERE id='".$_POST[id]."' ";

Tjobbe
February 20th, 2005, 14:04
Thats didn't do anything Anoop, thanks for the reply anyway. on edit.php I'm still getting a link without an id.

Tjobbe
February 21st, 2005, 15:30
I've done some more reading into this and I'm still lost, how would I, ignoring the above, retrieve all the data for a selected id and be able to edit it on one page?