Page 1 of 1

How to Update this? PHP.

Posted: Mon Mar 12, 2012 12:40 am
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,

Re: How to Update this? PHP.

Posted: Mon Mar 12, 2012 12:51 am
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);

?>

Re: How to Update this? PHP.

Posted: Mon Mar 12, 2012 1:19 am
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!