| | |
Super newbie needs help
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I have written lots of php dealing with html strings, but it's my first time trying to get a very simple page to function. It has a textarea, and I guess that all I need to do is get the text into the database on the server. I really have no idea how to go about this, and I'm so frustrated that I can't find what I'm looking for. It's simple (again, guessing) as all there is, for now, is the text area. The client (user) types in some text, sends it, after previewing it through my nifty string work :-) and it goes to the database. It would be very nice of someone to walk me through this. I'm the type of learner who needs guidance over "solutions". Sorry if I seem dumb, but I just need help. I'm only a hobbyist programmer.
My html:
PHP:
PHP Syntax (Toggle Plain Text)
<form action="insert.php" method="post" name="form"> <br/><span style="position:relative: left: -20%; font-size: small;"></span><br/> <textarea name="text" id="DynamicText" style="font-family: arial, helvetica, sans-serif; font-size: large;" cols="100" rows="5" wrap="soft" onclick="document.form.text.value='';"> Lorem ipsum . . . </textarea> <br/> <br/> <input type="button" value="Preview" onclick="reloadTextDiv();" /> </form>
PHP Syntax (Toggle Plain Text)
$username = "-------"; $password = "-------"; $database = "----"; $text = $_POST['text']; mysql_connect("mysql.gravityway.com", $username, $password); @mysql_select_db($database) or die("Unable to select database"); $query = "INSERT INTO ---- VALUES ('','$first')"; mysql_query($query); mysql_close();
Last edited by tefflox; Oct 30th, 2007 at 5:14 am.
Here is my progress. I have connected to the localhost database and inserted text.
Insertion code:
I need help with retrieving the data. This is mainly copynpaste code, but it's a start. I get an unknown category error. The table has two columns, the int key and a text field.
Insertion code:
PHP Syntax (Toggle Plain Text)
<?php $username = "root"; $password = "-------"; $database = "grav"; $host = "localhost"; $text = $_POST['text']; $cid = mysql_connect($host, $username, $password); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } if ($_SERVER['REQUEST_METHOD'] == "POST") { /* // the following 4 lines are needed if your server has register_globals set to Off $category = $_POST['category']; $sitename = $_POST['sitename']; $siteurl = $_POST['siteurl']; $description = $_POST['description']; */ $SQL = " INSERT INTO posts VALUES ('', '$text')"; $result = mysql_db_query($database, $SQL, $cid); if (!$result) { echo("ERROR: " . mysql_error() . "\n".$SQL."\n"); } else echo ("New Post Added\n"); } mysql_close($cid); ?>
PHP Syntax (Toggle Plain Text)
<?php $username = "root"; $password = "-------"; $database = "grav"; $host = "localhost"; # connect to database $cid = mysql_connect($host,$username,$password); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } ?> <html> <head> <title>Display Posts</title> </head> <body> <?php $category = "Posts"; # setup SQL statement $SQL = " SELECT * FROM posts WHERE category = '$category' "; # execute SQL statement $retid = mysql_db_query($database, $SQL, $cid); # check for errors if (!$retid) { echo( mysql_error()); } else { # display results echo ("<h3><b>$category</b></h3>\n"); while ($row = mysql_fetch_array($retid)) { $post = $row["post"]; echo ($post."<br/>\n"); } } ?> </body> </html>
•
•
•
•
I need help with retrieving the data. This is mainly copynpaste code, but it's a start. I get an unknown category error. The table has two columns, the int key and a text field.
PHP Syntax (Toggle Plain Text)
<?php $username = "root"; $password = "-------"; $database = "grav"; $host = "localhost"; # connect to database $cid = mysql_connect($host,$username,$password); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } ?> <html> <head> <title>Display Posts</title> </head> <body> <?php $category = "Posts"; # setup SQL statement $SQL = " SELECT * FROM posts WHERE category = '$category' "; # execute SQL statement $retid = mysql_db_query($database, $SQL, $cid); # check for errors if (!$retid) { echo( mysql_error()); } else { # display results echo ("<h3><b>$category</b></h3>\n"); while ($row = mysql_fetch_array($retid)) { $post = $row["post"]; echo ($post."<br/>\n"); } } ?> </body> </html>
please replace this part of your code:
php Syntax (Toggle Plain Text)
<?php $category = "Posts"; # setup SQL statement $SQL = " SELECT * FROM posts WHERE category = '$category' "; # execute SQL statement $retid = mysql_db_query($database, $SQL, $cid); # check for errors if (!$retid) { echo( mysql_error()); } else { # display results echo ("<h3><b>$category</b></h3>\n"); while ($row = mysql_fetch_array($retid)) { $post = $row["post"]; echo ($post."<br/>\n"); } } ?>
with this one:
php Syntax (Toggle Plain Text)
$category = "Posts"; # setup SQL statement $SQL = " SELECT * FROM posts WHERE category = '$category' "; # execute SQL statement $retid = mysql_query($query); $result=mysql_query($retid) or die (mysql_error()); $num=mysql_num_rows($result); # display results while ($i<$num) { $post=mysql_result($result,$i,"post"); echo "<h3><b>'.$category.'</b></h3>\n"; echo $post; $i++; }
I think that you are getting an unknown column because the field "post" is not the column name of what you are trying to display.
Look at your database, look what field do you want to display and replace "post" in
php Syntax (Toggle Plain Text)
$post=mysql_result($result,$i,"post");
with that field.
Or better yet please display here all your table fields so I can guide you.
Actually, there are problems with the code provided. I had mistakenly copied my own back in. This works ---
PHP Syntax (Toggle Plain Text)
# setup SQL statement $SQL = " SELECT post FROM posts "; # execute SQL statement $retid = mysql_db_query($database, $SQL, $cid); # check for errors if (!$retid) { echo( mysql_error()); } else { # display results echo ("<h3><b>Posts</b></h3>\n"); while ($row = mysql_fetch_array($retid)) { $post = $row["post"]; echo ($post."<br/>\n"); } }
![]() |
Similar Threads
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- downloading from the net is incomplete! (Web Browsers)
- How to network two Win98 machines (Networking Hardware Configuration)
- What super power would you most likely want? (Geeks' Lounge)
Other Threads in the PHP Forum
- Previous Thread: PHP Prerequisite?
- Next Thread: Export RSS 1.0 or 2.0 feeds to an array
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array basics beginner binary broken cakephp checkbox class cms code cron curl database date datepart directory display download dynamic echo email error file files folder form forms function functions google head href htaccess html image include insert integration ip java javascript joomla limit link list login loop mail menu mlm mod_rewrite multiple mysql number oop parse password paypal pdf php phpmyadmin problem query radio random recourse recursion regex remote script search seo server sessions sms soap source space sql structure syntax system table tutorial update upload url validation validator variable video web webdesign xml youtube





