roldahayes Posted December 4, 2009 Share Posted December 4, 2009 Hi, I know this should be very basic stuff but I'm really stuck with it! I'm trying to include a field onto a page (BusinessName) <?php echo "$BusinessName"; ?> This is not working - Do I have to include something else in the page to let it know about the field. A variable? Quote Link to comment https://forums.phpfreaks.com/topic/184022-echo-help-please/ Share on other sites More sharing options...
premiso Posted December 4, 2009 Share Posted December 4, 2009 When you say field, do you mean an HTML form field? Has $BusinessName been defined else where? If you have more code please show us so we can better help you as well, just remember to place code inside of tags. Quote Link to comment https://forums.phpfreaks.com/topic/184022-echo-help-please/#findComment-971547 Share on other sites More sharing options...
roldahayes Posted December 4, 2009 Author Share Posted December 4, 2009 Its a field in the database. i want the "businessname" to appear at the top of that business page. the pages are defined by the username i.e. index.php?user=test@test.com So somehow I need it to pull the "businessname" from the database in relation to the user ID... i hope that makes sense! Quote Link to comment https://forums.phpfreaks.com/topic/184022-echo-help-please/#findComment-971552 Share on other sites More sharing options...
premiso Posted December 4, 2009 Share Posted December 4, 2009 Do you even have a database setup? If so which one, MySQL ? This is a rough example that you will need to cater to your database: <?php $conn = mysql_connect("localhost", "username", "password") or trigger_error("Could not connect: " . mysql_error()); mysql_select_db("yourdatabasename") or trigger_error("Unable to select DB: " . mysql_error()); if (isset($_GET['user'])) { $user = mysql_real_escape_string($_GET['user']); // mysql_real_escape_string will prevent against sql injection $query = mysql_query("SELECT BusinessName FROM table_name WHERE userID = '$user'") or trigger_error("Query Failed: " . mysql_error()); while ($row = mysql_fetch_assoc($query)) { echo $row['BusniessName'] . "<br />"; } }else { echo "No user supplied."; } ?> If any of that is confusing I would go find a tutorial on PHP / MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/184022-echo-help-please/#findComment-971555 Share on other sites More sharing options...
roldahayes Posted December 4, 2009 Author Share Posted December 4, 2009 That all makes sense but maybe I'm over confusing things with my explanation You know how a website will have: Welcome "name of user is inserted here" ? thats what I want to but the field is the business name. If it helps, the table is called "clients" ? The page code is: <?php //Set unread to 0 $sql = "DELETE FROM admin_unread WHERE user_id = '$user_id'"; if (!mysql_query($sql,$conn)) { die('Error: ' . mysql_error()); } //Save Comment if(isset($_POST['comment'])) { $row2 = mysql_fetch_array($result2, MYSQL_ASSOC); $email = $row2['email']; $comment = mysql_escape_string($_POST['comment']); $comment = strip_tags($comment); $comment = stripslashes($comment); $commentSQL = "INSERT INTO notes (text, isadmin, user_id) VALUES ('$comment', '1', '$user_id')"; $commentSQL2 = "UPDATE clients SET unread = 1 WHERE id = '$user_id'"; mysql_query($commentSQL,$conn) or die('Error: ' . mysql_error()); mysql_query($commentSQL2,$conn) or die('Error: ' . mysql_error()); //Send Notification Email //$emailSubject = "You Have Received a Message About Your Project"; //$message = "Here is the message from the admin:\n\n".$comment; //mail($email, $emailSubject, $message, "From: ".$fromEmail); echo "<p class=\"good\">Your note has been added.</p>"; } ?> <h1><?php echo $BusinessName;?></h1> <h1><a href="javascript:history.go(-1)"><img src="../../images/back.png" width="50" height="40" border="0" /></a></h1> <div id="fileTable" class="margin"> <?php //List Notes while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $text = stripcslashes($row['text']); if(!$row['isadmin'] == 1) { echo "<div class=\"fileRow\">".$text."</div>"; } else { echo "<div class=\"fileRow admin\">".$text."</div>"; } } //If new, then list if(isset($_POST['comment'])) { echo "<div class=\"fileRow admin\">".stripslashes($comment)."</div>"; } ?> </div> <div class="right-box"> <h2>Add new note </h2> <form method="post"> <textarea id="comment" name="comment" class="margin"></textarea> <input name="submit" type="submit" value="Add Note" class="submit" /> </form> </div> <script type="text/javascript"> //Add validation for inputs var comment = new LiveValidation('comment'); comment.add( Validate.Presence ); </script> Quote Link to comment https://forums.phpfreaks.com/topic/184022-echo-help-please/#findComment-971582 Share on other sites More sharing options...
raytri Posted December 8, 2009 Share Posted December 8, 2009 As the previous poster commented, you're trying to show $BusinessName when you haven't defined it yet. To show $BusinessName, you need to run the SQL query, fetch the query results as an array, and define $BusinessName as the contents of the appropriate database column -- BEFORE you try to echo $BusinessName. So you need to do stuff in this order: 1. Define an SQL query ($sql). 2. Run the query: $myQuery=mysql_query[$sql]; 2. Fetch the array: $myArray = mysql_fetch_array($myQuery); 3. Define $BusinessName: $BusinessName=$myArray['BusinessName']; 4. Echo $BusinessName anywhere you like. Going a step further: Once you have $BusinessName defined, you can use it on subsequent pages without a new database query by either using sessions and defining it as a session variable, or passing it as a variable in the URL (using $_GET) or in a form field (using $_POST). Quote Link to comment https://forums.phpfreaks.com/topic/184022-echo-help-please/#findComment-973392 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.