Jump to content

[SOLVED] Updating Form to Database


aeafisme23

Recommended Posts

Ok, i got my database to fill in my form for updating using an id structure and it lets me edit the text in each field, but when i hit update it says it was successful but when i go check my records via php form and via the database it really did not update at all and it posts back to its original data. I am so close to getting this to work yet im stumped!

 

*this is not really a repost of my original, it's a different problem i feel. It has to be something to do with the Update or Posting to itself and not keeping the data. Thanks so much everyone! :D

 

<?php   
//Get variables from config.php to connect to mysql server
require 'config.php';
$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 = $_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="dealer"; 
$sAction="Update";
// this is for update part 

if($_REQUEST['sAction']=="Update")
{
$query =    "UPDATE   
                            `dealer`   
                        SET  
                            `dealer` = " . mysql_real_escape_string ( $_POST [ 'dealer' ] ) . "  
                            `address` = " . mysql_real_escape_string ( $_POST [ 'address' ] ) . "  
                            `address2` = " . mysql_real_escape_string ( $_POST [ 'address2' ] ) . "  
						`city` = " . mysql_real_escape_string ( $_POST [ 'city' ] ) . "  
                            `state` = " . mysql_real_escape_string ( $_POST [ 'state' ] ) . "  
                            `zip` = " . mysql_real_escape_string ( $_POST [ 'zip' ] ) . " 
                            `areacode` = " . mysql_real_escape_string ( $_POST [ 'areacode' ] ) . "  
                            `number` = " . mysql_real_escape_string ( $_POST [ 'number' ] ) . "  
                            `tollnumber` = " . mysql_real_escape_string ( $_POST [ 'tollnumber' ] ) . "  
                            `ulweb` = " . mysql_real_escape_string ( $_POST [ 'ulweb' ] ) . "  
                            `email` = " . mysql_real_escape_string ( $_POST [ 'email' ] ) . "  
                            `dealerinfo` = " . mysql_real_escape_string ( $_POST [ 'dealerinfo' ] ) . "  

                        WHERE  
                            `ID` = " . mysql_real_escape_string ( $_REQUEST["id"] ); 

mysql_query($query);	

echo "Entry 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)
{

while($row=mysql_fetch_array($result))
{
	$dealer=$row['dealer'];
	$address=$row['address'];
	$address2=$row['address2'];
	$city=$row['city'];
	$state=$row['state'];
	$zip=$row['zip'];
	$areacode=$row['areacode'];
	$number=$row['number'];
	$tollnumber=$row['tollnumber'];
	$ulweb=$row['ulweb'];
	$email=$row['email'];
	$dealerinfo=$row['dealerinfo'];

	$sAction="Update";
}
}
// end 
  
?>   

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

<table width="700" cellpadding="0" cellpsacing="0" border="1" bordercolor="#000000">
<tr>
	<td>
		<table width="700" cellpadding="0" cellpsacing="0" border="0" bordercolor="#000000">
			<tr>
				<td align="left" width="180">Dealer/ Distributor Name*</td>
				<td width="320" align="left"><input type="text" name="dealer" value="<?php echo $dealer ?>" size="40"></td>
				<td align="left" width="200"></td>
			</tr>
			<tr>
				<td align="left" width="180">Street Address</td>
				<td width="320" align="left"><input type="text" name="address" value="<?php echo $address ?>" size="40"></td>
				<td align="left" width="200"></td>
			</tr>
			<tr>
				<td align="left" width="180">Address 2</td>
				<td width="320" align="left"><input type="text" name="address2" value="<?php echo $address2 ?>" size="40"></td>
				<td align="left" width="200"></td>
			</tr>
			<tr>
				<td align="left" width="180">City*</td>
				<td width="320" align="left"><input type="text" name="city" value="<?php echo $city ?>" size="30"></td>
				<td align="left" width="200"></td>
			</tr>
			<tr>
				<td align="left" width="180">State*</td>
				<td width="320" align="left"><input type="text" name="state" value="<?php echo $state ?>" size="20"></td>
				<td align="left" width="200"></td>
			</tr>
			<tr>
				<td align="left" width="180">Zip*</td>
				<td width="320" align="left"><input type="text" name="zip" value="<?php echo $zip ?>" size="10"></td>
				<td align="left" width="200">ex) 20100</td>
			</tr>
			<tr>
				<td align="left" width="180">Area Code*</td>
				<td width="320" align="left"><input type="text" name="areacode" value="<?php echo $areacode ?>" size="10"></td>
				<td align="left" width="200">ex) 489</td>
			</tr>
			<tr>
				<td align="left" width="180">Local Number*</td>
				<td width="320" align="left"><input type="text" name="number" value="<?php echo $number ?>" size="20"></td>
				<td align="left" width="200">ex) 123-4567</td>
			</tr>
			<tr>
				<td align="left" width="180">Toll Free Number</td>
				<td width="320" align="left"><input type="text" name="tollnumber" value="<?php echo $tollnumber ?>" size="20"></td>
				<td align="left" width="200">ex) 1 800-123-4567</td>
			</tr>
			<tr>
				<td align="left" width="180">Website Address</td>
				<td width="320" align="left"><input type="text" name="ulweb" value="<?php echo $ulweb ?>" size="40"></td>
				<td align="left" width="200">ex) www.google.com</td>
			</tr>
			<tr>
				<td align="left" width="180">Email Address</td>
				<td width="320" align="left"><input type="text" name="email" value="<?php echo $email ?>" size="40"></td>
				<td align="left" width="200">(i.e. “Sales” or persons name)</td>
			</tr>
			<tr>
				<td align="left" width="180" valign="top">Dealer Information</td>
				<td width="320" align="left"><textarea name="dealerinfo" value="<?php echo $dealerinfo ?>" cols="40" rows="6"></textarea></td>
				<td align="left" width="200"></td>
			</tr>
			<tr>
				<td colspan="3">
				<p align="center"><input type="submit" value="Update record"></p>
				</td>
			</tr>
		</table>

		</td></tr></table>
