How to Update this? PHP.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamesaero
Forum Newbie
Posts: 2
Joined: Mon Mar 12, 2012 12:18 am

How to Update this? PHP.

Post by jamesaero »

Hi there! I'm newbie in part of PHP. Here is my code.

Code: Select all

<?php

include("config.php");

$sql = "SELECT * FROM customers ORDER BY CompanyName ASC";
$result = mysql_query($sql);

?>
<form name="form" method="post" action="join.php">
			<select name="myOptions" onchange="document.form.price.value=this.value">
			<option value>----Choose Company Name----</option>
			<?php
			while ($rows=mysql_fetch_array($result))
			{
			?>
			<option value="<?php echo $rows['Price']?>"><?php echo $rows['CompanyName'] ?></option>
			<?php
			}
			?>
			</select>
<input type="text" name="price"><br>
<input type="submit" name="submit" value="Submit"/>
</form>

Here is the code for my update php

Code: Select all

<?php
include ("config.php");

$price=$_POST['price'];

$sql="UPDATE customers WHERE Price = '$price'";
$query=mysql_query($sql);

?>

When you pick one in the list of company name the price of their service will be fetch in the textbox and I want that price to edit but unfortunately I can't update it. Any help will much appreciated within bottom of my heart. Cheers!

Regards,
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to Update this? PHP.

Post by social_experiment »

If i understand correctly you want to display the value of price in a text box and update it into the database;

Code: Select all

 <input type="text" name="price" value="<?php echo $rows['Price']; ?>" >
The value of the input box should display the price value;

Code: Select all

<?php
$sql="UPDATE customers WHERE Price = '$price'";
?>
To update the price you need the old price kept somewhere or you won't have a reference to which field needs to be updated; you can use the value inside $_POST['myOptions']

Code: Select all

<?php
include ("config.php");

$price=$_POST['price'];
$oldPrice = $_POST['myOptions'];

$sql = "UPDATE customers SET Price = '" . $price . "' WHERE Price = '" . $oldPrice . "' ";
$query=mysql_query($sql);

?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jamesaero
Forum Newbie
Posts: 2
Joined: Mon Mar 12, 2012 12:18 am

Re: How to Update this? PHP.

Post by jamesaero »

@social_experiment

You sir, deserves a platinum medal! You've just made my day! :megusta:

Even though you misinterpret my intend but you're idea HELPS me and gave me also other ideas. Cheers man!

A very very big thanks from Philippines :)

This website rocks!
Post Reply