Jump to content

MySQL Update Query Not working


nivekz

Recommended Posts

Hi guys need some help.I created a simple Update Customer Details page Where you Enter the Customer ID ,The Customer Details Get Displayed inside a Form and you make changes within the Form.But the update query I wrote is not working as it should be.It Executes the Query ,when I hardcode the Customer ID inside the Update query but fails when I need to Enter the Customer ID directly from the form using post.In This Code I tried to Update only the Customer's Firstname.Thanks

Here Is The Code

<?php
$db=mysql_connect("localhost","root","") or die('Unable to Connect To Database.Please check the Database Parameters');
mysql_select_db('ecommerce') or die(mysql_error());

if(isset($_POST['enterid']))
{
$_POST['inputid'];
}

$query="SELECT * FROM customer WHERE Cid='$_POST[inputid]'";
$result=mysql_query($query) or die(mysql_error());
$row=mysql_fetch_array($result);
$new_cid=$row['Cid'];
$new_firstname=$row['Cfname'];
$new_lastname=$row['Clname'];
$new_email=$row['Email_id'];
$new_address=$row['Address'];
$new_pincode=$row['Pincode'];
$new_payment=$row['Mode_of_payment'];
$new_city=$row['City'];
$new_state=$row['State'];
$new_phone=$row['Phone'];

$html1=<<<HTML1
<form action="updatecustomer.php" method="post">
<p class="inputidentifier">Please Enter The Customer's ID</p><input type="text" style="width:375px;height:40px;font-size:30px;" name="inputid" size="20"><br><br>
<input type="submit" name="enterid" style="height:40px">
HTML1;

print($new_cid);

if(isset($_POST['update']))
{
  $query1="UPDATE customer SET Cfname='$_POST[firstname]' WHERE Cid='$_POST[inputid]'";
  mysql_query($query1) or die(mysql_error());
  if(mysql_affected_rows()==1)
  print("Query sucessful");
  else
  print("Something went wrong");
}  
?>
<html>
<head>
<style type="text/css">
.inputtext {width:300px; height:40px;font-size:30px;}
.inputidentifier{font-size:25px;font-family:"Arial"}
.h1type{font-family:"Arial"}
</style>
<title>Test</title>
</head>
<body>
<?php print($html1); ?>
<h1 align="center" class="h1type">Update Customer Details</h1>
<form action="updatecustomer.php" method="POST">
<table align="center" cellspacing="10" cellpadding="10" border="0" width="60%">
<tr>
<td align="right" class="inputidentifier">First Name</td>
<td align="left"><input type="text" class="inputtext" name="firstname" placeholder="eg:Kevin" value="<?php print($new_firstname) ?>"></td>
</tr>
<tr>
<td align="right" class="inputidentifier">Last Name</td>
<td><input type="text" class="inputtext" name="lastname" placeholder="eg:Aloysius" value="<?php print($new_lastname) ?>"></td>
</tr>
<tr>
<td align="right" class="inputidentifier">E-mail</td>
<td align="left"><input type="email" style="width:500" class="inputtext" name="email" placeholder="yourname@email.com" value="<?php print($new_email) ?>">
</tr>
<tr>
<td align="right" class="inputidentifier">Phone Number</td>
<td align="left"><input type="text" class="inputtext" name="phone" placeholder="How Do We Call You?" value="<?php print($new_phone) ?>"></td>
</tr>
<tr>
<td align="right" class="inputidentifier">Address</td>
<td><textarea style="width:500;height:150" wrap="virtual" class="inputtext" name="address" placeholder="Where is your Crib?"><?php print($new_address) ?></textarea></td>
</tr>
<tr>
<td align="right" class="inputidentifier">State</td>
<td align="left"><input type="text" style="width:500" class="inputtext" name="state" placeholder="State" value="<?php print($new_state) ?>">
</tr>
<tr>
<td align="right" class="inputidentifier">City</td>
<td align="left"><input type="text" class="inputtext" name="city" placeholder="City" value="<?php print($new_city) ?>">
</tr>
<tr>
<td align="right" class="inputidentifier">Pin Code</td>
<td><input type="text" class="inputtext" name="pincode" placeholder="Mulund 400080" maxlength="6" value="<?php print($new_pincode) ?>"></td>
</tr>
<tr>
<td align="right" class="inputidentifier">How do Pay for your Bling?</td>
<td align="left">
<input type="text" class="inputtext" name="payment" value="<?php print($new_payment)?>">
</tr>
<tr>
<td></td>
<td><input type="submit" name="update" value="Update!" style="width:100px;height:60px;"></td>
</tr>
</table>
</form>
</body>
</html>

Link to comment
Share on other sites

but fails when I need to Enter the Customer ID directly from the form using post

That is a bad idea... If you just get one number wrong, you'll mess up one of your customer details... Why exactly would you want to do this?

 

it's better to select the customer from a list, and edit without ever messing around with the id's.

Link to comment
Share on other sites

Creating a ecommerce website for my college project.And I have to Create a admin panel where i am able to modify my customer details so this idea struck me but ecexuting this has become a problem.Any other ways to edit my customer details with respect to the primary key which is the customer's id

Link to comment
Share on other sites

There's nothing wrong with editing information based on a primary key, it's actually a good way to do it, since the keys are unique. You just should not be able to input the primary key manually in your edit form.

 

The client ID should be passed from page to page through POST or even better, in a $_SESSION variable. If you're planning on posting it though the URL instead (GET method) then make sure you take all the necessary security steps and checks.

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.