Jump to content

perky416

Members
  • Posts

    177
  • Joined

  • Last visited

Everything posted by perky416

  1. Hi everyone, Sorry i've just found my problem, i had the $i++ out side of the while loop.
  2. Hi guys, I have a html table that contains rows of data, and each row has a check box next to it so the user can delete each row if the box is ticked I am using the following code to display a warning message before the entry is actually deleted, however lets say I have 10 rows of data, and only tick 1 check box, the same warning message is displayed 10 times instead of just the 1. if($_POST['submit']){ $i = 0; while($row = mysql_fetch_assoc($query)){ if ($_POST['check'][$i]){ echo "You are about to delete " . $_POST['check'][$i]; } } $i++; } Does anybody have any ideas as to how I only get the message to display once for each row checked, rather than once for all rows. Many thanks
  3. Hi guys, Thanks but i managed to solve it shortly after posting. Like a muppet i forgot i was actually saving the URL of the image in the database, i simply recalled the url as the image SRC. Thanks.
  4. Hi everyone, Sorry not sure if this is a php or html problem. Im using php and a html form to upload images to my site, i have made it so that jpg, jpeg, gif and png images can be uploaded. The problem im having is displaying the image. <img src="images/<?php echo $name ?>.jpg" /> That works fine if a jpg was uploaded, but what if a png or a gif was uploaded? The $name is going to be unique, there will not be more than one image with the same name, so what do i have to do to display the image regardless of what the extension is? Thanks
  5. Hi mjdamato, I completely agree with you, I would love to keep my php and html totally separate! I have a right game trying to interpret it all. The only problem is i dont know how. For example how would I go about displaying a form text box for each row in a database table without using php within the html? Thanks
  6. Hi requinix, Yes I tried it lol. Its as if its not reading the query. To get it to work I actually have to place a 2nd query exactly the same within the same php tags as the while query. <?php $query = mysql_query("SELECT whatever FROM table1 WHERE id='1'"); while (mysql_fetch_assoc($query){ ?> Thanks
  7. Hi everyone, Is it possible to use a query in multiple sections of php that are separated by html? If you look at the example below, You can see my defined query at the top of the page, but I also want to use the same query for the while loop that has been put within the html to repeat the textbox. I know I could get around this by putting the html within php however I wanted to keep my html and php separate if possible. <?php $query = mysql_query("SELECT whatever FROM table1 WHERE id='1'"); // some php here ?> <html> <form> <?php while (mysql_fetch_assoc($query){ ?> <input type="text" /> <?php } ?> </form> </html> Thanks
  8. Hi everyone, In case anybody else comes across this post looking for the same thing, here is my solution. It only uses 1 query to update the database and after tests updating 2 columns with 100 rows each, it was found to be about 4 times faster than using a query within a while loop. $i = 0; while($row = mysql_fetch_assoc($query)){ $product_string .= " WHEN domain='" . $_POST['checkbox_value'][$i] . "' THEN '" . $_POST['product'][$i] . "'"; $price_string .= " WHEN domain='" . $_POST['checkbox_value'][$i] . "' THEN '" . $_POST['price'][$i] . "'"; $i++; } mysql_query("UPDATE table1 SET column1 = CASE" . $product_string . " ELSE column1 END, column2 = CASE" . $price_string . " ELSE column2 END"); I hope it helps.
  9. The code im using is similar to the following: Query within a while loop: while($row = mysql_fetch_assoc($query)){ mysql_query("UPDATE products SET price='$_POST['price'][$i] WHERE product='$_POST['checkbox_value']'"); $i++; } single query using case: while($row = mysql_fetch_assoc($query)){ $string .= "WHEN product='" . $_POST['checkbox_value'][$i] . "' THEN ' ". $_POST['price'][$i] . "'"; $i++; } mysql_query("UPDATE products SET price = CASE $string ELSE price END");
  10. Hi mate. Even though with a while loop a new query is executed for each row? but with case only one query is executed for all rows? A while is still more efficient and faster? Just trying to understand. Thanks mate.
  11. Hi everyone, Please could somebody give me their views on which is a more efficient and faster way to update multiple rows of a database with different values? Using a query within a while loop or using a query with a case? Thanks
  12. Hi everyone How would i put the values of a foreach into a single string? Lets say I have 2 $_POST['test'] values. In the example below the first echo will display something like "This is a TestThis is a Test", and the 2nd echo will display something like "Hello This is a Test". foreach ($_POST['test'] as $test){ $string = "This is a '" . $test . "'"; echo $string; } echo "Hello $string"; How would I get the 2nd echo to display something like "Hello This is a TestThis is a Test" for use outside of the foreach? Thanks
  13. Hi djlee, I was using the checkbox value as the unique key. Iv tried using your example however for the life of my I cant get it to work. Im only working with 2 products atm, it is inserting 2 new rows each time i submit the form, and it is updating the value in all of the previous rows apart from the original 2 that i am trying to update. Do you have any idea as to what i could be doing wrong? Thanks
  14. Hi AyKay47 Iv threw together a quick example of the form im using: www.directbullion.co.uk When a user changes the value in the text box, ticks the checkbox and clicks save changes, im trying to update the database using as few a queries as possible. If I use a while loop wouldn't multiple queries be submitted? Thanks
  15. Hi Guys, Sorry this is similar to the last threat I started, however I think I made it a little confusing. I use the code below to update rows in my database depending on which check-box is ticked. Basically id like to carry out the following but with the query outside of the while loop so that only 1 query is ran rather than a query for each check-box. $i = 0; while($row = mysql_fetch_assoc($query)){ $check = $_POST['checkbox_value'][$i]; $value1 = $_POST['value1'][$i]; mysql_query("UPDATE table1 SET field1='$value1' WHERE field2='$check'"); $i++; } Iv tried using mysql_query("UPDATE table1 SET field1='$value1' WHERE field2 IN ('$check')"); outside of the while loop however I don't know how to get it working with the arrays. Any help is greatly appreciated.
  16. Iv just read my initial post, I think it may be a little confusing. I wrote the initial example code out quickly and bodged it up. Im trying to achieve the following: if ($_POST['action'] == "Save Changes"){ mysql_query("UPDATE table1 SET field1='$value1' WHERE field2='$checkbox_value'"); }
  17. Hi mate, Thanks for the sql injection tip, iv been meaning to do a bit of research in how to make my site more secure . Iv took your example and now I have the code below, however it is writing a blank value to the database. $value1 = $_POST['value1'][$i]; $_POST['checkbox_values'] = array_map('mysql_real_escape_string', $_POST['checkbox_values']); if (empty($error)){ mysql_query("UPDATE table1 SET field1='$value1' WHERE field2 IN ('" . implode("','", $_POST['checkbox_value']) . "')"); } Do you know why this could be? Also, wouldnt I still have to have the while loop around the "$value1 = $_POST['value1'][$i];"? I tried this also and this was writing a 0 to the database. Thanks mate I appreciate your help
  18. Hi gristoi, Iv had a read of the IN function and used the example you have given me. I have modified it a bit to try and integrate it with my code however I cant get it to work. My latest code is: $i = 0; while($row = mysql_fetch_assoc($query)){ $value1 = $_POST['value1'][$i]; $exampleValues = $_POST['checkbox_value'][$i]; $i++; } if (empty($error)){ mysql_query("UPDATE table1 SET field1='$value1' WHERE field2 IN ($exampleValues)"); } Can you see where im going wrong? Thanks mate.
  19. Hi everyone, Im using code similar to that below to update or delete database entries based on checked checkboxes when a form is submitted. Would I be right in saying that, say if the user ticks 10 check-boxes and then clicks submit, 10 queries are executed? If so is there a way to make it so that only 1 query is executed but updates or deletes multiple entries? $i = 0; while ($row = mysql_fetch_assoc($query)) { // if checkbox is checked, validate data if ($_POST['check'][$i]){ // validation code here } // if no errors are present update or delete each checked entry if (empty($error)){ if ($_POST['action'] == "Save Changes"){ mysql_query("UPDATE example SET example='$example' WHERE value='$checked'"); } if ($_POST['action'] == "Delete"){ mysql_query("DELETE FROM example WHERE value='$checked'"); } } $i++; Thanks
  20. Hi mate, Thanks for the tip however that wasn't really what I was looking for. Im hoping to do it just by altering the code if its possible. I could have loads of users adding loads of domains all at the same time. I don't really want to be creating files on the server. Thanks anyway.
  21. Hi everyone. Iv touched on something similar to this before however having come back to it after a while i dont fully understand it. I have a website where people can add their domains. They add multiple domains in a text area. I have code that splits up each line of the text box and validates each domain etc...that is all working fine. Below is the code that I currently use to insert each domain into the database. If the user adds 100 domains, i take it that the code below will execute 100 queries to write each domain? Is it possible to execute only 1 query and add all the domains, each to a new row in the database? $not_exists = array_diff(array_map('trim', $lines), $exists); foreach ($not_exists as $domain){ mysql_query("INSERT INTO domains VALUES("'$domain'"); } Thanks
  22. Hi guys, I have a quick follow on question. Im now using the following code: $username_count = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) as count FROM users WHERE username='$username'")); $username_count['count'] > 0 ? $error[] = "Username already exists!<br />" : null; When the database is empty and i try to run the search im getting the following error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource How would i get around this? Thanks
  23. Sorry I typed it out quickly. <option value="value 1" <?php if ($_POST['dropdown'] == 'value 1') echo 'selected="selected"'; ?>>value 1</option> The php in the above line is basically saying "if the posted value is the same as the option value, display 'selected=selected' within the line of html". If an <option> tag reads <option selected="selcted"> then that dropdown value is displayed by default when the page loads. I hope you understand that. I get it in my head im just not sure if I put it across too good lol.
  24. Hi mate, I use something similar to the following: <select name="dropdown"> <option value="value 1" <?php if $_POST['dropdown'] == "value 1" echo "selected"; ?>>value 1</option> <option value="value 2" <?php if $_POST['dropdown'] == "value 2" echo "selected"; ?>>value 2</option> <option value="value 3" <?php if $_POST['dropdown'] == "value 3" echo "selected"; ?>>value 2</option> </select> Hope it helps.
×
×
  • 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.