• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

change php code to allow user input variaable

The code below  how can i change the code so that user can change

$result = mysql_query(" SELECT * FROM `prdmas`
WHERE `Shelf1` =71 AND `Stare1` =1 ORDER BY course");


Shelf1` =71 AND `Stare1` =1       some time user want to in put  Shelf1 is  70 , 69 70  or Stare1` =  4 ,3

etc







<?php
// connect to the database
mysql_connect(hostname,username,password);

// select the database
mysql_select_db(database) or die("Unable to select database");

// run the query and put the results in an array variable called $result
$result = mysql_query(" SELECT * FROM `prdmas`
WHERE `Shelf1` =71 AND `Stare1` =1 ORDER BY course");

// start a counter in order to number the input fields for each record
$i = 0;


// open a form
print "<form name='namestoupdate' method='post' action='update.php'>\n";

// start a loop to print all of the courses with their book information
// the mysql_fetch_array function puts each record into an array. each time it is called, it moves the array counter up until there are no more records left
while ($books = mysql_fetch_array($result)) {

// assuming you have three important columns (the index (id), the course name (course), and the book info (bookinfo))
// start displaying the info; the most important part is to make the name an array (notice bookinfo[$i])
print "<input type='hidden' name='id[$i]' value='{$books['id']}' />";
print "<p>{$books['course']}: <input type='text' size='40' name='bookinfo[$i]' value='{$books['bookinfo']}' /></p>\n";


// add 1 to the count, close the loop, close the form, and the mysql connection
++$i;
}
print "<input type='submit' value='submit' />";
print "</form>";
mysql_close();
?>
0
teera
Asked:
teera
2 Solutions
 
nizsmoDeveloperCommented:
$result = mysql_query(" SELECT * FROM `prdmas`
WHERE `Shelf1` =$shelf AND `Stare1` =$stare ORDER BY course");


You can modify your mysql query to the above, and assign variables $shelf and $stare by prompting the user in your form to enter a value in lets say 2 different textboxes (1 for $shelf and 1 for $stare).
When the form is submitted, you have to assign the values that the user has entered into the textbox:
(assuming the textboxes had names shelftextbox and staretextbox)


$shelf = $_REQUEST["shelftextbox"];
$stare = $_REQUEST["staretextbox"];

Note that the above 2 lines have to go before the mysql_query statement...

Let me know if you need clarification!
0
 
nplibCommented:
Have a form page send the data to your php page.

form.html

<html>
<body>
<form action="yourdb.php" method="post" name="myform">
<input type="text" name="stare1" />
<input type="text" name="shelf1" />
<input type="submit" value="go" />
</form>
</body>
</html>

yourdb.php

<?php

$shelf1 = $_POST['shelf1'];
$stare1 = $_POST['stare1'];

// connect to the database
mysql_connect(hostname,username,password);

// select the database
mysql_select_db(database) or die("Unable to select database");

// run the query and put the results in an array variable called $result
$result = mysql_query(" SELECT * FROM `prdmas`
WHERE `Shelf1` = $shelf1 AND `Stare1` = $stare1 ORDER BY course");

// start a counter in order to number the input fields for each record
$i = 0;


// open a form
print "<form name='namestoupdate' method='post' action='update.php'>\n";

// start a loop to print all of the courses with their book information
// the mysql_fetch_array function puts each record into an array. each time it is called, it moves the array counter up until there are no more records left
while ($books = mysql_fetch_array($result)) {

// assuming you have three important columns (the index (id), the course name (course), and the book info (bookinfo))
// start displaying the info; the most important part is to make the name an array (notice bookinfo[$i])
print "<input type='hidden' name='id[$i]' value='{$books['id']}' />";
print "<p>{$books['course']}: <input type='text' size='40' name='bookinfo[$i]' value='{$books['bookinfo']}' /></p>\n";


// add 1 to the count, close the loop, close the form, and the mysql connection
++$i;
}
print "<input type='submit' value='submit' />";
print "</form>";
mysql_close();
?>
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now