Jump to content

Building Add, Delete, Edit system, stuck on Edit (update)


aeafisme23

Recommended Posts

As my subject line says, I am not too sure on how to approach a edit page. The add form works , the delete works , but the edit i have not really grasped how i actually lay it out and to show the appropriate row to Update. I will include the 2 files so you can see the variables and what not being passed to help:

 

search.php (works fine displays edit and delete)

<?php
//Get variables from config.php to connect to mysql server
require 'config.php';

// connect to the mysql database server.
$connect = mysql_connect($dbhost, $dbusername, $dbuserpass) or die("Unable to connect to SQL server..<br />".mysql_error());
$selectDB = mysql_select_db($dbname) or die("Unable to select database.<br />".mysql_error());

//trim whitespace from variable
$searchcode = preg_replace('/\s+/', ' ', trim($_GET['search']));

//seperate multiple keywords into array space delimited
$keywords = array_diff(explode(" ", $searchcode), array(""));

if( !empty($searchcode) ) {
foreach($keywords as $k => $v) 
$keywords[$k] = (int)$v;	
$codes = implode(",", $keywords);

$searchcode_query = "SELECT * FROM dealer WHERE areacode = '".$codes."'";

//Store the results in a variable or die if query fails
$result = mysql_query($searchcode_query) or die("Error executing MySQL query<br />".mysql_error());

$count = mysql_num_rows($result);
}

/** html code **/
echo "You searched for ".$codes."<br />";

//If users doesn't enter anything into search box tell them to.
if( empty($searchcode) ){
echo "<html><head>";
echo "<title>search</title>";
echo "</head>";
echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">";
echo "<center>";
echo "<br /><form name=\"searchform\" method=\"GET\" action=\"search.php\">";
echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />";
echo " <input type=\"submit\" value=\"Search\" />";
echo "</form>";
} else {
if( $count == 0 ){
	echo "Your query returned no results from the database";
} else {
	$row_count = 0;
	while( $row = mysql_fetch_array($result) ) {
		//table background color = row_color variable
		echo "<center><table width=\"680\" bgcolor=".($row_count % 2) ? $color1 : $color2.">";
		echo "<tr>";
		echo "<td width=\"100\" valign=\"top\">".$row['areacode']."</td>";
		echo "<td width=\"150\" valign=\"top\">".$row['dealer']."</td>";
		echo "<td width=\"300\" valign=\"top\">".$row['dealerinfo']."</td>"; 
		echo "<td width=\"65\" valign=\"top\"><a href=\"#\">Edit</a></td>
		<td width=\"65\" valign=\"top\"><a href=\"delete_record.php?id=".$row['id']."\">Delete</a></td></tr>";
		echo "</table></center>";
		$row_count++;
	}
}
}

if( isset($result) ) {
mysql_free_result($result);
mysql_close($connect);
}
?>

 

edit_record.php ( no clue, i just kind of copied my delete_records.php and then added a UPDATE where ever i see a DELETE)

 

<?php
//Get variables from config.php to connect to mysql server
require 'config.php';
// connect to the mysql database server.
$connect = mysql_connect($dbhost, $dbusername, $dbuserpass) or die("Unable to connect to SQL server..<br />".mysql_error());
$selectDB = mysql_select_db($dbname) or die("Unable to select database.<br />".mysql_error());

//$codes = ((int)($_GET['id']));
$codes = $_GET['id'];
$searchcode_query = "SELECT * FROM dealer WHERE id = '".$codes."'";
$sql_delete_query = "Update FROM dealer WHERE id = '".$codes."'";

$result = mysql_query($searchcode_query) or die("Error executing MySQL SELECT query<br />".mysql_error());

if( mysql_affected_rows() != 1 ) 
{
  	 print "No such dealer";
} 
else
{
	mysql_free_result($result);
    	$result = mysql_query($sql_delete_query) or die("Error executing MySQL Update query<br />".mysql_error());
   	 	echo "Updated: ".$code;
}
/** ... **/
?>

 

 

Link to comment
Share on other sites

Sorry it was red of me to put that edit_records.php with out a better attempt so here it is:

It says that the query is empty..... http://www.thegreatestsave.org/ut/search.php?search=574  and hit "EDIT" not delete please

<?php   

//Get variables from config.php to connect to mysql server
require 'config.php';
// connect to the mysql database server.
$connect = mysql_connect($dbhost, $dbusername, $dbuserpass) or die("Unable to connect to SQL server..<br />".mysql_error());
$selectDB = mysql_select_db($dbname) or die("Unable to select database.<br />".mysql_error());

//$codes = ((int)($_GET['id']));
$codes = $_GET['id'];
$searchcode_query = "SELECT * FROM `dealer` WHERE `ID` = " . mysql_real_escape_string ( $_GET['ID'] );
  
    mysql_select_db ( $database, $connect );   
    if ( @mysql_query ( $sql ) )   
    {   
        $query = mysql_query ( $sql );   
        $row = mysql_fetch_assoc ( $query );   
  
        if ( array_key_exists ( '_submit_check', $_POST ) )   
        {   
            if ( $_POST [ 'areacode' ] != '' && $_POST [ 'dealer' ] != '' && $_POST [ 'dealerinfo' ] != '' )   
            {   
                mysql_select_db ( $database, $connect );   
                $query =    "UPDATE   
                            `dealer`   
                        SET  
                            `areacode` = " . mysql_real_escape_string ( $_POST [ 'areacode' ] ) . "  
                            `dealer` = " . mysql_real_escape_string ( $_POST [ 'dealer' ] ) . "  
                            `dealerinfo` = " . mysql_real_escape_string ( $_POST [ 'dealerinfo' ] ) . "  
                        WHERE  
                            `ID` = " . mysql_real_escape_string ( $_GET['ID'] );   
       
                if ( @mysql_query ( $query ) )   
                {   
                    $success = 'Article updated successfully!';   
                }   
                else {   
                    die ( mysql_error () );   
                }   
            }   
            else {   
                echo 'Please ensure that you have a all info!';   
            }   
        }   
    }   
    else {   
        die ( mysql_error () );   
    }   
  
  
    if ( isset ( $success ) ) {   
        echo $success;   
    }   
    else {   
    //we need this form only if we don't have the success message   
?>   
    <form id="update" name="update" method="post" action="<?=$_SERVER['PHP_SELF']?>">   
        <input type="hidden" name="_submit_check" value="1"/>    
        Area Code:<br />   
        <input name="areacode" type="text" id="areacode" size="55" value="<?php if ( isset ( $_POST['areacode'] ) ){ echo $_POST['areacode']; }else{ echo $row['areacode'];} ?>" />   
            <br /><br />   
        Dealer:<br />   
        <input name="dealer" type="text" id="dealer" size="55" value="<?php if ( isset ( $_POST['dealer'] ) ){ echo $_POST['dealer']; }else{ echo $row['dealer'];} ?>" />   
            <br /><br />   			
        Dealer Info:<br />   
        <textarea name="dealerinfo" cols="55" rows="5" id="dealerinfo"><?php if ( isset ( $_POST['dealerinfo'] ) ){ echo $_POST['dealerinfo']; }else{ echo $row['dealerinfo'];} ?></textarea>   
            <br /><br />   
        <input type="submit" name="Submit" value="Update Record" />   
    </form>   
<?php } ?>  

Link to comment
Share on other sites

You need some form fields to EDIT the contents of the table,so it needs to be like

First you click EDIT

You pass on to a page (edit.php) where there will be text fields and the data will be fetched from DB and will be populated in that text fields.

User will alter the data and click on Update and then the data will be updated & saved.

 

Now you are fetching the data from a id, nothing is edited or updated.

 

The UPDATE statement updates columns in existing table rows with new values.

If you set a column to the value it currently has, MySQL notices this and does not update it.

Link to comment
Share on other sites

Hey neon, i wish there was a way to modify my original post and not my reply, but on my reply i added a new edit_record.php that had forms built in. if anyone see's this dont look at the first edit_record but my reply, and with that said I am still trying to figure out a way to get it to work. Sorry neon if you were referring to my reply then i apparently am as worthless as a box of screws on a boat in the middle of an ocean :P

Link to comment
Share on other sites

Try this

edit.php

<?php   
//Get variables from config.php to connect to mysql server
require 'config.php';
// you can keep these inside your config.php file, you dont need to write it in every page just select the table in other pages
$connect = mysql_connect($dbhost, $dbusername, $dbuserpass) or die("Unable to connect to SQL server..<br />".mysql_error());
$selectDB = mysql_select_db($dbname) or die("Unable to select database.<br />".mysql_error());
// ....till here can be kept inside config.php file
$codes = $_GET['id'];
if(isset($_REQUEST["id"]))
$nId=$_REQUEST["id"]; // will be requested.. from search page 
// you have selected the DATABASE now you can select the table and assign this on variable 
$sTable="my_table"; 
$sAction="Update";
// this is for update part 

if($_REQUEST['sAction']=="Update")
{
$query =    "UPDATE   
                            `dealer`   
                        SET  
                            `areacode` = " . mysql_real_escape_string ( $_POST [ 'areacode' ] ) . "  
                            `dealer` = " . mysql_real_escape_string ( $_POST [ 'dealer' ] ) . "  
                            `dealerinfo` = " . mysql_real_escape_string ( $_POST [ 'dealerinfo' ] ) . "  
                        WHERE  
                            `ID` = " . mysql_real_escape_string ( $_REQUEST["id"] ); 

mysql_query($query)
echo "Article updated successfully!";
}
// this value will be passed from the previous page and the text field will be filled with the corresponding ID 
if(isset($_REQUEST['up']) && isset($nId))
{
$sql="select * from ".$sTable." where id=".$_REQUEST['id'].""; // here id is the id column in the table
$result=mysql_query($sql);
if(mysql_num_rows($result)>0)
{
	$row=mysql_fetch_array($result);
	$areacode=$row['areacode'];
	$dealer=$row['dealer'];
	$dealerinfo=$row['dealerinfo'];
	$sAction="Update";

}
}
// end 
  
?>   
<form id="update" name="update" method="post" action="<?=$_SERVER['PHP_SELF']?>"> 
<input type='hidden' name='id' value="<?=$nId?>">
<input type='hidden' name='sAction' value="<?=$sAction?>" >  

Area Code:<br />   
<input name="areacode" type="text" id="areacode" size="55" value="<?=$areacode?>" />   
<br /><br />   
Dealer:<br />   
<input name="dealer" type="text" id="dealer" size="55" value="<?=$dealer?>" />   
<br /><br />   			
Dealer Info:<br />   
<textarea name="dealerinfo" cols="55" rows="5" id="dealerinfo"><?=$dealerinfo?></textarea>   
<br /><br />   
<input type="submit" name="Submit" value="Update Record" />   
</form> 

 

and in your search.php replace this edit link

echo "<td width=\"65\" valign=\"top\"><a href=\"edit.php?up=1&id=".$row['id']."\">Edit</a></td>

 

And also change your table name and column id in the code...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.