Jump to content

Chris.P

Members
  • Posts

    34
  • Joined

  • Last visited

    Never

Everything posted by Chris.P

  1. Let's see if I can explain this ok.. Basically I have a site and the page product categories use dynamically created URL's. I want to use these URL's to put on my products so I can list all the categories each product is in. But, as these URL's are not stored anywhere I'm not sure how to go about calling them. Is the best way going to be to modify the site to store the page URL's in MySQL? Or could they be stored within variables in PHP? A little guidance would be great! Thanks in advance
  2. Thanks Nightslyr for the advice. The code was actually written by someone else and I'm editing it as I'm learning so didn't spot what you mentioned. I'll get it changed! Thanks for the solution too I'll give it a go
  3. I've written the following code that grabs the meta description set in the MySQL database. Basically what I want to do it check the $metadescription variable and if it's empty run another query to put a different piece of information from the database in there. It seems like another if statement would be needed? function metadescription() { global $db; //checks if it's our category page if(isset($_GET['cat_id'])) { $cat_id = mysql_real_escape_string($_GET['cat_id']); $query = "SELECT * FROM categories WHERE cat_id = '$cat_id'"; $result = $db->query($query); $result = $db->fetchrow($result); $metadescription .= $result['cat_meta_description']; } return htmlentities($metadescription); }
  4. I have the following code. Basically I'm trying to count the amount of colours returned in the $colour variable. What I have is always returning 1, any ideas why? $colour = $db->query("SELECT * FROM colours WHERE prod_id = ".$row['prod_id']." AND LENGTH(col_image_1) > 0 ORDER BY col_rank ASC LIMIT 1"); $colourcount = mysql_num_rows($colour); if($db->numrows($colour)) { $colour = $db->fetchrow($colour); } else { unset($colour); } echo $colourcount;
  5. Ok, so I've read a few tutorials of OOP and have a basic understanding of how it works but where is it generally handy? I have a basic site created with PHP that consists of a form that submits data to MySQL and then lists the data from the MySQL database on another page. Kind of like a little online planner. I don't see how me converting my site to use OOP would help here. Maybe it's best for more complex situations such as a shopping cart where data needs to be shared across multiple pages?
  6. Thanks for the reply! I'm a bit of a noob so I'm going to have to study what you just said a little more before I get my head around it. I'm currently doing each page like this which looks like a different way of outputting to how you did it. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if (mysql_num_rows($result) > 0) { echo "<table>"; echo "<tr class='titles'>"; echo "<td><h2><a href='note.php?order=date_added'>Date Added</a></h2></td>"; echo "<td><h2><a href='note.php?order=note_description'>Note Description</a></h2></td>"; echo "<td><h2><a href='note.php?order=added_by'>Added By</a></h2></td>"; echo "<td><h2><a href='note.php?order=note_status'>Done?</a></h2></td>"; echo "<td><h2>Delete</h2></td>"; echo "</tr>"; while($row = mysql_fetch_assoc($result)) { $itemid = $row['note_id']; echo "<tr class='$rowclass'>"; //echo "<td><p>".$row['item_id']."</p></td>"; echo "<td>".$row['date_added']."</td>"; echo "<td>".$row['note_description']."</td>"; echo "<td>".$row['added_by']."</td>"; echo "<td>".$row['note_status']."</td>"; echo "<td><a href='?deleteitem=$itemid' onclick='return delete_me()' value='delete me box'>Delete</a></td>"; echo "</tr>"; } echo "</table>";
  7. At the moment I have multiple pages with different HTML tables coded in (some have 5 columns and some 6 etc) and PHP echoing out data to populate from the database. The table in each page holds different data. It looks messy to me to code a different table for every page so I'm wondering if it's possible to create a function to do this? The function would have to print tables with different amounts of columns and echo out different data depending on what the page is along with table headers etc. How would I go about that?
  8. I have the following slice of PHP that inserts data into the database and is later called into a page. I understand I need to use the get_magic_quotes() function to stop people from inserting javascript for example into the page when the data is called back. How do I go about this? <?php $profileID = $_POST["profileID"]; $comment = $_POST["commentBox"]; $poster = $_POST["poster"]; loginDetails(); $query = "INSERT INTO comments SET id = '$profileID', comment = '$comment', poster = '$poster' "; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); echo "<h4>Comment added<h4>"; ?>
  9. Ok thanks guys, obtaining the variable from the $_GET worked like a charm I should have thought of that! Can I ask out of curiosity why you don't recommend declaring a global?
  10. I'm not entirely sure what you mean, when you say "exit" from the single quotes do you mean by using the single quotes you put round the variable? I tried the solution you posted and I'm getting nothing from that variable. I can echo out the variable just fine from outside the addComment() function though. ???
  11. I want to put a variable as the content of a hidden field. I realise that normally you would just open up a set of php tags and echo the variable in which I have done in the past, although I need the whole form in php tags as I have made the form a function in order to hide it with sessions when no body is logged in. This seems to be causing a problem as I can't get the content of the variable into the field its just echoing the actual statement in. Any help? Here's the function contained in functions.php that holds the form. <?php function addComment() { // Check if we have established an authenticated session if (isset($_SESSION["authenticatedUser"])) { echo '<form id="form1" name="form1" method="post" action="addcomment.php"> Comment: <br /> <input name="hiddenField" type="hidden" value="<?php echo $bandid ?>" /> <br /> <textarea name="commentBox" id="commentBox"></textarea> <br /> <br /> <input name="Submit" type="submit" class="button" value="Submit" /> </form>'; } else { echo '<h2>Please log in to post a comment.</h2>'; } } ?> And here's the page that pulls the function. <?php session_start(); $bandid = $_GET["bandid"]; addComment();
  12. Thanks, yes I do understand it although I could see why you may find it hard to understand due to the typos. Apologies for that I shall check before I post in the future. I did try the AND statement before you posted which did not work although I think I may have missed off the table name. The solution you suggested works fine, thanks.
  13. I have created a join between two tables that gets all of the contents from both tables into one. I now need to do this again but only list the details for a particular id that that matches the variable $bandid. I cant figure out how to go about this as I don't be able to put in ore than one WHERE statement? ??? <?php $bandid = $_GET["bandid"]; echo "<h1>Profile for: ".$bandid."</h1><br />"; include_once 'functions.php'; loginDetails(); $query = "SELECT users.id, users.bandname, users.bio, users.history, users.discography, images.path ". "FROM users, images ". "WHERE users.id = images.id";
  14. Cheers I did it with a MySQL join. It seems to be working although images are not showing properly as the image locations are all getting %3c/a added on after the file extension. Has me baffled as to why this is happening, any ideas? Full query with output <?php include_once 'functions.php'; loginDetails(); session_start(); $query = "SELECT users.id, users.bandname, images.path ". "FROM users, images ". "WHERE users.id = images.id"; //$query = "SELECT * FROM users ORDER BY id"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table id=drtable>"; echo "<tr><td><h2>Band Image</h2></td>"; echo "<td><h2>Band Name</h2></td>"; echo "<td><h2>Band ID</h2></td></tr>"; while($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">","<img src=".$row['path']."</a></h2></td>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['bandname']."</a></h2></td>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['id']."</a></h2></td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No bands found."; } ?>
  15. I have a query below that gets its results and puts them in a table. The current results are from two fields in a db table. I am wondering if it is possible to create a second query to get results from a different db table and echo them out in the same html table as the ones already there? Many thanks. <?php include_once 'functions.php'; loginDetails(); session_start(); $query = "SELECT * FROM users ORDER BY id"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table id=drtable>"; echo "<tr><td><h2>Band ID</h2></td>"; echo "<td><h2>Band Name</h2></td></tr>"; while($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['id']."</a></h2></td>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['bandname']."</a></h2></td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No bands found."; } ?>
  16. Chris.P

    upload form

    I have the following upload form that I want to be able to browse images only. Is this possible or something I will have to implement through PHP? <form action="image_upload_action.php" method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="100000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form>
  17. Just solved it! It was indded an update I needed and all is working well! Thanks for the help. This was the solution. $query = "UPDATE images SET name = '$fileName', size = '$fileSize', type = '$fileType', path = '$filePath' WHERE id = $bandid";
  18. There is already a user id in the images field that is auto incremented ever time someone signs up. All I want to do is put in the information from the upload script into the row with the matching user id (bandid) from the session. Should I be using an UPDATE WHERE instead of this or am I completely off track? Apologies if I'm way off I'm new to all this.
  19. Cheers, I removed them and it works fine except it inserts a new row rather than inserting into the row with the selected id. I can't see why this is happening although I'm sure there is a simple explanation. ??? edit heres the full script being adopted if it helps: <?php include_once 'functions.php'; loginDetails(); $uploadDir = 'image_upload/'; $bandid = $_SESSION['bandid']; if(isset($_POST['upload'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; // get the file extension first $ext = substr(strrchr($fileName, "."), 1); // make the random file name $randName = md5(rand() * time()); // and now we have the unique file name for the upload file $filePath = $uploadDir . $randName . '.' . $ext; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $query = "INSERT INTO images (name, size, type, path ) ". "SELECT '$fileName', '$fileSize', '$fileType', '$filePath' FROM images WHERE id = $bandid"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); echo "<br>Completed<br>"; } ?>
  20. Just trying this out now although I'm getting this error: Error, query failed : Operand should contain 1 column(s) Does this basically mean nothing was selected matching my query? Heres the full query I'm now using. $query = "INSERT INTO images (name, size, type, path ) ". "SELECT ('$fileName', '$fileSize', '$fileType', '$filePath') FROM images WHERE id = '$bandid'";
  21. Noted thanks although I'm not too sure how to do that with a result variable like this: $result = move_uploaded_file($tmpName, $filePath);
  22. Hmm that gives the same result as above, just shows an empty variable. Makes me think the variable is empty but like I say it can't be because I can echo its contents out just fine. ???
  23. That stops the variable name being used but it shows an empty variable in the url now. :-\
  24. Ok in my user.php page I have the following section of PHP. <?php include_once 'functions.php'; //This includes some common functions $bandid = $_GET['band']; echo $bandid; showLogin(); //This is a function in 'functions.php' ?> Heres the functions.php file that conatins the showlogin() function that called form the user.php file above and contains the link concerned. <?php //login details function loginDetails(){ //db access details $hostname = "xxxxx"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; // (2) Open the database connection $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect to database!");; // (3) Select your database mysql_select_db($dbname) or die ("Unable to select database!"); } //login script function showLogin() { // Check if we have established an authenticated session if (isset($_SESSION["authenticatedUser"])) { $_SESSION["message"] = "You are logged in as ". $_SESSION['authenticatedUser']; echo '<h2><a href="user.php">Control Panel</a><br /> <a href="update_details.php">Update details</a><br /> <a href="image_upload.php?bandid=".$bandid."">Upload Image</a><br /> <a href="logout.php">Logout</a></h2>'; } else { echo '<h1>Login</h1> <form name="form1" id="form1" method="post" action="loginaction.php"> <h2>Username: <input name="loginUser" type="text" id="loginUser" size="8" /> <br /> Password: <input name="loginPass" type="password" id="loginPass" size="8"/> <br /> <input type="submit" name="Submit" value="Go!" class="button"/> </h2> </form> <h2><a href="register_details.php">Register</a><br /> <a href="recoverpass.php">Forgotten password?</a></h2>'; } echo "<h2>". $_SESSION["message"]. "</h2>"; } //End showLogin ?>
  25. I'm trying to get this script I have to insert values into a table but only where the value of the id row matches what I have in a variable. Script I had was this $query = "INSERT INTO images (name, size, type, path ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')"; I presumed I would be able to tag the WHERE parameter on at the end with the variable like so. $query = "INSERT INTO images (name, size, type, path ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath') WHERE id = '$bandid'"; But apparently not as I get this Error, query failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id = '5'' at line 1
×
×
  • 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.