</form><br />* denotes a Required Field</center>
<?php } ?>

Link to comment
Share on other sites

If you are needing a better idea

 

http://www.unitedtruckdesign.com/dealers/main_login.php

 

user:   > user

pass:  > pass

 

*aint i smart lol

 

then paste this URL in: http://www.unitedtruckdesign.com/dealers/view_records_test.php?search=574  ; you must paste this url because the login system is using sessions and it is an unlinked page from the options there are right now!

 

Then click the edit button and you will see that it posts all the data but dealerinfo (wierd trying to figure that out right now) but it will let you think it's updated when you hit update/submit button but you can click on view records and hit in 574 and you will see that it did not update it. I hope this helps. Please do not delete record as i do not feel like re-creating test area codes. Thanks!

Link to comment
Share on other sites

text

 

 

heh, you can tell im not a true programmer....

 

CREATE TABLE `dealer` (

  `id` mediumint(10) NOT NULL auto_increment,

  `dealer` varchar(65) NOT NULL default '',

  `address` varchar(65) NOT NULL default '',

  `address2` varchar(65) NOT NULL default '',

  `city` varchar(65) NOT NULL default '',

  `state` varchar(65) NOT NULL default '',

  `zip` int(10) NOT NULL default '0',

  `areacode` int(10) NOT NULL default '0',

  `number` varchar(20) NOT NULL default '0',

  `tollnumber` varchar(25) NOT NULL default '0',

  `ulweb` varchar(65) NOT NULL default '',

  `email` varchar(65) NOT NULL default '',

 

***was text*** now varchar?

`dealerinfo` varchar(200) NOT NULL default '',

 

 

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

 

Link to comment
Share on other sites

Hmm i think the database structure is fine, i cant seem to figure out why the dealer info isnt getting pulled, i am not sure if it is logic such as (  [ {  ;  or if it is bad SQL Update or if my variables are somewhat wrong. Is there a way to debug this or does anyone have any ideas why this is not updating even though it says it is.... 

 

* above you can get user/ pass and url to get to what im talking about if your new to this thread.

 

Thanks

Link to comment
Share on other sites

Change

mysql_query($query);	

 

to

 

$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

 

See if it reports errors.

 

Also, what editor are you using?  I see some odd characters in your code.  There is something after your query;

 

Also, there is really nothing stopping this line from echoing

 

echo "Entry updated successfully!";

 

Since you have no Error Reporting.

Link to comment
Share on other sites

I am using Dreamweaver 8, just CTRL+A > CTRL+C to copy the text and then copy paste.

 

 

Replaced and this is what i got...

 

Error in query: UPDATE `dealer` SET `dealer` = Fort Wayne Company `address` = 12345 Rd Ridge `address2` = `city` = Fort Wayne `state` = Indiana `zip` = 46060 `areacode` = 574 `number` = 732 `tollnumber` = 1 `ulweb` = www.ftwayne.com `email` = rog@ftwayne.com `dealerinfo` = WHERE `ID` = 5. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Wayne Company `address` = 12345 R

Link to comment
Share on other sites

I put in commas, but is the ` effecting it or ". my...etc", how should the statement look like?

 

$query =    "UPDATE 

                            `dealer` 

                        SET 

                            `dealer` = " . mysql_real_escape_string ( $_POST [ 'dealer' ] ) . "  ,

                            `address` = " . mysql_real_escape_string ( $_POST [ 'address' ] ) . " ,

                            `address2` = " . mysql_real_escape_string ( $_POST [ 'address2' ] ) . "  ,

`city` = " . mysql_real_escape_string ( $_POST [ 'city' ] ) . "  ,

                            `state` = " . mysql_real_escape_string ( $_POST [ 'state' ] ) . "  ,

                            `zip` = " . mysql_real_escape_string ( $_POST [ 'zip' ] ) . " ,

                            `areacode` = " . mysql_real_escape_string ( $_POST [ 'areacode' ] ) . "  ,

                            `number` = " . mysql_real_escape_string ( $_POST [ 'number' ] ) . "  ,

                            `tollnumber` = " . mysql_real_escape_string ( $_POST [ 'tollnumber' ] ) . "  ,

                            `ulweb` = " . mysql_real_escape_string ( $_POST [ 'ulweb' ] ) . "  ,

                            `email` = " . mysql_real_escape_string ( $_POST [ 'email' ] ) . "  ,

                            `dealerinfo` = " . mysql_real_escape_string ( $_POST [ 'dealerinfo' ] ) . " 

 

                        WHERE 

                            `ID` = " . mysql_real_escape_string ( $_REQUEST["id"] );

Link to comment
Share on other sites

You're missing a ending quote too at the end of the statement.

 

This is what I would do so it's easier to read.

 

<?php

$dealer = mysql_real_escape_string($_POST['dealer']);
$address = mysql_real_escape_string($_POST['address']);
$address2 = mysql_real_escape_string($_POST['address2']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$zip = mysql_real_escape_string($_POST['zip']);
$areacode = mysql_real_escape_string($_POST['areacode']);
$number = mysql_real_escape_string($_POST['number']);
$tollnumber = mysql_real_escape_string($_POST['tollnumber']);
$email = mysql_real_escape_string($_POST['email']);
$ulweb = mysql_real_escape_string($_POST['ulweb']);
$dealerinfo = mysql_real_escape_string($_POST['dealerinfo']);
$id = mysql_real_escape_string($_REQUEST['id']);

$query = "UPDATE `dealer` SET  `dealer` = '$dealer', `address` = '$address', `address2` = '$address2', `city` = '$city', `state` = '$state', `zip` = '$zip', `areacode` = '$areacode', `number` = '$number', `tollnumber` = '$tollnumber', `ulweb` = '$ulweb', `email` = '$email', `dealerinfo` = '$dealerinfo' WHERE `ID` = '$id'"; 
?>

Link to comment
Share on other sites

See if this works

 

This line

<td width="320" align="left"><textarea name="dealerinfo" value="<?php echo $dealerinfo ?>" cols="40" rows="6"></textarea></td>

 

to

 

<td width="320" align="left"><textarea name="dealerinfo" cols="40" rows="6"><?php echo $dealerinfo ?></textarea></td>

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.