PHP - Need Help Building Form And Insterting Into Mysql ;)
hello,
I am trying to create a form that will log items. Say I have a list of fruits and I want to log the fruit and its color. I don't know how many fruits I have so I need the form to start with one row first then add rows after I hit enter. I then need to insert the fruits into mysql any ideas or links? anyone? Similar TutorialsHi, can some one please explain to me the best way to do this, currently I am using txt files on the server to hold the content of the webpage for example: header, title_1, paragraph_1, title_2, paragraph_2 etc. But with more content this is getting very tedious and I'm sure using mysql would be better here's how it works now ( write to file) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="keywords" content=""> <meta name="description" content=" "> <meta name="robots" content="FOLLOW,INDEX"> <title></title> <link rel='stylesheet' href='../css/style.css' type='text/css'> <link rel="shortcut icon" href=""> <script type="text/javascript" src="../js/ckeditor/ckeditor.js"></script> </head> <body> <?php //This condition will check either the submit button was pressed, for such test you need to check that if there is an element which has the same name as your button. if(isset($_POST['Submit'])) { //this is just the name of the file, it can be used directly but its better to use a variable its just for convinence $file = "../content/menu_header_a.txt"; if(is_writable($file)) { //this is to open the file in write more 'w' says that it will open in writeable more $fh = fopen($file, 'w') or die("can't open file"); //I again used a variable to store the data.... which is to be written to the file //The main point is to receive the data on submit, it is when submit is done, and uppon condition for submit is fulfilled //all the elements of the form are put in the array named $_POST and you can get their values by their names //in the same way I just took the value of 'editor1' it is the name for textarea in which you displayed the text $data = stripslashes(stripslashes($_POST['editor_header_a'])); ///This routine will just write the data to file given the file handle and the data. fwrite($fh, $data); //must close the file after all writing is done. fclose($fh); } else { echo "File is Not Writable<br>"; } } ?> <form method="post"> <textarea name="editor_header_a"> <?php $file = file_get_contents('../content/menu_header_a.txt'); echo $file; ?> </textarea> <script type="text/javascript"> CKEDITOR.replace( 'editor_header_a', { toolbar : [ ['Source','-','Save','NewPage','Preview','-','Templates'], ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'], ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'], ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'], '/', ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'], ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'], ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'], ['Link','Unlink','Anchor'], ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'], '/', ['Styles','Format','Font','FontSize'], ['TextColor','BGColor'], ['Maximize', 'ShowBlocks','-','About'] ] }); </script> </p> <p> <input name="Submit" type="submit" /> </p> </form> </body> </html> display content from file. <?php $file = file_get_contents('content/menu_header_a.txt', 'r'); if(isset($_SESSION['user_id'])){ ?><a href="javascript:open4()"><?php echo $file ?></a><?php } else { echo $file; }?> js Code: [Select] var win=null; function NewWindow(mypage,myname,w,h,scroll,pos){ if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;} if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;} else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20} settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes'; win=window.open(mypage,myname,settings);} function open4(){ var open4 = window.open('http://localhost/projects/public_html/contentscript/content_menu_header_a.php','jav','width=540,height=480,resizable=yes'); } I like the way it works currently, I just have to click on the content I want to make changes to for the wysiwyg editor to open up but these txt files are doing my head in:). btw Im a still learning all this please try to keep it simple for me Thanks for you help. Hi, I have this script which I would like to use to build an html form from a MySQL table. The <input text................ writes successfully to file but none of the other form types are written to file when using the elseif command Also, I would like this script to carry out the file write foreach row in the table It seems complicated and i am not sure if I am going about it in the right way, but here goes Code: [Select] //// Create Forms from MySQL $result = mysql_query("SELECT * FROM forms"); while($row = mysql_fetch_assoc($result)){ $field_label = $row['field_label']; $column_name = $row['column_name']; $field_type = $row['field_type']; if ($row['field_type'] = 'Text') { $stringData = "$field_label<input type=text name=$column_name>\n"; fwrite($fh, $stringData); } elseif ($row['field_type'] = 'Text Area') { $stringData = "$field_label<input type=textarea name=$column_name></textarea>\n"; fwrite($fh, $stringData); } elseif ($row['field_type'] = 'Select Menu') { $stringData = "$field_label<select name=$column_name></select>\n"; fwrite($fh, $stringData); } elseif ($row['field_type'] = 'Checkbox') { $stringData = "$field_label<input type=checkbox name=$column_name></textarea>\n"; fwrite($fh, $stringData); } fclose($fh); As always, any help is much appreciated I have a table in my db with all my menus and submenues of my site, I'd like to show them in some kind of menu containing all of them each entry in db has the columns ID,NAME,TEXT,LEVEL,DEPENDENT level has a value of 1 or 2 depending if it's an item or a subitem dependent has a reference to the id of the section it belongs to Using dreamweaver I created two recordsets based on the level, they are called row_sections (only where level = 1) and row_subsections (only where level = 2) here is the code I tried to use: <?php do { ?> <p><a href="test.php?sec=<?php echo $row_sections['id'];?>"><?php echo $row_sections['name']; ?></a></p> //prints the section text <?php do { ?> <?php if($row_subsections['dependent'] == $row_sections['id']) { echo $row_subsections['name']."<br>"; //tries to print all subitems corresponding to the actual item } ?> <?php } while ($row_subsections = mysql_fetch_assoc($subsections)); ?> <?php } while ($row_sections = mysql_fetch_assoc($sections)); ?> Here's the output: Quote section 1 title subsection 1 subsection 2 section 2 title both section 1 and 2 should have two different subitems.. where is the problem? if I add the line <?php echo $row_sections['id']; ?> right before the second <?php do{ ?> it shows the corresponding ID for BOTH sections so I can't understand the problem... I'm a newbie and want to explore on the use of php.I want to create a transcript system but do not know how to go about it.please where should i start from? This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=351877.0 There are two pieces to this- The HTML Form and the resulting php. I can't seem to make the leap, from the code to having the form produce the php page so others can view it until the form is again submitted overwriting the php, thus generating new content. The environment I am working in is limited to IIs 5.1 and php 5.2.17 without mySQL or other DB I'm new to php, this isn't homework,or commercialization, it's for children. I am thinking perhaps fwrite / fread but can't get my head around it. Code snipets below. Any help, please use portions of this code in hopes I can understand it Thanks Code snipet from Output.php Code: [Select] <?php $t1image = $_POST["t1image"]; $t1title = $_POST["t1title"]; $t1info = $_POST["t1info"]; $t2image = $_POST["t2image"]; $t2title = $_POST["t2title"]; $t2info = $_POST["t2info"]; ?> ... <tbody> <tr><!--Headers--> <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Animal</td> <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Image thumb<br> </td> <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Date<br> </td> <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Information<br> </td> </tr> <tr> <td style="vertical-align: top; text-align: center;">Monkey </td> <td style="vertical-align: top; text-align: center;"><img src="<?php echo $t1image.'.gif'; ?>"><!--single image presented selected from radio buttons--> </td> <td style="vertical-align: top; text-align: center;"><?php echo date("m/d/Yh:i A"); ?><!--time stamp generated when submitted form populates all fields at once--> </td> <td style="vertical-align: top; text-align: center;"><a href="#monkey" rel="facebox"><?php echo $t1title ?></a><!--Link name provided by "Title 1", that links to hidden Div generated page with content from "Info1" field--> <div id="Monkey" style="display:none"> <?php echo $t1info; ?> </div> </td> </tr> <tr> <td style="vertical-align: top; text-align: center;">Cat<br> </td> <td style="vertical-align: top; text-align: center;"><img src="<?php echo $t2image.'.gif'?>"></td> <td style="vertical-align: top; text-align: center;"><?php echo date("m/d/Yh:i A"); ?></td> <td style="vertical-align: top; text-align: center;"><a href="#Cat" rel="facebox"><?php echo $t2title ?></a> <div id="Cat" style="display:none"> <?php echo $t2info; ?> </div> </td> </tr> <tr> This replicates several times down the page around 15-20 times ( t1### - t20###) Code Snipet from HTML Form Code: [Select] <form action="animals.php" method="post"> <div style="text-align: left;"><big style="font-family: Garamond; font-weight: bold; color: rgb(51, 51, 255);"><big><big><span>Monkey</span></big></big></big><br> <table style="text-align: left; width: 110px;" border="0" cellpadding="2" cellspacing="0"> <tbody><tr> <td style="vertical-align: top;">Image thumb<br> <input type="radio" name="t1image" value="No opinion" checked><img src="eh.gif" alt="Eh"> <input type="radio" name="t1image" value="Ok"><img src="ok.gif" alt="ok"> <input type="radio" name="t1image" value="Like"><img src="like.gif" alt="Like"> <input type="radio" name="t1image" value="Dont"><img src="dont.gif" alt="Don't Like"> <input type="radio" name="t1image" value="Hate"><img src="hate.gif" alt="Hate"> <input type="radio" name="t1image" value="Other"><img src="other.gif" alt="Other"> <br> Why Title:<input type="text" name="t1title" size="45" value="..."/></td> <td style="vertical-align: top;"> Explain:<br> <textarea name="t1info" cols=45 rows=3 value="..."></textarea> </td></tr></table> <br> <!--Next--> How do I get the Form data to save to the php page for others to view? Hi- the code below lets me upload a CSV file to my database if I have 1 field in my database and 1 column in my CSV. I need to add to my db "player_id" from the CVS file and "event_name" and "event_type" from the form... any ideas??? here's the code: Code: [Select] <?php $hoststring =""; $database = ""; $username = ""; $password = ""; $makeconnection = mysql_pconnect($hoststring, $username, $password); ?> <?php ob_start(); mysql_select_db($database, $makeconnection); $sql_get_players=" SELECT * FROM tabel ORDER BY player_id ASC"; // $get_players = mysql_query($sql_get_players, $makeconnection) or die(mysql_error()); $row_get_players = mysql_fetch_assoc($get_players); // $message = null; $allowed_extensions = array('csv'); $upload_path = '.'; //same directory if (!empty($_FILES['file'])) { if ($_FILES['file']['error'] == 0) { // check extension $file = explode(".", $_FILES['file']['name']); $extension = array_pop($file); if (in_array($extension, $allowed_extensions)) { if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name'])) { if (($handle = fopen($upload_path.'/'.$_FILES['file']['name'], "r")) !== false) { $keys = array(); $out = array(); $insert = array(); $line = 1; while (($row = fgetcsv($handle, 0, ',', '"')) !== FALSE) { foreach($row as $key => $value) { if ($line === 1) { $keys[$key] = $value; } else { $out[$line][$key] = $value; } } $line++; } fclose($handle); if (!empty($keys) && !empty($out)) { $db = new PDO( 'mysql:host=host;dbname=db', 'user', 'pw'); $db->exec("SET CHARACTER SET utf8"); foreach($out as $key => $value) { $sql = "INSERT INTO `table` (`"; $sql .= implode("`player_id`", $keys); $sql .= "`) VALUES ("; $sql .= implode(", ", array_fill(0, count($keys), "?")); $sql .= ")"; $statement = $db->prepare($sql); $statement->execute($value); } $message = '<span>File has been uploaded successfully</span>'; } } } } else { $message = '<span>Only .csv file format is allowed</span>'; } } else { $message = '<span>There was a problem with your file</span>'; } } ob_flush();?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>CSV File Upload</title> </head> <body> <form class="form" action="" method="post" enctype="multipart/form-data"> <h3>Select Your File</h3> <p><?php echo $message; ?></p> <input type="file" name="file" id="file" size="30" /> <br/> <label>Event Name:</label><input name="event_name" type="text" value="" /> <br/> <label>Event Type:</label><input name="event_type" type="text" value="" /> <br/> <input type="submit" id="btn" class="button" value="Submit" /> </form> <br/> <h3>Results:</h3> <?php do { ?> <p><?php echo $row_get_players['player_id'];?></p> <?php } while ($row_get_players = mysql_fetch_assoc($get_players)); ?> </body> </html> Hi guys, I have recently been asked by a freinds to turn his web-site in to a CMS web site and i was just wondering if implementing it in php is the write way to go about it. I have never realy build anything like a CMS so if you guys have tips for me that would be spot on. Hey guys, I'm not exactly sure what the term used for this is, but I'll try explaining it very thoroughly. My site I'm building is this: http://r-gaming.net/urban-rp/home/ On index.php file, I would like it so you if you're logged in (I have a logged in system already scripted), you're able to post current news. Something like this: http://ls-rp.com/ Would anyone willing to help me by linking a tutorial to code this method 'latest news' tool? Would be greatly appreciated. Crayon Hi people. I'm building a quiz with one correct answer out of possible 4. My problem is that after I choose a random answer (which is the correct one) I have problems with choosing random 3 wrong answers which are not the same as answer 1 (they all come from the same table). My first mysql query gives me back 1 row my second mysql query gives me back 15 row (there were 16 - 1 which is the correct one). How can take only 3 rows out of that second query and output them into variables that I can later echo into a form? Can you help me understand this problem or can you propose an alternative method to deal with this? Here are my two queries: Code: [Select] // Fetch random answer (correct) $query = "SELECT * FROM psychofarm_brand WHERE `id`=".$randomNumber; $result = mysql_query($query); $row = mysql_fetch_array($result); // Fetch wrong answer $query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber; $result2 = mysql_query($query2); $row2 = mysql_fetch_array($result2); I hope this is clear. Thanks If I use this code: $values = range(0, 10); echo "<select name=\"my_field\">\n"; foreach($values as $v) { echo "<option value=\"$v\">$v</option>\n"; }echo "</select>\n"; to generate a dropdown, it will offer options from 1 thru 10. Is there any way to make them increment by .50 (ie: 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, etc.? Hi Guys I am trying to build a menu sing the code below, I can get the main headings, the problem arises when I try and produce the sub menus. Can someone please tell me where I am going wrong, or a better way of getting the desired result Code: [Select] <?php function submenu() { $submenu_sql=mysql_query("SELECT * FROM `tbl_menu` where `parent_menu_id` = '$menu_row[menu_id]'"); if(mysql_num_rows($submenu_sql)>0) { echo('<ul>'); while ($submenu_row=mysql_fetch_assoc($submenu_sql)) { echo('<li><a href="'.$submenu_row['link'].'">'.$submenu_row['menu_display'].'</a></li>'); } echo('</ul>'); }; } $menu_sql=mysql_query("SELECT * FROM `tbl_menu` where `parent_menu_id` = '0'"); if (mysql_num_rows($menu_sql)>0) { echo('<div id="accordian">'); while ($menu_row=mysql_fetch_assoc($menu_sql)) { echo('<div class="panel_container"> <h3><a href="'.$menu_row['link'].'">'.$menu_row['menu_display'].'</a></h3> <div class="panel_body">'); submenu(); echo('</div> </div>'); } echo('</div>'); } ?> I am getting the following error Notice: Undefined variable: menu_row in /home/sites/j-slink.co.uk/public_html/includes/menu.php on line 4 The desired result is to produce a menu with the following layout Code: [Select] <div id="accordion"> <div class="panel_container" id="panel1"> <h3 id="visible">Home</h3> <div id="panel1-body" class="panel_body"> </div> </div> <div class="panel_container" id="panel2"> <h3>Music</h3> <div id="panel2-body" class="panel_body"> <div> <ul> <li>Calling Card Mixing Tape</li> </ul> </div> </div> </div> <div class="panel_container" id="panel3"> <h3>Panel 3</h3> <div id="panel3-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> <div class="panel_container" id="panel4"> <h3>Panel 4</h3> <div id="panel4-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> <div class="panel_container" id="panel5"> <h3>Panel 5</h3> <div id="panel5-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> </div> Thanks in advance I was thinking how can I make a script that will build strings incrementing through each letter starting with "a" and ending with "zzzzzz" or what ever ending length I want such as 12. So if your were to output everything, you could find the words: "dog" "spider" "superman" "hippo" etc What would be the best way to do this, so I don't have to have a loop that is 500 blocks deep to show a word that is 500 letters example Quote a ... z aa ab ac ... az ba bb bc ... ... aaa aab aac In php how could I build a .net application? So I have a source that I want to build in .net, but i'm not sure how... Greetings freaks, I am trying to figure out how to build an array for the following purpose, and have just been spinning my wheels as it's a very new topic to me. I have a database with a table and the following important rows: ID, Parent, and Name. In this table, Parents are given the value 0 for 'Parent' and a unique 'ID' & 'Name' - children are given all unique 'ID' and 'Name' with the value for Parent being 'ID' of the Parent they belong to. Example: ( as 'ID', 'Parent', 'Name') 1, 0, John & 2, 1, Jane / 3, 0, Mark & 4, 3, Mary & 5, 3, Mindy. As you can see, the second names in each example (the children) are connected to their parents by the 'Parent' row, whereas the first names (the parents) are not associated with anybody else. What I would like to do is build an array that associates every child to the parent ([Parent] => child1 ). Simple MySQL grants me an array for the parents: $parent_pull = mysql_query('SELECT * FROM `table` WHERE Parent = 0') while($row = mysql_fetch_array($parent_pull)){ $name[] = $row['Name']; $id[] = $row['ID']; } Now, I want to be able to use the $id[]s to build arrays WHERE Parent = $id[]. I have tried in loops with $id[$i] & $i++ to cycle the array values, but PHP tells me there mysql_fetch_array() expects parameter 1 to be resource, boolean given ... on line 32. $i = 0 while($i < 10){ $child_pull = mysql_query('SELECT * FROM `table` WHERE Parent = $id[$i] '); $child = mysql_fetch_array($child_pull); $child[] = $child['Name']; <-- Line 32 $i++; } How would I go about pulling all the children and associating them with their parents? Hi All, I have an array like this where the cars are the first level of the array, and the owners are children of that first level: [Car 1] ----[Owner 1] [Car 2] ----[Owner 2] [Car 2] ----[Owner 3] [Car 2] ----[Owner 4] I want to convert it to: [Car 1] ----[Owner 1] [Car 2] ----[Owner 2] ----[Owner 3] ----[Owner 4] So I started making this script: $cars = array(); foreach($owners as $owner) { if(!in_array($owner['Car'], $cars)) { $cars[] = $owner['Car']; } echo array_search($owner['Car'], $cars, true); $cars[array_search($owner['Car'], $cars)]['Owner'][] = $owner['Owner']; } The result of this is the same as the beginning result, any ideas what could be causing that? Hey guys, I'm currently trying to make a tutorial website, and I'd like to have a fairly simple blog / news page that I can update with pictures and news stories. I would also like people to be able to comment on these postings. I'm pretty new to PHP but i made a simple blog using a php handler and a database table, but I was just wondering is there some 3rd party application I can use to manage my website with like users and articles and such without having to use their website layout? Like, programs such as WordPress and PHP-Fusion, Joomla etc, dont let you use your own html and css pages do they? I want to keep my website but have something purely in the background for me to manage the content and maybe like paste some script to my actual pages or whatever. Im just lost is all I need a little guidance =] Thanks a bunch. This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=352102.0 Hello i try to explain my situation, I have a dynamic table so i need to build a dynamic radiobutton list and build a dynamic sql req. First of all i build my table with radio button in a while So the radio button is named rdYanViau ( in the html it's working no prob with that ) Quote $name = $row2['FirstName'].$row2['LastName']; Quote <input type="radio" name="rd<?php echo $name; ?>" value="1"> Quote <input type="radio" name="rd<?php echo $name; ?>" value="2"> And in the same while i build a portion of my dynamic sql like that Quote $valueDeclare .= $row2['FirstName'].$row2['LastName'].', '; So in my insert this variable give me the FieldName and in the same while i build my $_POST variable for my sql sentence Quote $valueRadio .= "'".'".$_POST['."'".'rd'.$row2['FirstName'].$row2['LastName']."'".']."'."', "; This value is '".$_POST['rdYanViau']."' After that i build my sql with those 2 variables, the problem is when i execute my sql insert where he's supose to insert my rdButton value ... it's insert the string '".$_POST['rdYanViau']."' who give me a 0 in the table, he's not getting the value of '".$_POST['rdYanViau']."' he's inserting the string in the table. Here's the echo of my sql sentence : Quote INSERT INTO sondage (idSondage, username, idMatch, YanViau, YanViau2, YanViau3) VALUES (NULL , 'admin', '1', '".$_POST['YanViau']."', '".$_POST['YanViau2']."', '".$_POST['YanViau3']."') my echo should look like : Quote INSERT INTO sondage (idSondage, username, idMatch, YanViau, YanViau2, YanViau3) VALUES (NULL , 'admin', '1', '1', '2', '1') How i can build this dynamic sentence and have the radiobutton value and not the string value ... I hope you can understand my problem. Yan. Hi, I was trying to build a chat room and tried the code given he http://www.codingmix.com/2010/12/privat ... mment-form but when i try to go to chat , sending is disabled and I cant chat Can anyone help? Also I want to create a room for chat like: based on a topic some more features like Adding a topic........and while chat choose the category like speaking for the topic, against or neutral and with option to post a point while chatting please help pooja_dubey12 Forum Newbie Posts: 3 Joined: Fri Jul 03, 2009 8:29 am |