Hi, below is just code that pulls items out of a database. Right now it is pulling any row from the field "name." From the table called "users." From a database. How can I get a form to make some php code execute based on the name someone puts in... I can make the form field and action, have the following code for you below, just need help with the rest. So their names would be listed in the field "name." For instance there would be the database field named "name" with several names listed in it and data in each one of them. Bobby1 Mandi1 Ty1 Anytime someone puts their name into a form and clicks submit, it will pull database row with their name in it plus one. If Bobby entered his name into the form field, the information in the row named bobby1 would show on the page. If Mandi put in her name in the form, the information named Mandi1 would be pulled from the database and so on. Database code that works for rows only. <?php $link = mysql_connect('host', 'username', 'password'); $db_selected = mysql_select_db('database', $link); $query = "SELECT name FROM users"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { print "<p>$row[name]</p>"; } ?> Hope somone can tell me how to do this, thank you very much.
You will need to create a SELECT statement based on the form variable to get the info from the database based on the form POST. Like: $link = mysql_connect('host', 'username', 'password'); $db_selected = mysql_select_db('database', $link); $sql = "SELECT name FROM users WHERE nickname = '".$_POST['name_from_form']."'"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { echo "<p>".$row['name']."</p>"; } PHP:
<? $link = mysql_connect('host', 'username', 'password'); $db_selected = mysql_select_db('database', $link); if(!$_POST) { ?> <form action="" method="post"> Name : <input type="text" name="name" value="<?=$_POST['name'] ?>" /><input type="submit" value="look" /> </form> <? } else { $res = mysql_query("SELECT * FROM `users` WHERE name='".mysql_real_escape_string(stripslashes(trim($_POST['name'])))."'"); if(!$res) { die("User doesn't exist!"); } while($arr = mysql_fetch_assoc($res)) { echo $arr['name']; echo $arr['age']; # Where age is a table name in the database } } ?> PHP:
Thank you very much for helping me!! I am having trouble with something else but will try and test these soon.
Hi, thanks for the helpful code, if you guys could help me with this I'll send you some things that have helped me over a few years. Good news is I only got a parse error on the very last line for joes but I'm sure I'm using it incorrectly. The form text field isn't appearing as is. Strange thing is its saying the very last ?> is unexepected which I wouldn't have thought because its the closing php. So not quite sure what that means if anything. Here's what I have so far. <?php $link = mysql_connect('host', 'databasename', 'password'); $db_selected = mysql_select_db('databasename', $link);if(!$_POST) {?><form action="" method="post">Name : <input type="text" name="name" value="<?=$_POST['name'] ?>" /><input type="submit" value="look" /></form> <?}else { $res = mysql_query("SELECT * FROM `name` WHERE name='".mysql_real_escape_string(stripslashes(trim($_POST['name'])))."'"); if(!$res) { die("User doesn't exist!"); } while($arr = mysql_fetch_assoc($res)) { echo $arr['name']; echo $arr['name']; # Where age is a table name in the database }}?> I set up a table named "name" in the database with a field "bobby" in it and put a few things in the field "bobby." I think it might work if I can just pull whatever is in "bobby" with the form, I was too confused the first way I mentioned, it might make it easier to write the code for it as well. So do leave the fields calling them all "name"? After it inserts bobby's name will it pull whats in the field "bobby" automatically? Please get back to me, thank you very much.
<?php $link = mysql_connect('host', 'databasename', 'password'); $db_selected = mysql_select_db('databasename', $link);if(!$_POST) { ?> <form action="" method="post"> Name : <input type="text" name="name" value="<?=$_POST['name'] ?>" /> <input type="submit" value="look" /> </form> <? } else { $res = mysql_query("SELECT * FROM `name` WHERE name='".mysql_real_escape_string(stripslashes(trim($_POST['name'])))."'"); if(!$res) { die("User doesn't exist!"); } while($arr = mysql_fetch_assoc($res)) { echo $arr['name']; } } ?> PHP: The code above is definately correct, do not attempt to change where the lines are or move form code, ok so, the SQL tells the MySQL Server to select all the data from the table named "name" and return the row that holds the users information, so if you have a table called name and one of the fields in there is called "address" then $arr['address'] would echo the address ... get it ?
Getting closer, for some reason it only works if I put "name" into the form field and then click "look." But only if "name" is also inserted for the field named "name." That might confuse someone so... For example there is the database name, then the table called "name" then the fields "mandi" and "bobby" in the table. In mandi and bobby text area, I have "testmandi" and "testbobby." If I put mandi or bobby into the form nothing appears. So I made a field called "name" for the table also. After I did that, I put "name" into the form nothing still appears. But if I change "testname" to just "name" in the text area for the field "name" then the word "name" finally appears. I don't know what this means hoping you do, because I really want to use this code. This is what I need to happen. When I insert text into a field for bobby or mandi etc. When bobby is put in the form field I need the text "testmandi" or whatever kind of text I put in his field to appear on the page. Hope you or someone can help and thanks very much for the help.
Ok, what's the name of the database? What's the name of the table with the user data in ? Also, post either a screenshot or a dump of the schema of the db, and we'll sort this out once and for all
Thank you! I think the biggest thing fouling this up is I called my table "name," instead of something else. Sorry about that. My database name is llogin. The table name is "name." The fields in the table are just persons names. This is how it looks on my screen AFTER I click on the table called "name." First it shows the field names and field types. Field Type bobby text mandi text Then when you use browse it shows what I put in the fields for bobby and mandi. bobby mandi testbobby testmandi Thats all there is. When I clicked on "create php code" on this bobby mandi testbobby testmandi It gave me this. $sql = 'SELECT * FROM `name`;
First, login to cpanel, and create a new database, call it whatever you want.... Second login to phpmyadmin, select the database you just created and press the SQL tab : CREATE TABLE `info` ( `id` int(11) NOT NULL auto_increment, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1 ; Do that.... Then, you should be able to use the followin code to select / display data : <?php $link = mysql_connect('host', 'databasename', 'password'); $db_selected = mysql_select_db('databasename', $link); if(!$_POST) { ?> <form action="" method="post"> Name : <input type="text" name="name" value="<?=$_POST['name'] ?>" /> <input type="submit" value="look" /> </form> <? } else { $res = mysql_query("SELECT * FROM `info` WHERE name='".mysql_real_escape_string(stripslashes(trim($_POST['name'])))."'"); if(!$res) { die("User doesn't exist!"); } while($arr = mysql_fetch_assoc($res)) { echo $arr['name']; } } ?> PHP: Try that .....
Wow! Thank you very much and for helping me get through this. Can tell you know a lot about php. OK there is only one thing left to fix. I have needed to call items that are in bobby etc. Right now if I put bobby into the form field the word "bobby" appears. How do I insert data into the field "bobby" and also pull the data from "bobby." So that when "bobby" is entered in the form field his data is pulled not his name? Another way to put it if that is too confusing. Bobby comes along and enters "bobby" into the form field. Prior to that I had already entered data into the "bobby" field in the table. So when he enters his name, all his data appears on the page. I don't know how to enter data into his name field the way it is set up now. I also don't know how his data can be pulled from the database intead of his name. Which is what I needed in the beginning too. Thanks.
you can also try to install phpmyadmin ( Web Script ) it will be much easier to man your ways or you can try mysql cc ( application ) to add delete ur database. If not DIY your own web amin to add and delete. You can try MySQL Help in yahoo Few Examples - Insert into tablename VALUES ('field1','field2','field3','field4'); - Delete from tablename where yourfield = yourselectedvariable ( eg id=1 ) - UPDATE tablename SET Yourfiled=yourvariable WHERE id=1; ( just an example )
Heya Joe, I got it working trying something a bit different. So you don't have to spend any more time on this. I'll send you what I am using. Thanks a lot Jen.
stripslashes() deals with extra \ on text items sometimes added if you use a ' in the field, and trim strips whitespace from the beginning and the end of the text.