Jump to content

ramone_johnny

Members
  • Posts

    50
  • Joined

  • Last visited

Everything posted by ramone_johnny

  1. Hi guys, I'm trying to pull a value from the database and assign it to a variable, of which I then need to populate another table. My background is ASP/MSSQL so I'm not overly familiar with how to do this. I have the following query, which is erroring out. I need help here. $r=query_wrapper("SELECT pcode_area, pcode_postcode, pcode_city FROM tblnew_cities WHERE pcode_area = ?",$share_suburb . " AND pcode_postcode = ?",share_postcode); $rstDBEdit=mysql_fetch_assoc($r); $a = array(); $a["pcode_city"] = $pcode_city; echo $pcode_city; exit(); Then of course once I have the $pcode_city I need to insert it into the tblshare_adverts table. (see below) $r=query_wrapper("SELECT * FROM tblshare_adverts"); $rstDBEdit=mysql_fetch_assoc($r); $a = array(); $a["share_number"] = $share_number; $a["share_street"] = $share_street; $a["share_suburb"] = $share_suburb; ....etc etc $a["share_city"] = $pcode_city; ...etc etc query_wrapper("INSERT INTO tblshare_adverts SET ?%",$a); $share_ID = mysql_insert_id(); I'm guessing this could be simplified by perhaps using an inner join and running just one query? Or is this way okay?
  2. Sorry, here's the query_wrapper stuff. // // sql_placehloder() + mysql_query(). // function query_wrapper() { global $linkScript; $args = func_get_args(); $tmpl = array_shift($args); if(is_resource($tmpl)) { $link=$tmpl; $tmpl = array_shift($args); } else $link=$linkScript; $query = sql_placeholder_ex($tmpl, $args, $error); if ($query === false) { $error = "Placeholder substitution error. Diagnostics: \"$error\""; if (function_exists("debug_backtrace")) { $bt = debug_backtrace(); $error .= " in ".@$bt[0]['file']." on line ".@$bt[0]['line']; } trigger_error($error, E_USER_ERROR); return false; } $r = mysql_query($query,$link); if (!$r) { echo "<hr>$query<hr>"; $error = "Mysql_query error: ".mysql_error(); if (function_exists("debug_backtrace")) { $bt = debug_backtrace(); $error .= " in ".@$bt[0]['file']." on line ".@$bt[0]['line']; } trigger_error($error, E_USER_ERROR); } return $r; } ?>
  3. Well when I run this, I get an error that indicates that its pulling those a$ values the second time round. My best guess was that I might need to change the second query to use something like b$, but that would be a complete guess.
  4. That's a good question - lol I *think* it prevents sql injection attacks.
  5. Sorry guys, I'm relatively new at PHP, and as I've never done this before, I need to ask. How do I correctly format the syntax below in order to insert to 2 separate tables, one after the other? $r=query_wrapper("SELECT * FROM tblmembers WHERE mem_email = ?",$mem_email); $rstDBEdit=mysql_fetch_assoc($r); if($rstDBEdit) redirect("/message.php?message=already_a_member"); $a["mem_name"] = $mem_name; $a["mem_password"] = $mem_password; $a["mem_email"] = $mem_email; .... query_wrapper("INSERT INTO tblmembers SET ?%",$a); $mem_ID = mysql_insert_id(); ---------------------------------------------------- $r=query_wrapper("SELECT * FROM tblshare_adverts"); $rstDBEdit=mysql_fetch_assoc($r); $a["share_available"] = $share_available; $a["share_header"] = $share_header; $a["share_description"] = $share_description; .... query_wrapper("INSERT INTO tblshare_adverts SET ?%",$a); $share_ID = mysql_insert_id();
  6. I have the following snippet of code that I *think* is right, but being new to PHP I just wanted if someone could let me know if this is indeed correct. I'm simply redirecting the user if they are already a member (email exists in the db) $r=query_wrapper("SELECT * FROM tblmembers WHERE mem_email = ?",$mem_email); $rstDBEdit=mysql_fetch_assoc($r); if($rstDBEdit) redirect("/message.php?message=already_a_member");
  7. Well this might be a separate question, but I'm not sure I want null values for any of these request variables. Infact, I know I dont. Certainly not in the DB. Is there a better way to write this? $_SESSION['start'] = isset($_REQUEST["start"]) ? $_REQUEST["start"] : ""; Can I strip that stuff off the end and make sure isset($_REQUEST["start"]) actually holds a value?
  8. Yeah I don't really understand what : ""; actually means on the end there. Sorry, working with someone elses code. Is that simply setting a base (default) value if nothing is passed?
  9. Sorry I just realised I was killing the session and then resetting it. My apologies. *doh! I'm not quite sure where I set $_SESSION[''start'] to ''? I'm debugging it to screen and it's showing 1 based upon the previous page. <input type="hidden" name="start" value="1" id="start" />
  10. Hey guys, I have the following snippet of code that I'm *trying* to use in order to redirect users based upon a session time out. As I've relatively new to PHP and never written a redirect before, I just wanted to check that it's okay. $_SESSION['start'] = isset($_REQUEST["start"]) ? $_REQUEST["start"] : ""; if (!isset($_SESSION['start'])) { header('Location:http://localhost/advertise/placeanad.php'); session_destroy(); exit(); } The page doesn't redirect, it just sits there - even upon refreshing it. Is there something I'm missing? Do I need to use an !empty statement or something?
  11. Ahh, perfect! Thanks dude, appreciate it.
  12. Thanks man. Sorry, I'm not intending to be lazy - I'm just completely new at PHP. Here's what i ended up with. It seems to work, but I'd say it could be written more efficiently. <?php $prefage_options = ''; $selected_value = false; if (!isset($_SESSION['share_prefage']) || !ctype_digit($_SESSION['share_prefage'])) { // no session value or session value is text $prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n"; } else { $selected_value = $_SESSION['share_prefage']; } for ($i=18; $i<=99; $i++) { $selected = ($i == $selected_value) ? ' selected="selected"' : ''; $prefage_options .= "<option value='{$i}'{$selected}>Around {$i}</option>\n"; } ?> <select name="share_prefage" class="textarea" style="width:150px"> <?php if (isset($_SESSION['share_prefage']) AND ctype_digit($_SESSION['share_prefage'])) { //Session value is number. Set selected value $prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n"; } echo $prefage_options; ?> </select> Here's what I changed/added. <?php if (isset($_SESSION['share_prefage']) AND ctype_digit($_SESSION['share_prefage'])) { //Session value is number. Set selected value $prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n"; } echo $prefage_options; ?>
  13. The error is .... Warning: in_array() expects parameter 2 to be array, string given in C:\xampp\htdocs\advertise\placeanad.php on line 336 Which is... $checked = (in_array($r['cat_ID'], $selectedCategories)) ? ' checked' : ''; Because $_SESSION['share_category'] has no value.
  14. Thank you. Hmm, that looks close, but I lose the "Doesnt Matter" when I make a selection (an age) then go back. I need to keep it there as said, incase they change their mind.
  15. Hey guys, I just wanted to follow up on this with one more question. It seems this code falls over if $_SESSION['share_category'] is not set, as the following variable gets its value from it. $selectedCategories = isset($_SESSION['share_category']) ? $_SESSION['share_category'] : array(); Here's the code in it's entirety. Could someone chime in with how to prevent this from falling over if $_SESSION['share_category'] is not set? Thanks. <? echo "<table width='100%' border='0' cellpadding='0' cellspacing='0' class='body_text' style='padding-left:20px; padding-right:15px;'>"; $strSQL = mysql_query("SELECT * FROM tblcategories ORDER BY cat_ID ASC"); //Set array of selected categories $selectedCategories = isset($_SESSION['share_category']) ? $_SESSION['share_category'] : array(); // [x] photos per row, change this to the number of photos/images you want per row $images_per_row = 4; for($x = 0; $r = mysql_fetch_array($strSQL); $x++){ if($x % $images_per_row == 0){ echo "<tr>"; } //customize this to the variables you want to grab $cat_category = $r["cat_category"]; $cat_ID = $r["cat_ID"]; //customize to display image $checked = (in_array($r['cat_ID'], $selectedCategories)) ? ' checked' : ''; //echo "this is what checked equals : " . $checked; echo "<td><input type='checkbox' name='share_category[]' {$checked} value='{$r['cat_ID']}'>{$r['cat_category']}<td>"; //Here's the key part, this adds a new row after the [x]result/image from the database is echo'd if ($x % $images_per_row == ($images_per_row)-1) { echo "</tr>"; } } if($x != mysql_num_rows($strSQL)){ echo "</tr>"; } echo "</table>"; ?>
  16. Hey guys, This is probably a complete no brainer, but I'm stuck with it. Here's the code I currently have. <select name="share_prefage" class="textarea" style="width:150px"> <? if (!isset($_SESSION['share_prefage'])) echo "<option value='Doesnt Matter'>Doesnt Matter</option>"; else { echo "<option selected value=" . $_SESSION['share_prefage'] . ">Around " . $_SESSION['share_prefage'] . "</option>"; } { for ($i=18; $i<=99; $i++) echo "<option value='$i'>Around $i</option>"; } ?> </select> Put simply here's what I'm trying to accomplish 1. If the session variable has not been set, set the default value to "Doesnt Matter. 2. If the session variable has been set, write it within the drop down BUT... if it equals "Doesnt Matter" then don't write Doesnt Matter twice, and dont write "Around Doesnt Matter" 3. Keep the loop at the end regardless.
  17. Got it! There was a slight issue with this bit of code which I changed up. $checked = (in_array($r['cat_ID'], $selectedCategories)) ? ' checked' : ''; //echo "this is what checked equals : " . $checked; echo "<td><input type='checkbox' name='share_category[]' {$checked} value='{$r['cat_ID']}'>{$r['cat_category']}<td>"; Creating the array like so. $_SESSION['share_category'] = isset($_REQUEST["share_category"]) ? $_REQUEST["share_category"] : ""; Any issues you guys can see with this?
  18. You're right. I realised that issue when I started getting "string" related errors. Ive taken that out. I now have... $_SESSION['share_category'] = isset($_REQUEST["share_category"]) ? $_REQUEST["share_category"] : ""; ...where do I use this? $array_name[] = $value;
  19. Okay, I *think* I'm getting closer with this. Here's the session variable that contains the array. $_SESSION['share_category'] = isset($_REQUEST["share_category"]) ? $_REQUEST["share_category"] : ""; And here's the code that I was helped with earlier. <? echo "<table width='100%' border='0' cellpadding='0' cellspacing='0' class='body_text' style='padding-left:20px; padding-right:15px;'>"; $strSQL = mysql_query("SELECT * FROM tblcategories ORDER BY cat_ID ASC"); //Set array of selected categories $selectedCategories = isset($_SESSION['share_category']) ? $_SESSION['share_category'] : array(); // [x] photos per row, change this to the number of photos/images you want per row $images_per_row = 4; for($x = 0; $r = mysql_fetch_array($strSQL); $x++){ if($x % $images_per_row == 0){ echo "<tr>"; } //customize this to the variables you want to grab $cat_category = $r["cat_category"]; $cat_ID = $r["cat_ID"]; //customize to display image $checked = (in_array($r['cat_ID'], $selectedCategories)) ? ' checked="checked"' : ''; echo "<td><input type='checkbox' name='share_category[]' value='{$r['cat_ID']}'>{$r['cat_category']}{$checked}<td>"; //Here's the key part, this adds a new row after the [x]result/image from the database is echo'd if ($x % $images_per_row == ($images_per_row)-1) { echo "</tr>"; } } if($x != mysql_num_rows($strSQL)){ echo "</tr>"; } echo "</table>"; ?> Line 330 is giving me an error....? Warning: in_array() expects parameter 2 to be array, string given in C:\xampp\htdocs\advertise\placeanad.php on line 330 Which is this line.... $checked = (in_array($r['cat_ID'], $selectedCategories)) ? ' checked="checked"' : ''; Any thoughts?
  20. Ah, okay, I *think* have created the array correctly? $share_category = implode(', ', $_POST['share_category']);
  21. Okay, getting closer. The first issue is constructing the array. I have this bit of code, but it's not appending anything after the first value? $checked = $_POST['share_category']; for($i=0; $i < count($checked); $i++){ $_SESSION['share_category'] = $checked[$i] . ", "; } echo $_SESSION['share_category'] Then I guess I'll need to do something like this on the other page. $selectedCategories = isset($_SESSION['share_category']) ? $_SESSION['share_category'] : array();
×
×
  • 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.