PHP - Basic Problem With Isset
Hi, I'm very new to PHP so I hope this is not frustrating. I'm having trouble understanding how "isset" works. I'm trying to get it so that when I click a button, it returns two variables(in an array). This works, but I tried to use "Isset" to detect if a array was created, then alter the variable. It's not working... I'm not too sure where I'm going wrong, could anyone give me some advice? Thanks
<?php if(isset($_POST['name'])) { $userAnswer = $_POST['name']; if(isset($_SESSION['test'])){ $_SESSION['test'][$userAnswer]['var1']++; $test = $_SESSION['test'][$userAnswer]; foreach ($test as $key){ echo json_encode($key); }; } else { $test = $_SESSION['test'][$userAnswer]=array("var1" => 1, "var2" => 2); foreach ($test as $key){ echo json_encode($key); }; } } ?> $(document).ready(function(){ $("button").click(function(){ var test = "here"; $.ajax({ url: 'info.php', type: "POST", dataType:'json', data: ({name: test}), success: function(data){ $("#click").fadeOut(function() { $(this).append(data); }).fadeIn(); } }); }); }); Similar TutorialsI'm having a problem where when I click the "use" link it only displays my check message which is "Hey", but for some reason I have to click it again for my queries to work. Here's a pick: . Here's the code: Code: [Select] <?php if(isset($_SESSION['user'])){ include_once('connect.php'); $session = $_SESSION['user']; echo " <br/> <div class='profile-general-stats'> <table> <th>Item Name</th><th>Description</th><th>Use/Sell</th> "; $yo = "SELECT * FROM player_items WHERE Username = '$session' ORDER BY id ASC"; $check = mysql_query($yo) or die (mysql_error()); $num_of = mysql_num_rows($check); if($num_of > 0){ while ($is = mysql_fetch_array($check)){ $item_id = $is['id']; $item_name = $is['Name']; $item_type = $is['Type']; $item_value = $is['Value']; $item_description = $is['Description']; $item_sell_value = $is['Sell_Price']; echo " <tr> <td align='left' style='padding: 5px; padding-left: 25px;'><strong>$item_name</strong></td> <td align='left' style='padding: 5px;'>$item_description</td> <td align='middle' style='padding: 5px;'><a href='?use&id=$item_id&name=$item_name&type=$item_type&value=$item_value'>Use</a> | <a href='#sell_$item_name' id='$item_id'>Sell</a></td> </tr> "; } }else{ echo " <tr> <td align='left' style='padding: 5px; padding-left: 25px;'>No Items</td> <td align='left' style='padding: 5px;'>No Items</td> <td align='middle' style='padding: 5px;'>No Items</td> </tr> "; } echo " </table> </div> "; if(isset($_GET['use'])){ echo "Hey"; $use_id = $_GET['id']; $use_name = $_GET['name']; $use_type = $_GET['type']; $use_value = $_GET['value']; if($use_type == "Credits"){ $use_type = "Current_Credits"; } $check_item = "SELECT id FROM player_items WHERE id = '".mysql_real_escape_string($use_id)."' AND Username = '".$_SESSION['user']."'"; $run_check = mysql_query($check_item) or die(mysql_error()); $num_check = mysql_num_rows($run_check); $e = mysql_fetch_array($run_check); $item_stat = "SELECT `".$use_type."` FROM stats WHERE Username = '".$_SESSION['user']."'"; $run_stat = mysql_fetch_array(mysql_query($item_stat)) or die (mysql_error()); $new_stat = $run_stat[''.$use_type.'']; if($num_check == 1){ $new_stat = $new_stat + $use_value; mysql_query("UPDATE stats SET `".$use_type."` = '".mysql_real_escape_string($new_stat)."' WHERE Username = '".mysql_real_escape_string($_SESSION['user'])."'") or die(mysql_error()); mysql_query("DELETE FROM player_items WHERE id = '$use_id'") or die(mysql_error()); }else{ } }else{ } }else{ header("Location: index.php"); } ?> Hey, I've got a site and I need to query a database to check if there is a result. If there isnt then I need a default result... //Check the database and get a result $skinName = strtolower(get_post_meta($post_id, $key, $single)); if (isset($skinName)) { //If there is a result, then use it $skinName = strtolower(get_post_meta($post_id, $key, $single)); } else { //If NO result then use RED $skinName = 'red'; } //Build up the var to show REDsite $skinName = $skinName . 'site'; For some reason the above doesnt work. If there is a result then it uses it, however if there is no result then it simply shows "site" - not "redsite" like it should do. Any ideas what I missed? Hi guys I am trying to create a page where if a selection has not been made then a jump menu is displayed listing all of the fixtures for a season, if a selection has been made then at present I want it to show Enter match info, however when a match is selectd it goes to a yahoo search page, as if the page cannot be found, the code I have used is below, any ideas what I have done wrong? The page can be seen at www.yardleyheroes.co.uk/admin/edit_result.php Code: [Select] <h1>Submit Results</h1> <?php if (isset ($_GET['match_id'])) { echo('Enter match info'); } else { $con = mysql_connect('localhost', 'user', 'password', true); if(!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('dbname', $con); echo('<form name="form" id="form">'); echo('<select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu(\'parent\',this,0)">'); $fixtures=mysql_query('SELECT * FROM `fixtures` where current=1 ORDER BY `match_date`'); if(mysql_num_rows($fixtures)==0) echo('<option>there is no data in table..</option>'); else echo('<option selected="selected">Please select the match</option>'); { while($matchrow=mysql_fetch_assoc($fixtures)) { echo ('<option value="edit.php?match_id='.$matchrow['match_id'].'">'.date("d-M-Y", strtotime($matchrow['match_date'])).'|'.$matchrow['Opposition']."</option>"); } } echo('</select>'); echo('</form>'); } ?> [/cod] Hi guys, first time posting here. Anyway, heres my problem. I have this section of code at the beggining of my page: Code: [Select] if (isset($_POST['userChoice'])) { $try = check_word($_POST['userChoice']); echo ("We are into the loop"); } else { echo ("<br/>about to call change word"); changeword(); echo ("<br/> we called the changeword"); } And here is my form: Code: [Select] <html> <head> </head> <body> <form name="wordguess" action="Index.php" method="POST"> <table> <tbody> <tr> Try to guess what the following word is: <?=$scrambled ?> <br/> </tr> <tr> Your Guess: <input type="text" id="userChoice" size="20" /> <br/> </tr> <tr> <td> <input type="submit" name="submit" id="submit" value="Submit"> </td> </tr> <tr> <?=$correct?> </tr> </tbody> </table> </form> </body> </html>' Now, how i assumed it worked was that the first time the page is loaded it would display the form and that's it. However, even when they hit the submit button on the form, its not really doing the POST, so when the page reloads, gives me the same stuff so it always goes on the else part of the if. Any help would be appreciated. Thanks, GoodVibe I am an absolute beginner in php & am trying to validate a form using javascript. Problem is I am checking if the textbox is left empty before the user clicks on the submit button(using javascript). On the other hand i'm using isset function of php for the same submit button so that it does not run the code unless and until the button is clicked. Due to this, the default value of text box is being entered into database. How do i prevent this? Is there an alternate function to isset for such an event? Here is a sample code Code: [Select] <?php if(isset($_POST[sub])) $sql="insert into testing values('$_POST[txt1]')"; $exec = mysql_query($sql); ?> <script type="text/javascript"> function check() { var a = document.getElementById('add'); if (a.value = "") { return false; } } </script> <form action="add.php" method="post"> <input type="text" name="txt1" id="add"><br> <input type="submit" name="sub" value="Click to Add" onclick = "check();"> </form> I'm using ghost text technique on my page instead of labels so the text boxes have different values at different instances and hence I cant use !empty function either. How do i unset the submit button so that it does not run the script further? I have a form that is supposed to use PHP to display errors next to incomplete form fields when the form is submitted. But I am never getting into my PHP loop. I tried using the print_r(true) function to debug, but no luck. When I click submit I see this i the URL... http://localhost/00_MyWebSite/zzEmptyPHP.php?firstName=&lastName=&submit=Process+Order&submitted=true which is what I would expect on a blank form being submitted. Here is my code... Code: [Select] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href=".css"> <style type="text/css" > form{ width: 400px; margin: 0 auto; } </style> </head> <body> <?php // Initialize. $errors = array(); $errors['firstName'] = ''; $errors['lastName'] = ''; $firstName = $lastName = ''; echo "print_r = " . print_r(true); if (isset($_POST['submit'])){ // Handle Form. // Trim all incoming data. $trimmed = array_map('trim', $_POST); // Check First Name. if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['firstName'])){ $firstName = $_POST['firstNamae']; }else{ $errors['firstName'] = 'Please enter your First Name.'; } // Check Last Name. if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['lastName'])){ $lastName = $_POST['lastName']; }else{ $errors['lastName'] = 'Please enter your Last Name.'; } // if there are errors then go back to the form and display them }else{ } ?> <form action=""> <fieldset> <legend>Billing Details</legend> <ol> <li> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" class="text" type="text" /> <?php echo $errors['firstName']; ?> </li> <li> <label for="lastName">Last Name:</label> <input id="lastName" name="lastName" class="text" type="text" /> <?php echo $errors['lastName']; ?> </li> </ol> <input type="submit" name="submit" value="Process Order" /> <input type="hidden" name="submitted" value="true" /> </fieldset> </form> </body> </html> Any ideas what I am doing wrong? Thanks, Debbie I am having problems with my $_GET. When a user chooses an Article from "article_index.php", they click on a hyperlink, e.g. Code: [Select] <a href="<?php echo BASE_URL; ?>articles/postage-meters-can-save-you-money"> This triggers my Mod_Rewrite... Code: [Select] RewriteEngine on #PRETTY: articles/postage-meters-can-save-you-money #UGLY: article.php?slug=postage-meters-can-save-you-money RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?slug=$1 Which should take them to "article.php?slug=postage-meters-can-save-you-money" and then this code is supposed to grab the "slug" and retrieve the Article... Code: [Select] // Attempt to Retrieve Article. if (isset($_GET['slug'])){ // Slug found in URL. // Set Slug. $slug = $_GET['slug']; That part is working fine, however, if I go into the Address Bar and delete out the slug, so my URL looks like this... Code: [Select] www.MySite.com/articles/"> I would expect the ELSE part of the above code to kick in... Code: [Select] }else{ // Slug not found in URL. header("Location: " . BASE_URL . "index.php"); }//End of ATTEMPT TO RETRIEVE ARTICLE Unfortunately I am just getting this error... Quote Not Found The requested URL /articles/ was not found on this server. Apache/2.0.59 (Unix) PHP/5.2.6 DAV/2 Server at local.debbie Port 80 Why is my ELSE not detecting that there is no slug?? Debbie When the form is summited it is supposed to check if the password field contained any text so that way it updates the password. so only if $password exists should it run that query. I tried all different ways of making that happen and it never works. I will summit the form without anything in the password field and it will still update the password to something encrypted in md5 anyways. Code: [Select] <?php include ('header.php'); if(isset($_GET['sa'])) { switch ( $_GET['sa']) { case 'edit': edit(); break; } }else{ echo "<div class='header'>Edit admins</div><div class='content'>"; $query = mysql_query("SELECT * FROM `admins`"); while($row = mysql_fetch_array($query)){ $username = clean_up($row['username']); $id = clean_up($row['id']); $email = clean_up($row['email']); $website = clean_up($row['website']); echo "$id $username $email $website | <a href='index.php?action=editadmins&sa=edit&id=$id'> Edit</a><br>"; } echo "</div>"; } function edit() { $id = clean_up($_GET['id']); if(isset($_POST['submit'])){ $username = clean_up($_POST['username']); $password = md5(clean_up($_POST['password'])); $email = clean_up($_POST['email']); $website = clean_up($_POST['website']); if(isset($username)){ if(isset($password)){ mysql_query("UPDATE `admins` SET `password`='$password' WHERE `id`='$id'"); } mysql_query("UPDATE `admins` SET `username`='$username', `email`='$email', `website`='$website' WHERE `id`='$id'"); echo "<b><font color=green>Information edited. Go <a href='index.php'>home</a>?</font></b><br>"; }else{ echo "please enter a username"; } }else{ $query = mysql_query("SELECT * FROM `admins` WHERE `id`='$id'"); while($row = mysql_fetch_array($query)){ $username = clean_up($row['username']); $email = clean_up($row['email']); $website = clean_up($row['website']); echo "<div class='header'>Edit $username</div><div class='content'><form action='index.php?action=editadmins&sa=edit&id=$id' method='post'> <b>Username:</b> <input type='text' name='username' id='username' value='$username'><br> <b>Password:</b>(leave blank to keep same) <input type='password' name='password' id='password' value=''><br> <b>Email:</b> <input type='text' name='email' id='email' value='$email'><br> <b>Website:</b> <input type='text' name='website' id='website' value='$website'><br> <input type='submit' name='submit' id=submit' value='Edit $username'></form></div>"; } } } include('footer.php'); ?> I am trying to do isset($_GET but I have a bizarre problem come up. I am using "id" as one of my fields but when I try to use it in the code it says it is not recognised. However I use this field quite widely. The code its coming up with is "Undefined variable: id" but I cant see why it has a problem with id. My code is: <?php if(isset($_GET['id'])){ $sql = mysql_query("SELECT * FROM productfeed WHERE id='$id' LIMIT 1"); } { while($row = mysql_fetch_array($sql)) $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo "<div class=\"productdisplayshell\"> <div class=\"productdisplayoutline\"> <div class=\"productborder\"><center> <a href=\"$link\" target=\"_blank\" ><img src=\"$image\" /></a> </center> </div></div> <div class=\"productdescriptionoutline\"><div class=\"productdescriptionbox\"> <a href=\"$link\" target=\"_blank\" >$description</a> </div><div class=\"productfulldescriptionbox\"> $fulldescription </div></div> <div class=\"productpriceoutline\"> <div class=\"productpricebox\"><center>&#163; $price</center></div> <div class=\"productbuybutton\"><center><a href=\"$link\" target=\"_blank\" ><img src=/images/buybutton.png /></a></center></div></div></div>"; } ?> http://i.imgur.com/6olCy.png The picture (in the imgur link) pretty much describes what I'm trying to do. As it stands, it would take too long to simply do everything manually with html and css (both of which I'm pretty good at I think), moving all img src and dates from one div tag to another whenever I wanna add something. So yeah, what's the most efficient way to simply submit a picture, and all my other pictures will move? I know some of you guys will say to just learn php, but quite frankly, I don't have the time... and all the php books are ~1000pgs long which seems really daunting. I know, my problem's pretty basic and it'll probably be covered in the first 100pages or so, but still, like I said I don't have that much time for the moment. When I do, however, can you guys recommend me the quickest guide/video series/book to learn practical php? Not the most comprehensive, as the stuff I'm going to be doing's not that complex. One last thing, I've been hearing about CakePHP; what is it? I read that it's an "open source web application framework", but I still have little clue. I'm currently using dreamweaver for all my web stuff (no I do not use the WYSIWYG feature, I just grown accustom to using it). Should I "learn cakephp"? Please answer any one of those questions (particularly the first one). Many thanks and I'll be sure to reply! Hello there. Well this problem maybe basic but is kind of annoying me. I havea solution to it which I wil lexplain. I have created a .php document which has html codes as well. It is a form that provides validation, for arguments sake lets say it is a form that validates if a user has entered their username and password, if not it will produce an error. The problem I am having is I have embedded the code inside the echo " code is here "; Now I have looked at escape characters and tried afew things, yet it still produces the same error on the same line. Example echo " !!!!!!!<form action='book.php' method='post' onsubmit="return checkForm('flogin');">!!!!!! The reset of the form is here ..... ""; echo The area highlighted in exclamation marks is where the area occurs, due to the quotes. Any suggestions. Thanks. I have a table 'combo' with two fields, id and char.
I want to populate a table, with the following query which fails.
$sql = mysql_query("SELECT id,char FROM combo ORDER BY id DESC LIMIT 28");
I a bit rusty on sql but can't see what is wrong with this.
Can anyone help please.
Ok first of all I want to apologize for asking what I'm about to ask. I understand it should be some stupidly easy to find mistake but considering the fact I have to send the code tomorrow leaves me with little to no time to try and find the solution so I'll be asking here and hope that by the time I wake up tomorrow an answer will be here and I'll be able to resume scripting. So my problem is that I just can't find how to make php commands run at all. I tried just randomly inserting them into my html code then found out it needs a separate file. So I created the basic.php with this code: <html> <body> <?php echo "Hello World"; ?> <div>adgaeganerhaohn</div> </body> </html> tried opening it through a form in my html code but it wouldn't print the Hello world line, then I tried running it straight into firefox but I only got adgaeganerhaohn again (without that line I get a white page) So I need to know what am I doing wrong? I have php5.301 installed with default options (no server). thanks for your help beforehand, once I get a bit more into php I'll be coming back with serious questions but for now I really need someone to tell me how php is supposed to work and what I'm doing wrong Hi, I have just started using PHP again after a long break and am very rusty. I am having a few problems with some basic things: I have defined a function to connect to my MySQL database and it doesn't work. My $hostname, $username, $password variables are stored in a separate php file which I am including first and the standard mysql_connect function works ok, but I wanted to put it inside another user defined function called "condb" to make things quicker later. my function looks like this: function condb(){ mysql_connect($hostname, $username, $password) OR DIE ('Unable to connect to database! Please try again later.'); } and I am trying to run it like this: condb(); Unfortunately it is not working. Probably a basic error but any help appreciated. Thanks Isn't this code redundant... if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == TRUE){ After all, if $SESSION['logged_in'] == TRUE then it is *obviously* set... Debbie Hi there, I'm inserting data into a mysql database. My submissions works fine, however when a part of the form is not filled out the php scripit crashes. How can I submit all data that is set and skip pieces that aren't filled out? For example I have a plant database which has an option to insert different types of common names for the plant. It's ok if they aren't all filled out. My code is as follows: Code: [Select] <?php //insert general plant data into db $queryplant="INSERT INTO actualplanttable (id, scientific, common1, common2, common3, family, genus, description, planting, growing, care, duration, reference) VALUES ('NULL', '$_POST[scientific]', '$_POST[common1]', '$_POST[common2]', '$_POST[common3]', '$_POST[family]', '$_POST[genus]', '$_POST[description]', '$_POST[planting]', '$_POST[growing]', '$_POST[care]','$_POST[duration]','$_POST[reference]')"; mysql_query($queryplant) or die ('Error updating actualplanttable'); //insert maintenance data into db $querymaintenance="INSERT INTO plantmaintenancelinktable (plant_id, maintenance_id) VALUES ('$inserted_id', '$_POST[maintenance]')"; mysql_query($querymaintenance) or die ('Error updating plantdurationtable'); ?> Should I be trying to work an isset function into this code? Any help would be greatly appreciated. Thanks! Bill... guys this thing called isset() is bugging me.What is the difference in when you say Code: [Select] if(isset($_POST['name'])) and Code: [Select] if($_POST['name']) and Code: [Select] if(!$_POST['name']) and why would someone require a code such as Code: [Select] if($_POST['submit']=='Submit') And what is that code which tells you the user has entered something into the form Thanks People why dose this not work <?php if(isset($_SESSION['SESS_ADMIN'])) $loc = ../admin/admin.php; else $loc= ../head/head.php"; ?> <form><INPUT TYPE="button" value="Main Menu" onClick="parent.location='<?php echo $loc; ?>'"></form> </body> </HTML> Hello all, i am hoping someone can help me. i have some code i have been working on and i am trying to add a 404 page if it cant find the page, i know it will be some sort of if statment, but with me been pretty much a noob at php i am stuck. heres the code that the page calls. Code: [Select] function get_content() { //Get page name from browser if none call it home $page = (isset($_GET['page'])) ? $_GET['page'] : 'home'; //Get The Page Content $sql = "SELECT * FROM content WHERE page = '$page'"; $res = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_assoc($res)){ //Show The Page Content echo '<h1>' . $row['title'] . '</h1>'; echo $row['body']; } } thanks all. |