Change a value in a MYSQL database

Soldato
Joined
10 May 2004
Posts
3,759
Location
East Yorkshire, UK
Hi

I have this script of what you see when you in a MYSQL database

Code:
  <?
$conn = mysql_connect("localhost","watotron_infraon","12345");
$db = mysql_select_db("watotron_infraone");

$username = $_POST["username"];
$password = $_POST["password"];

$result = MYSQL_QUERY("SELECT * from form WHERE username='$username'and password='$password'")
   or die ("username or password are incorrect");

$worked = mysql_fetch_array($result);

$username = $worked[username];
$password = $worked[password];
$forename = $worked[forename];
$othernames = $worked[othernames];
$plan = $worked[plan];

if($worked)
   echo "Welcome $forename $othernames! Your current plan is $plan"; 
?>

Is it possible to change the value of $plan to something else? So it says

What would you like your new plan to be?

Thanks
 
erm, not really sure what your after but you could either change the entry for plan in the database or just simply $plan = 'What would you like your plan to be'; instead of $plan = $worked[plan]; surely?
 
If you're displaying results surely you don't want one of the columns to contain a question - you want to either list the plan, or put "no plan chosen" or similar, then link to a section where they can choose a plan?

But (I think) to answer your question, assuming the default value of plan can be a blank entry in the database row, how about this:

Code:
$plan = (empty($worked['plan'])) ? "What would you like your new plan to be?" : $worked['plan'];
Methinks that should work :)

Edit: Ah, you want to change the value - sure - just setup another form similar to the signup page, then instead of using an INSERT SQL query, use UPDATE - the syntax is like this:

Code:
UPDATE table SET field1 = '$field1' WHERE id = '$id'

Where id is whichever field uniquely identifies that row :)
 
Last edited:
UPDATE queries update all the rows from a table unless you narrow it down with a 'WHERE' clause, just like you narrow down in the same way with a SELECT query - ie, to get one row to work with.

You only want to update one row (the row for that user), so you need to work out how to narrow it down to just the one row.

What fields do you have in your database? The problem is that if you update a row where the plan matches a certain value you'll update *all* the rows to the new value. And if you use the username instead, all the plans by that user will be updated - unless of course each username can have only one hosting plan, in which case you can use the username to uniquely identify a row.

Here's a more applicable query for you:
Code:
UPDATE form SET plan = '$plan' WHERE username = '$username'
What that's saying is "update all the rows in the table called form where the row has a username which equals $username and set each row's plan field to $plan".

So if each username can only have one entry, then that'll just update that one row :)
 
thats exactly what i want, however when I add it I get

Parse error: syntax error, unexpected T_STRING in /home/watotron/public_html/School/Project/InfraOne/welcome.php on line 114

this is the current code

Code:
  <?
$conn = mysql_connect("localhost","watotron_infraon","12345");
$db = mysql_select_db("watotron_infraone");

$username = $_POST["username"];
$password = $_POST["password"];

$result = MYSQL_QUERY("SELECT * from form WHERE username='$username'and password='$password'")
   or die ("username or password are incorrect");

$worked = mysql_fetch_array($result);

$username = $worked[username];
$password = $worked[password];
$forename = $worked[forename];
$othernames = $worked[othernames];
$plan = $worked[plan];

if($worked)
   echo "Welcome $forename $othernames! Your current plan is $plan"; 
   UPDATE form SET plan = '$plan' WHERE username = '$username'
?>
 
That line you added is an SQL query - use it in the same manner as the INSERT query.

ie:

$result = mysql_query("UPDATE form SET plan = '$plan' WHERE username = '$username'");

But still, you haven't written any code to get the value which should be put into the database, so it's kinda pointless as you'd be updating the database with a value you've just retrieved from the database - this line:

Code:
$plan = $worked[plan];
 
Yup - construct the form the same way as the signup form. I'm assuming if you've setup the signup form then you can setup a form for this, too. IIRC this is a project so I don't want to just give you the answers :)
 
thx for reply
ok ive tried setting it up, but its coming up with this error

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/watotron/public_html/School/Project/InfraOne/welcome.php on line 121

heres the code

Code:
<?PHP
$username = $_POST["username"];
$password = $_POST["password"];

$result = MYSQL_QUERY("SELECT * from form WHERE username='$username'and password='$password'")
   or die ("username or password are incorrect");

$worked = mysql_fetch_array($result);

$forename = $worked[forename];
$othernames = $worked[othernames];
if($worked)
   echo "Welcome $forename $othernames!; 
   ?>
   
  <hl> 
  </span>


  <form action="" method="post">
     <p class="style6">
	 Plan:
      <label>
        <select name="plan" class="box1" id="plan">
          <option>Starter</option>
          <option>Pro</option>
          <option>Premier</option>
          <option>Business</option>
        </select>
        </label>
      <label>
      <input name="submit" type="submit" class="box2" value="Submit!" />
      </label>
    </p>
  </form>
	
      <?php
  } else {
  $plan = $_POST['plan'];
 $result = mysql_query("INSERT INTO form (plan) VALUES ('$plan')");

if(!$result) {
   die(mysql_error());
} else {
   echo "Thanks you, your hosting has been updated.";
}
}
?>
 
Back
Top Bottom