Jump to content

chrisguk

Members
  • Posts

    24
  • Joined

  • Last visited

Everything posted by chrisguk

  1. Its ok woohoo I figured it alone if ($stmt->rowCount() > 0) { while ($row = $stmt->fetch()) { $title = $row[1]; $description = $row[2]; $keywords = $row[3]; } } else { $title = 'Set Default Title'; $description = 'Set Default Description'; $keywords = 'Set Default Keywords'; }
  2. Ok I seem to be getting somewhere now. I used this: while($row = $stmt->fetch()) { print_r($row); } and it printed this: Array ( [id] => 50 [0] => 50 => home [1] => home [title] => Home [2] => Home [descr] => ssssgdgdfgbooooooooooo [3] => ssssgdgdfgbooooooooooo [keywords] => sdfsfsdf [4] => sdfsfsdf ) So I am guessing the query is working. @Jessica I understand that you probably have thousands of posts regarding similar subjects. But please understand I am new to this stuff. I do absolutely appreciate the help though because every piece of advice you give me will help me learn better. Can I ask some advice for the final step please ?
  3. I added that code and inserted the query above. I didnt have errors turned on full but I have now. I ran the above and no errors are produced. based on my previous code what would be the best way to include my desired output, as I am guessing $row is now undefined: if ($stmt->num_rows > 0) { while ($row = $stmt->fetch_object()) { $title = $row->title; $description = $row->descr; $keywords = $row->keywords; } } else { $title = 'Set Default Title'; $description = 'Set Default Description'; $keywords = 'Set Default Keywords'; }
  4. Jess, thank you for your reply. The output is the default values declared in the bottom half of the code. Obviously I am expecting it to produce and output based on the page name that has been selected. For example: I select the home page and the title, keywords and description are pulled from the DB and populated using variables. This is really just the begining as I ultimately want to start populating repetitive text values across the site too in the future. I am extremely new to PDO and prepare statements as for some unknown reason I have been doing things the old way for a long time. When you mention bindValue() between 13-14, would this following example be the correct usage: $stmt->bindValue($page, PDO::PARAM_STR); I have tried it like that and still the code is not retrieving the data. Are there any debug steps I could take to try and understand the function better? Thank you for your patience with me so far Chris
  5. I really hope someone can help me because its driving me mad trying to understand what I need to do here. I have read the PDO and prepare statements manual, but just cant work out whats wrong with my code below: $dsn = "mysql:host=localhost;dbname=maindb"; $user = "root"; $password = "mypass"; try { $pdo = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } // Database $page = isset($_GET['page']) ? $_GET['page'] : 'home'; #$page = $pdo->real_escape_string($page); #$sql = $mysqli->query("SELECT * FROM url WHERE page = '" . $page . "' LIMIT 1"); $stmt = $pdo->prepare("SELECT * FROM url WHERE page = ? LIMIT 1"); $stmt->execute(array($page)); if ($pdo->connect_errno) { printf("Connect failed: %s\n", $pdo->connect_error); exit(); } if ($stmt->num_rows > 0) { while ($row = $stmt->fetch_object()) { $title = $row->title; $description = $row->descr; $keywords = $row->keywords; } } else { $title = 'Set Default Title'; $description = 'Set Default Description'; $keywords = 'Set Default Keywords'; } I would be really glad for any assistance
  6. I get that, I think But how can I be sure that it will use the .mo file in the locale folder. What I am attempting to do is have a backend panel that I can choose the preferred language from a drop down and then hit the save button. The that changes the site wide language.
  7. Hi, I have managed to get the code below working with a few tweaks here and there. However, you can see that I have written a small function in the file name local.php to allow the user to select "English" or "German" in the link on the test.php page. This works fine but it will only work on that particular webpage. As soon as I move away from that page the locale will return back to "en_US"(English). Is there a way I could implement lets say an option dropdown and save the locale across the whole site. Just in case its relevant I have a MySQL DB available for use if thats appropriate. local.php <?php function english(){ $directory = dirname(__FILE__).'/locale'; $domain = 'messages'; $locale ="en_UK.utf8"; setlocale( LC_MESSAGES, $locale); bindtextdomain($domain, $directory); textdomain($domain); bind_textdomain_codeset($domain, 'UTF-8'); } function german(){ $directory = dirname(__FILE__).'/locale'; $domain = 'messages'; $locale ="de_DE.utf8"; setlocale( LC_MESSAGES, $locale); bindtextdomain($domain, $directory); textdomain($domain); bind_textdomain_codeset($domain, 'UTF-8'); } //Pagination and normal view switch if (isset($_GET['run'])) $linkchoice=$_GET['run']; else $linkchoice=''; switch($linkchoice){ case 'English' : english(); break; case 'German' : german(); break; default : english(); break; } ?> test.php <?php include("local.php"); ?> <html><head></head> <body> <?php echo "<p><a href='?run=English'>English</a> | <a href='?run=German'>German</a> </p>"; ?> <br /> <?php echo gettext("Welcome to My PHP Application"); echo "<br />"; // Or use the alias _() for gettext() echo _("Have a nice day"); ?> </body> </html> Many thanks in advance!
  8. I would love to say that fixed it but it hasnt
  9. Hi dark, Unfortunately that has not resolved the issue. I have made a final amendment to the code below, if you can figure out the issue you are a better man than me lol: <?php $gettext_domain = 'sk'; // change by language setlocale(LC_ALL, 'sk_SK.UTF-8'); // change by language, directory name sk_SK, not sk_SK.UTF-8 bindtextdomain($gettext_domain, "lang"); textdomain($gettext_domain); bind_textdomain_codeset($gettext_domain, 'UTF-8'); local.php $locale = false; if (isSet($_GET["locale"])){ $locale = $_GET["locale"]; setcookie("locale", $locale, time()+60*60*24*30, "/");// save a cookie } if (!$locale && isSet($_COOKIE["locale"])){ $locale = $_COOKIE["locale"]; } putenv("LC_ALL=$locale");//needed on some systems putenv("LANGUAGE=$locale");//needed on some systems setlocale(LC_ALL, $locale); bindtextdomain("messages", $_SERVER['DOCUMENT_ROOT']. "locale"); bind_textdomain_codeset("messages", "UTF-8"); textdomain("messages"); ?> test.php <?php require_once "local.php";?> <html><head></head> <body> <a href="?locale=en_US">English</a> | <a href="?locale=es_ES">Spanish</a> | <a href="?locale=de_DE">German</a> <br> <?php echo _("Hello World!");?><br> <p><?php echo _("My name is");?> Bob.</p> </body> </html>
  10. I think I have fixed it, I needed to install the de_DE language pack on my server
  11. Hi, Here it is: <?php $gettext_domain = 'de'; // change by language setlocale(LC_ALL, 'de_DE.UTF-8'); // change by language, directory name sk_SK, not sk_SK.UTF-8 bindtextdomain($gettext_domain, "lang"); textdomain($gettext_domain); bind_textdomain_codeset($gettext_domain, 'UTF-8'); $locale = "de_DE"; if (isSet($_GET["locale"])) $locale = $_GET["locale"]; putenv("LC_ALL=$locale"); setlocale(LC_ALL, $locale); bindtextdomain("messages", "locale"); bind_textdomain_codeset("messages", 'UTF-8'); textdomain("messages"); ?> test.php <?php require_once "local.php"; ?> <html><head></head> <body> <a href='?locale=en_US'>English</a> | <a href='?locale=es_ES'>Spanish</a> | <a href='?locale=de_DE'>German</a> <br> <?php echo _("Hello World!");?><br> <?php $world = 'world'; printf(gettext("Hello %s!"), $world); ?> <p><?php echo _("My name is");?> Bob.</p> </body> </html>
  12. Unfortunately that has not fixed the issue
  13. Hi, I have looked through the forum for a fix for this but I was unable to find one. Basically the code I have provided below is not giving me the correct output in other words changing the text to German. I am using an "Ubuntu" environment with Apache2 and php5 installed. I have checked that php-gettext is installed. I have also attached my .mo file (I have added the .txt extension to upload here) local.php <?php $locale = false; if (isSet($_GET["locale"])){ $locale = $_GET["locale"]; setcookie("locale", $locale, time()+60*60*24*30, "/");// save a cookie } if (!$locale && isSet($_COOKIE["locale"])){ $locale = $_COOKIE["locale"]; } putenv("LC_ALL=$locale");//needed on some systems putenv("LANGUAGE=$locale");//needed on some systems setlocale(LC_ALL, $locale); bindtextdomain("messages", $_SERVER["DOCUMENT_ROOT"]."locale"); bind_textdomain_codeset("messages", "UTF-8"); textdomain("messages"); ?> test.php <?php require_once "local.php"; echo $_SERVER["DOCUMENT_ROOT"]."locale"; //for testing ?> <html><head></head> <body> <a href='?locale=en_US'>English</a> | <a href='?locale=es_ES'>Spanish</a> | <a href='?locale=de_DE'>German</a> <br> <?php echo _("Hello World!");?><br> <p><?php echo _("My name is");?> Bob.</p> </body> </html>
  14. I not a fan of short tags either: <? somephp; ?> should be <?php somephp; ?>
  15. If I am reading this right? First you are referring to .aspx, this is a PHP forum. To mask URLs you can use a URL redirect rule in .htaccess but in your case it does not fall into that category because you seem to be passing a URL string to complete either a form/registration submission, therefore the request would not be complete. In PHP the "GET" method is usually associated with strings in the URL but "POST" is favoured as the variable(values) are hidden in the server side. Im not an aspx guru so im not sure if you can achieve the same but im guessing you can?
  16. <?php /* ---------------------------------------- */ // Change these parameters with your Twitter // user name and Twitter password. /* ---------------------------------------- */ $twitter_username ='yourTwitterUserName'; $twitter_psw ='yourTwitterPassword'; /* ---------------------------------------- */ /* Don't change the code beloW /* ---------------------------------------- */ require('twitterAPI.php'); if(isset($_POST['twitter_msg'])){ $twitter_message=$_POST['twitter_msg']; if(strlen($twitter_message)<1){ $error=1; } else { $twitter_status=postToTwitter($twitter_username, $twitter_psw, $twitter_message); } } /* ---------------------------------------- */ ?> <!-- Send message to Twitter --> <?php if(isset($_POST['twitter_msg']) && !isset($error)){?> <div class="msg"><?php echo $twitter_status ?></div> <?php } else if(isset($error)){?> <div class="msg">Error: please insert a message!</div> <?php }?> <p><strong>What are you doing?</strong></p> <form action="insertTwitterMsg.php" method="post"> <input name="twitter_msg" type="text" id="twitter_msg" size="40" maxlength="140"/> <input type="submit" name="button" id="button" value="post" /> </form>
  17. Maybe you should ask this question in MrExcel.com instead of a PHP Coding forum?
  18. //LINE 155 ======== if ($stmt = $mysqli->prepare("INSERT URL (page, title, description, keywords) VALUES (?, ?)")) { $stmt->bind_param("ss", $page, $title, $description, $keywords); $stmt->execute(); $stmt->close(); }
  19. Hi Jes, I am having issues with this error which I believe is stopping the script: array(5) { ["page"]=> string(6) "asdasd" ["title"]=> string(4) "rere" ["description"]=> string(5) "Test2" ["keywords"]=> string(23) "device helper structure" ["submit"]=> string(6) "Submit" } Warning: mysqli::prepare() [mysqli.prepare]: (21S01/1136): Column count doesn't match value count at row 1 in /var/www/test/records.php on line 155 ERROR: Could not prepare SQL statement. I just cant trace it
  20. Guess I could make use of VAR_DUMP too just in case, Darn, echo city here we go!!
  21. Jes Sorry I posted the wrong extract: <?php // connect to the database include("connect.php"); // creates the new/edit record form // A function that is easily reusable function renderForm($page = '', $title = '', $description = '', $keywords = '', $error = '', $id = '') { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title> <?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?> </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1> <?php if ($error != '') { echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>"; } ?> <form action="" method="post"> <div> <?php if ($id != '') { ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <p>ID: <?php echo $id; ?></p> <?php } ?> <strong>Page: *</strong> <input type="text" name="page" value="<?php echo $page; ?>"/><br/> <strong>Title: *</strong> <input type="text" name="title" value="<?php echo $title; ?>"/><br/> <strong>Description: *</strong> <input type="text" name="description" value="<?php echo $description; ?>"/><br/> <strong>Keywords: *</strong> <input type="text" name="keywords" value="<?php echo $keywords; ?>"/> <p>* required</p> <input type="submit" name="submit" value="Submit" /> </div> </form> </body> </html> <?php } /* EDIT RECORD */ // if the 'id' variable is set in the URL, we know that we need to edit a record if (isset($_GET['id'])) { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // make sure the 'id' in the URL is valid if (is_numeric($_POST['id'])) { // get variables from the URL/form $id = $_POST['id']; $page = htmlentities($_POST['page'], ENT_QUOTES); $title = htmlentities($_POST['title'], ENT_QUOTES); $description = htmlentities($_POST['description'], ENT_QUOTES); $keywords = htmlentities($_POST['keywords'], ENT_QUOTES); // check that page and title are both not empty if ($page == '' || $title == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($page, $title, $description, $keywords, $error, $id); } else { // if everything is fine, update the record in the database if ($stmt = $mysqli->prepare("UPDATE URL SET page = ?, title = ?, description = ?, keywords = ? WHERE id=?")) { $stmt->bind_param("ssi", $page, $title, $description, $keywords, $id); $stmt->execute(); $stmt->close(); } // show an error message if the query has an error else { echo "ERROR: could not prepare SQL statement."; } // redirect the user once the form is updated header("Location: view.php"); } } // if the 'id' variable is not valid, show an error message else { echo "Error!"; } } // if the form hasn't been submitted yet, get the info from the database and show the form else { // make sure the 'id' value is valid if (is_numeric($_GET['id']) && $_GET['id'] > 0) { // get 'id' from URL $id = $_GET['id']; // get the record from the database if($stmt = $mysqli->prepare("SELECT * FROM URL WHERE id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $page, $title, $description, $keywords); $stmt->fetch(); // show the form renderForm($page, $title, $description, $keywords, NULL, $id); $stmt->close(); } // show an error if the query has an error else { echo "Error: could not prepare SQL statement"; } } // if the 'id' value is not valid, redirect the user back to the view.php page else { header("Location: view.php"); } } } /* NEW RECORD */ // if the 'id' variable is not set in the URL, we must be creating a new record else { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // get the form data $page = htmlentities($_POST['page'], ENT_QUOTES); $title = htmlentities($_POST['title'], ENT_QUOTES); $description = htmlentities($_POST['description'], ENT_QUOTES); $keywords = htmlentities($_POST['keywords'], ENT_QUOTES); // check that page and title are both not empty if ($page == '' || $title == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($page, $title,$description, $keywords, $error); } else { // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT URL (page, title, description, keywords) VALUES (?, ?)")) { $stmt->bind_param("ss", $page, $title, $description, $keywords); $stmt->execute(); $stmt->close(); } // show an error if the query has an error else { echo "ERROR: Could not prepare SQL statement."; } // redirect the user header("Location: view.php"); } } // if the form hasn't been submitted yet, show the form else { renderForm(); } } // close the mysqli connection $mysqli->close(); ?>
  22. Okay, I have moved on a few more steps and now starting to add, edit and delete records. The first issues I encountered before have been fixed as I read up on the prepare statement. I had to rewrite most of the code though. With this issue I am having trouble get the script to update or add new records. It produces no error messages: My DB: Column Type Null Default id int(11) No Autoincrement page varchar(255) Yes NULL title varchar(255) Yes NULL description varchar(255) Yes NULL keywords varchar(255) Yes NULL The code: view.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>View Records</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <h1>View Records</h1> <p><b>View All</b> | <a href="paginated.php">View Paginated</a></p> <?php // connect to the database include('connect.php'); // get the records from the database if ($result = $mysqli->query("SELECT * FROM URL ORDER BY id")) { // display records if there are records to display if ($result->num_rows > 0) { // display records in a table echo "<table border='1' cellpadding='10'>"; // set table headers echo " <tr> <th>ID</th> <th>Page</th> <th>Title</th> <th>Description</th> <th>Keywords</th> <th>Edit</th> <th>Delete</th></tr></tr>"; while ($row = $result->fetch_object()) { // set up a row for each record echo "<tr>"; echo "<td>" . $row->id . "</td>"; echo "<td>" . $row->page . "</td>"; echo "<td>" . $row->title . "</td>"; echo "<td>" . $row->description . "</td>"; echo "<td>" . $row->keywords . "</td>"; echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>"; echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; } // if there are no records in the database, display an alert message else { echo "No results to display!"; } } // show an error if there is an issue with the database query else { echo "Error: " . $mysqli->error; } // close database connection $mysqli->close(); ?> <a href="records.php">Add New Record</a> </body> </html> connect.php // connect to the database $mysqli = new mysqli($server, $user, $pass, $db); // show errors mysqli_report(MYSQLI_REPORT_ERROR);
  23. Hi, I think my problem is a mixture of MODREWRITE and PHP. I have the following .htaccess located in the web root. RewriteEngine On RewriteRule ^about/(([^/]+/)*[^/.]+)$ /about/index.php?p=$1 [L] RewriteRule ^(([^/]+/)*[^/.]+)$ index.php?p=$1 [L] The index.php example below is located in the folder "about". Just for your info I also have an index at the web root too serving other files. <?php $page = isset($_GET['p']) ? $_GET['p'] : '/about/about'; switch($page) { /*----------------------- PAGES -----------------------------------*/ case 'about/profiles': $title = 'My Title'; $keyword = 'A few keywords'; $description = 'A good description.'; break; default: $title = 'My Title'; $keyword = 'A few keywords'; $description = 'A good description.'; break; } include($_SERVER['DOCUMENT_ROOT']. '/include/header.php'); include($_SERVER['DOCUMENT_ROOT']. 'about/'.$page.'.php'); include($_SERVER['DOCUMENT_ROOT']. "/include/footer.php"); ?> This is my problem: When I click on the link http://mysite.co.uk/about/ it throws the errors out below. When I click on the link http://mysite.co.uk/about/profiles/ it works fine and for the life of me I cant understand what I have done wrong. Warning: include(/var/www/mysite/about//about/about.php) [function.include]: failed to open stream: No such file or directory in /var/www/mysite/about/index.php on line 22 Warning: include() [function.include]: Failed opening '/var/www/mysite/about//about/about.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/mysite/about/index.php on line 22 I am a kinda of newbie so any help/guidance will be gratefully received
  24. Hi, Up until now I have been using the following .htaccess content: Code: RewriteEngine On RewriteCond %{REQUEST_URI} !\/$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1 !^robots\.txt$ RewriteRule ^([^/]+)/? index.php?p=$1 [L,QSA] There has been no issue with the way it works until I want to do something more. Example of my webpages: index.php home.php contact.php video.php music.php include/header.php include/footer.php My "index.php" pulls the pages in using switch: PHP Code: $page = isset($_GET['p']) ? $_GET['p'] : 'home'; switch($page) { case etc etc........ include('include/header.php'); include(''.$page.'.php'); include('include/footer.php'); Now the problem I have is within the "video.php" file I have a link that goes to another file within a directory folder. The folder location is: /video/newlist.php But the problem I have is when I select the "video.php" link it takes me to the video folder and not the file called "video.php" Is there anyway around this?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.