anon Posted December 11, 2007 Share Posted December 11, 2007 HI, I want to know how to make a form that sends its variables to a PHP script, which then stores in database? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 11, 2007 Share Posted December 11, 2007 http://www.phpfreaks.com/tutorials/142/0.php Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 Simple example: <? if(isset($_POST['name'])){ $name = $_POST['name']; $query = "INSERT INTO `db_table` (`id`,`name`) VALUES ('','$name')"; $run = mysql_query($query); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Sample Form</title> </head> <body> <form name="sampleForm" id="sampleForm" action="index.php" method="post" enctype="multipart/form-data"> Name: <input type="text" size="20" id="name" name="name" /> <br /> <input type="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 So before I do all of this, i must create a table. Then, when I use the script, it opens the connection and adds the data? Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 First create a database table, then establish a database connection. After those two steps, the code above will work. Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 Hold the lines. Let me explain the problem fully I have a form with the following stuff <form id="1" name="addurl" method="post" action="addurl.php"> Name<br /> <input type="text" name="name" id="textfield2" /> <p> URL <br /> <input type="text" name="URL" id="textfield" /> <p>Description<br /> <textarea name="description" id="textarea" cols="45" rows="5"></textarea> <p> <input type="submit" name="add" id="button" value="Submit" /> <br /> </form> Thank you for adding your URL to our directory! <p> </p> </body> </html> Obviously that's the form bit. I need to create a DB that will have these values for one entry. Could i do it like that or what? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 11, 2007 Share Posted December 11, 2007 Which is why I pointed you to the tutorial. Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 I agree, take a look at the tutorial. I was just trying to give you a quick answer. Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 Cool. Shot okes! Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 One more question; if i want to add an entry which has the name of a website; the url and a description; would i have to make three different fields or what? Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 That's probably the best way to do it. Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 I want to make this a search engine, so when someone performs a search, it will show a result. How will i connect the three fields together? EDIT: will show results Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 Do you want a single input searched against the url, title, or description fields in a database table? Or are you looking for results based off a given url, title, and description? Basically, depending on what exactly you're trying to accomplish, you can either setup the query to parse those three fields for the given input, or concatenate the three fields into one and then perform a search off the concatenated string. Can you give me more details on what exactly it is you are trying to do? Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 ??? okay. check it. I want to have a form where someone adds their website. This will consist of the name; URL and description. This is then added to the DB. Then, when someone types a keyword into a "search box"; it checks the keyword against the entries (name; url and description) and displays the results like google. The reason i want the three things(name; URL and description) to be linked is so that if one of the values matches the query, it displays all of them (like a google result). Do you get it? :-\ Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 Got it, When you create your database table make sure you create an id as a primary key that auto increments. When a form is submitted, the data creates a new row in the database table, where the id is unique to that row. When performing the search you'd use something like: $query = "SELECT `id`,`name`,`url`,`description` FROM `db_table` WHERE `name` LIKE '%$search_str%' OR `url` LIKE '%$search_str%' OR `description` LIKE '%$search_str%'"; $run = mysql_query($query); while($arr = mysql_fetch_assoc($run)){ extract($arr); echo "<h2><a href=\"$url\">$name</a></h2>". "<p>$description</p>"; } This is obviously a very basic example, but I believe it accomplishes what you're requesting. Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 Cool And do i make three separate fields? EDIT: MY bad. I'm kinda confused. In phpmyadmin, do i make three fields called name, URL and description? Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 In phpmyadmin, your table should look as follows: table_name id (primary key, auto-increment, int) name (varchar) url (varchar) description (varchar with long length, or text) Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 Thanks, so i got the table. Adding values from the form would work like this right? <?php // database information $host = 'localhost'; $user = 'username'; $password = 'password'; $dbName = 'databasename'; // connect and select the database $conn = mysql_connect($host, $user, $password) or die(mysql_error()); $db = mysql_select_db($dbName, $conn) or die(mysql_error()); // insert new entry in the database if entry submitted if (isset($_POST['newEntry']) && trim($_POST['newEntry']) != '') { // for easier variable handling... $newEntry = $_POST['newEntry']; // insert new entry into database $sql = "insert into testTable (someField) values ('$newEntry')"; $result = mysql_query($sql, $conn) or die(mysql_error()); } // end if new entry posted // select all the entries from the table $sql = "select someField from testTable"; $result = mysql_query($sql, $conn) or die(mysql_error()); // echo out the results to the screen while ($list = mysql_fetch_array($result)) { echo "{$list['someField']} <br>"; } // end while // echo out the form to add a new entry echo <<<FORMENT <br> Make a new entry: <br> <form action = "{$_SERVER['PHP_SELF']}" method = "post"> <input type = "text" name = "newEntry" maxlength="20" size = "10"> <input type = "submit" value = "Add Entry"> </form> FORMENT; ?> Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 Just glancing at it, it looks alright. Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 Where would i substitute my values? name ='name' url='url' description='description' Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 Change your select statement to the one I gave you earlier. Typically for search engines you don't want to use "WHERE `db_value` = '$submit_value'" instead use "WHERE `db_value` LIKE '%$submit_value%'" Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 In this previous code you gave (i think) <? if(isset($_POST['name'])){ $name = $_POST['name']; $query = "INSERT INTO `db_table` (`id`,`name`) VALUES ('','$name')"; $run = mysql_query($query); } ?> Where would i put 'url' and 'description' to the script Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 This is an insert statement ,not a select statement. The statement should look like this: <? if(isset($_POST['name'])){ $name = $_POST['name']; $query = "INSERT INTO `db_table` (`id`,`name`,`url`,`description`) VALUES ('','$name','$url','$description')"; $run = mysql_query($query); } ?> Quote Link to comment Share on other sites More sharing options...
anon Posted December 11, 2007 Author Share Posted December 11, 2007 But i want each value from the form (name; url and description) to go into the DB <? if(isset($_POST['name'])){ $name = $_POST['name']; $query = "INSERT INTO `db_table` (`id`,`name`) VALUES ('','$name')"; $run = mysql_query($query); } ?> there's only $name, should there also be $url and $description? Quote Link to comment Share on other sites More sharing options...
p2grace Posted December 11, 2007 Share Posted December 11, 2007 Yes, make a var for both. $url = $_POST['url']; $description = $_POST['description']; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.