smiles Posted March 5, 2007 Share Posted March 5, 2007 In SQL we have this syntax, for i.e ... SELECT Information FROM Person WHERE Name = 'Peter' Is there anyway to change 'Peter' to an variable which I get from a formlike this $result = %_POST['Some_Name_Here'];$result1 = mysql_query("SELECT Information FROM Person WHERE Name = $result ") ;//// these above code not work thanks ! Link to comment Share on other sites More sharing options...
pulpfiction Posted March 5, 2007 Share Posted March 5, 2007 $result = $_POST['Some_Name_Here'];$sqlstr = "SELECT Information FROM Person WHERE Name=' " . $result . " ' ";$result1 = mysql_query($sqlstr); Link to comment Share on other sites More sharing options...
smiles Posted March 5, 2007 Author Share Posted March 5, 2007 uhm ... could you tell me how to display the content in $result1 while ($row = mysql_fetch_array($result1)) { echo $row['Information']; }// these above code not work ??? Link to comment Share on other sites More sharing options...
aspnetguy Posted March 5, 2007 Share Posted March 5, 2007 while($row = mysql_fetch_array($result1){ echo $row["Some_Name_Here"];} Link to comment Share on other sites More sharing options...
smiles Posted March 5, 2007 Author Share Posted March 5, 2007 still not work Link to comment Share on other sites More sharing options...
pulpfiction Posted March 5, 2007 Share Posted March 5, 2007 If its giving any error, post it ORCheck if query is returning some value.... If you are using the below query, it must not contain unnecessary blank spaces, I put that in the previous post to show single and double quotes.....$sqlstr = "SELECT Information FROM Person WHERE Name='" . $result . "'"; Link to comment Share on other sites More sharing options...
smiles Posted March 5, 2007 Author Share Posted March 5, 2007 here is the code, I get no error and no display <?php include("1stfile.php"); $result1 = $_POST["theName"];$sqlstr = "SELECT Information FROM Person WHERE Name='" . $result1 . "'";$result2 = mysql_query($sqlstr) or die(mysql_error());while ($row = mysql_fetch_array($result2)) { echo $row["Information"]; }?> Link to comment Share on other sites More sharing options...
pulpfiction Posted March 5, 2007 Share Posted March 5, 2007 Code looks correct..... just echo the sql query and see if its correct.$sqlstr = "SELECT Information FROM Person WHERE Name='" .$result1. "'";echo $sqlstr; Link to comment Share on other sites More sharing options...
smiles Posted March 5, 2007 Author Share Posted March 5, 2007 uhm ... if the content in form is "hello"your code shows "SELECT Information FROM Person WHERE Name=' Hello 'it make me feel a bit better ! Link to comment Share on other sites More sharing options...
pulpfiction Posted March 5, 2007 Share Posted March 5, 2007 Guess its the blank space in ' hello ', so it must be 'hello'Try this,$sqlstr = "SELECT Information FROM Person WHERE Name='" . $result . "'"; Link to comment Share on other sites More sharing options...
smiles Posted March 6, 2007 Author Share Posted March 6, 2007 thanks pulpfiction, but I sure that it hasn't been solved yet What I am trying to do is having a drop down form, its items are get from database, when you choose one item and click submit button, the information of that item will appear below it .Anyone has another way better than what I did ? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 6, 2007 Share Posted March 6, 2007 If this is really exactly what your query says: "SELECT Information FROM Person WHERE Name=' Hello ' Then obviously there are spaces around "Hello". Those spaces are probably the problem. You need to trim the spaces off the value, either when you write it out in the <option> tag or when you get it from post here: $result1 = trim($_POST["theName"]); You'll also want to escape the value in the query to avoid a SQL attack. Link to comment Share on other sites More sharing options...
smiles Posted March 9, 2007 Author Share Posted March 9, 2007 my stupid !!! It works like you showed me Thanks you, all of you Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now