Re: insert data in to columns base on the selection of the list box.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well, simply put (not really following your structure):

--- code ---
<?php
   if (isset($_POST['R'])):
      $address = addslashes($_POST['address']);
      $phone = addslashes($_POST['phone']);
      $lastname = addslashes($_POST['R']);
$sql = "update $tablename set address = '$address', phone = '$phone' where LastName = '$lastname';";
      if (mysql_query($sql)):
         echo "Updated " . $_POST['R'] . "<br />\n";
      else:
         echo "Error: " . mysql_error();
      endif;
   endif;
?>
<html>
<head>
</head>
<body>
<form action="thisfile.php" method="post">
<select name="R">
<option value='Johansson'>Johansson</option>
<option value='Andersson'>Andersson</option>
</select><br />
Address: <input type="text" name="address"><br />
Phone: <input type="text" name="phone"><br />
<input type="submit" value="Update values">
</form>
</body>
</html>
--- code ---

Choosing a lastname in the select, entering some address and phone info and hitting submit will update Address and Phone on the row where Lastname matches what ever you've chosen in the select. The addslashes stuff is for security only, so no hacker does SQL insertions on you.


sam rumaizan skrev:
"I don't get the purpose of your code. It's not looking right anywhere!"
*(The purpose of my code is to update the fields PhoneNumber, Address, Job, FavoriteFood that belong to the selected last name from the menu).* "I'm guessing the select-box isn't working too well, right?" *(Sorry; but the select-box works fine) (Your code will print each field twice (repetition. Try my code then you will see the difference))* "Let's say there's 4 $lines: foreach ($line as $value)
       {
         echo"<OPTION value='$value'";
      }
      echo ">$value</OPTION>";
will produce: <OPTION value='value1'<OPTION value='value2'<OPTION
value='value3'<OPTION value='value4'>value4</OPTION>
... You need to use this code for the selectbox instead: while ($line = mysql_fetch_array($result))
   {
      foreach ($line as $value)
      {
         echo"<OPTION value='$value'>$value</OPTION>";
      }
   }
But, I must say, printing all columns as separate values in the select
box doesn't make any sense.
In a situation with this data in the database:
LastName PhoneNumber Address Job FavoriteFood
_________________________________________________________________________
Johnsson      12345            Superstreet  Programmer       Pizza
Gustavsson    54321            Miniway 42   Cleaner          Chicken
Andersson     23456            Richstreet 2 Boss             Noodles
Dood          45678            Projects     Security         Fajitas
you will get: <SELECT name="R">
<OPTION value='Johnsson'>Johnsson</OPTION>
<OPTION value='12345'>12345</OPTION>
<OPTION value='Superstreet'>Superstreet</OPTION>
<OPTION value='Programmer'>Programmer</OPTION>
<OPTION value='Pizza'>Pizza</OPTION>
<OPTION value='Gustavsson'>Gustavsson</OPTION>
<OPTION value='54321'>54321</OPTION>
<OPTION value='Miniway 42'>Miniway 42</OPTION>
<OPTION value='Cleaner'>Cleaner</OPTION>
<OPTION value='Chicken'>Chicken</OPTION>
<OPTION value='Andersson'>Andersson</OPTION>
<OPTION value='23456'>23456</OPTION>
<OPTION value='Richstreet 2'>Richstreet 2</OPTION>
<OPTION value='Boss'>Boss</OPTION>
<OPTION value='Noodles'>Noodles</OPTION>
<OPTION value='Andersson'>Andersson</OPTION>
<OPTION value='45678'>45678</OPTION>
<OPTION value='Projects'>Projects</OPTION>
<OPTION value='Security'>Security</OPTION>
<OPTION value='Fajitas'>Fajitas</OPTION>
</SELECT>
Is that really what you're after? *NO* *I need to pull the last name* *only from my database. *
*<SELECT name="R">*
*<OPTION value='Johnsson'>Johnsson</OPTION> *
*<OPTION value='Gustavsson'>Gustavsson</OPTION>*
*<OPTION value='Andersson'>Andersson</OPTION>*
*<OPTION value=' Dood'>Dood</OPTION>*
*</SELECT>*
*Then:* *Let's say I want to change the information in my database for Johnsson(How can I do this)*
* *
*Sam*

------------------------------------------------------------------------
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites <http://farechase.yahoo.com/promo-generic-14795097;_ylc=X3oDMTFtNW45amVpBF9TAzk3NDA3NTg5BF9zAzI3MTk0ODEEcG9zAzEEc2VjA21haWx0YWdsaW5lBHNsawNxMS0wNw--> to find flight and hotel bargains.


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux