Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    isset issues learning PHP

    Greetings all!

    This is my first post, and I resort to posting only having exhausted searching the internet for answers. I am using ( a 3 ry old) Cartoonsmart video (lent to me by a friend) to learn PHP for MySQL.

    I got through the first tutorial and am having a problem with the code, as many people are according to my internet search. There are no video or code updates to the tutorial.

    I have narrowed the problem to the ISSET function from my searches, have tried the many recommended fixes, but not have found a definitive solution.

    I even tried changing my ISSET function to:

    Code:
    if(isset($_POST['addData']) && $_GET['function']=='add')
    So far, I can NOT get the PHP to insert the form data into the database. Passing the data from the form does NOT work, but (hard) coding the data to insert works:

    Code:
    if(isset($function) && $function=="add"){
    		$sqlInsert = 	"INSERT INTO `statusValues` (
    						`firstName`, 
    						`lastName`, 
    						`region`, 
    						`SS#`) 
    						VALUES ( 'Bob', 'Smith', 2, 123121234 )";
    I have a hosting account (Linux, PHP5, MySQL) and have been testing it on that. I am half way to a web development degree and have not learned PHP yet, hence teaching my self.

    Any help is appreciated, thanks in advance. Here is the code that is not working:


    Code:
    <?php
    
    	include("connections/mySQL.phpConnectReal.php");
    	
    	mysql_select_db("$database_TestConnect") or die ("Unable to Connect to the database");
    	
    	$table = "statusValues";
    	if( mysql_num_rows( mysql_query("SHOW TABLES LIKE '".$table."'")))
    	{
    		
    	}else{
    		print("Table was not found, I will create it now.<br>");
    		
    		$sql = 	"CREATE TABLE `statusValues` (
    				`statVal_id` INT NOT NULL AUTO_INCREMENT ,
    				`FirstName` VARCHAR( 64 ) NOT NULL ,
    				`LastName` VARCHAR( 64 ) NOT NULL ,
    				`Region` INT NOT NULL ,
    				`SS#` INT NOT NULL,
    				INDEX ( `statVal_id` ) )"; 
    
    		mysql_query($sql) or die(mysql_error()) ;	
    		print("Table Creation Complete. . .<br>");
    	}
    	
    	
    	$function=$_GET['function'];
    	
    	if(isset($function) && $function=="add"){
    		$sqlInsert = 	"INSERT INTO  `statusvalues` (
    						`FirstName`, 
    						`LastName`, 
    						`Region`, 
    						`SS#`) 
    						VALUES ('$name', '$lastName', $regionNumber, $socialNumber)";
    
    		//print("$sqlInsert<br>");
    		mysql_query($sqlInsert) or die(mysql_error());
    		//print ("Date Enter Ok :D");
    		$function="";
    		echo'<script type="text/javascript" language="javascript">alert("Data Added.");location.replace("index.php");</script>';
    			}
    	
    	$regSQL = "SELECT `regionName`,`regionNumber` FROM ragions";
    	$resultQuery = mysql_query($regSQL) or die(mysql_error()) ;
    	
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>PHP/MySQL Programming Course Part 2</title>
    </head>
    
    <body>
    
    
    
    <form id="enterForm" name="dateEnter" method="post" action="index.php?function=add">
      Enter First Name
        <input type="text" name="name" id="name" />
        <br />
    Enter Last Name
    <input type="text" name="lastName" id="lastName" />
    <br />
    Select Region Number
    <select name="regionNumber" id="regionNumber">
      <?php while ( $myrow = mysql_fetch_array($resultQuery) ) { ?>
      
      	<option value="<?php echo $myrow['regionNumber']; ?>"><?php echo $myrow['regionNumber'] ."-". $myrow['regionName']; ?></option>
      
      <?php } ?>	
    </select>
    <br />
    Please Enter SS# No Dashes 
    <input type="text" name="socialNumber" id="socialNumber" />
    <br />
    <input type="submit" name="addData" id="addData" value="Ok" />
    </form>
    <?php
    
    	$selectQuery = "SELECT  * FROM statusvalues
    							 WHERE 1
    							 ORDER BY LastName ASC";
    	$resultName = mysql_query($selectQuery) or die(mysql_error());
    
    ?>
    <table width="400" border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td>id#</td>
        <td>First Name</td>
        <td>Last Name</td>
        <td align="center">Region</td>
        <td>&nbsp;</td>
      </tr>
    
    <?php while ($myrowRes = mysql_fetch_array($resultName)) { ?>
    
      <tr><p></p>
        <td><?php echo $myrowRes['statVal_id']; ?></td>
        <td><?php echo $myrowRes['FirstName']; ?></td>
        <td><?php echo $myrowRes['LastName']; ?></td>
        <td align="center"><?php echo $myrowRes['Region']; ?></td>
        <td>&nbsp;</td>
      </tr>
    
    <?php } ?>
    </table>
    
    </body>
    </html>
    Cheers,

    Ric

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Ric,

    This:
    if(isset($_POST['addData']) && $_GET['function']=='add')

    Is just wrong.

    It should be:

    if ((isset($_POST['addData'])) and ($_GET['function'] == 'add'))

    You were close, but you had use the $_GET[] == 'add' inside the isset function - you can't do that. It is something that must be done outside of the isset function but within your if().

    Note that you can use 'and' instead of && and or instead of ||.

    Also, tizag.com is very good for tutorials.

    Edit:
    I must be going blind. As pointed out by Kaleth, there is actually nothing wrong with the isset() posted by the op. That'll teach me or posting when I have flu and not reading things properly..
    Last edited by tangoforce; 07-12-2012 at 07:09 PM.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Uh, tango, I don't see anything wrong with his if statement... with all due respect, does anyone else?

    As far as I know, && vs AND is just a case of state and precedence (AND being a lower precedence then &&). In addition, he had closed the isset, so I don't see ANY issue with that.

    Rather, I notice you didn't define any of the values in your MySQL statement ($name, $lastName, $regionNumber, $socialNumber). If you echo/print the statement (I see its commented out just beneath the definition), does it show up properly?

    If you're using that old of a tutorial, I recommend stopping immediately, because you're using very out dated information. You are likely relying on directives and settings that have long been deprecated for good reason (like register globals).

    In general, and I don't mean this as an insult because I understand you're learning, so take it at face value, your code simply looks old/outdated. Print is not a commonly used function in my experience (though perfectly valid), and if you're just getting into PHP and MySQL, I recommend looking at PDO as soon as you're comfortable with classes (maybe even before, as it might be a good way to learn more about classes). I know its a bit of a "jump into the deep end" method of learning, so if you're not comfortable with it, don't do it, but I do strongly recommend getting a more up to date book/video series to learn.

  • #4
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Keleth,

    When I echo/print the statement, this is what I get:

    INSERT INTO `statusvalues` ( `FirstName`, `LastName`, `Region`, `SS#`) VALUES ('', '', , )
    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 ' )' at line 6
    BTW, I got the "near line 6" error many times when I tried different solutions. Line 6 itself is blank.

    Ric

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Saw something I missed. Changed
    "INSERT INTO `statusvalues`
    to
    "INSERT INTO `statusValues`
    (capital V in values).

    Still get this error:

    INSERT INTO `statusValues` ( `FirstName`, `LastName`, `Region`, `SS#`) VALUES ('', '', , )
    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 ' )' at line 6
    Ric

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Reread my post The variables you're putting into the statement aren't being set anywhere, so of course they'll come out blank. I assume the guide simple used the names of POST/GET variables as variables? If so, they're using the deprecated register_globals, which is a terrible setting that was removed a while back thankfully. Thus my statement that you should get a more up to date guide.

    You need to actually set a variable if you're going to use it.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by Keleth View Post
    Uh, tango, I don't see anything wrong with his if statement...
    And looking at it again, neither do I

    I could swear that was wrong earlier but looking at it again I see you're totally right - I've misread it and misunderstood it.

    Apologies to all.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Hehe, we all make that mistake

  • #9
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't mean to sound dumb, but this is a learning experience for me. I understand the concept of register_globals. You can just create and use variables whenever and however you want.

    I read a very good info about register_globals which stated:

    Reliance on this directive was quite common and many people didn't even know it existed and assumed it's just how PHP works.
    I thought this too, that it was just how PHP worked. I think I know how to set a variable (I assume it is similar to Basic, Fortran, & Pascal).

    I did an internet search and this is what I came up with. I added this code to the beginning of the code I originally posted.

    Code:
    $name = strval($_GET['$name']); 
    	$lastName = strval($_GET['$lastName']);
    	$regionNumber = intval($_GET[$regionNumber]);
    	$socialNumber = intval($_GET[$socialNumber]);
    It is processing, but it is still NOT passing the data to the database. I know I am getting closer to the answer.

    What am I doing wrong?

    Am I not banging my head enought?

    Ric

  • #10
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Let me clarify, the database is adding records, but there is no data in the records.

    Ric

  • #11
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    This is where you learn the basics of decoding!

    Lesson 1: ECHO! ECHO! ECHO!
    When you assign variable and you're not sure what value is in it, echo it!!!

    If you did, you'd have found that

    PHP Code:
    $name strval($_GET['$name']); 
    $lastName strval($_GET['$lastName']);
    $regionNumber intval($_GET[$regionNumber]);
    $socialNumber intval($_GET[$socialNumber]); 
    didn't store anything.

    $_GET['$name'] means you want to get the value of $_GET with the key "$name", when what you want is the key "name".

    $_GET[$regionNumber] means you want to get the value of $_GET with the key with the VALUE of $regionNumber, which in this case is nothing, so you're looking for the value of $_GET[''].

    You want

    PHP Code:
    $name strval($_GET['name']); 
    $lastName strval($_GET['lastName']);
    $regionNumber intval($_GET['regionNumber']);
    $socialNumber intval($_GET['socialNumber']); 
    I gotta say, it seems like your tutorial was pretty bad. I really gotta recommend you find something else, because storing and accessing variables is the most basic of basics. Again, I'm not trying to be offensive... I just think you're a bit over your head if this stuff is tossing you around.

  • #12
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Keleth, thanks for bearing with me.

    The tutorial was 2009, but it was free. As soon as I get some extra money, I am going to take a PHP course.

    I get what you mean where name in $name = strval($_GET['name']); is referencing name in <input type="text" name="name" id="name" />.

    I added an echo statement and the values are still ",",0,0 (nothing is being passed).

    Below is the code as fixed thus far. What is missing?

    Code:
    <?php
    
    	$name = strval($_GET['name']); 
    	$lastName = strval($_GET['lastName']);
    	$regionNumber = intval($_GET['regionNumber']);
    	$socialNumber = intval($_GET['socialNumber']);  
    	
    	
    	include("connections/mySQL.phpConnectReal.php");
    	
    	mysql_select_db("$database_TestConnect") or die ("Unable to Connect to the database");
    	
    	$table = "statusValues";
    	if( mysql_num_rows( mysql_query("SHOW TABLES LIKE '".$table."'")))
    	{
    		
    	}else{
    		print("Table was not found, I will create it now.<br>");
    		
    		$sql = 	"CREATE TABLE `statusValues` (
    				`statVal_id` INT NOT NULL AUTO_INCREMENT ,
    				`FirstName` VARCHAR( 64 ) NOT NULL ,
    				`LastName` VARCHAR( 64 ) NOT NULL ,
    				`Region` INT NOT NULL ,
    				`SS#` INT NOT NULL,
    				INDEX ( `statVal_id` ) )"; 
    
    		mysql_query($sql) or die(mysql_error()) ;	
    		print("Table Creation Complete. . .<br>");
    	}
    	
    	
    	$function=$_GET['function'];
    	
    	
    	if(isset($function) && $function=="add"){
    		$sqlInsert = 	"INSERT INTO  `statusValues` (
    						`FirstName`, 
    						`LastName`, 
    						`Region`, 
    						`SS#`) 
    						VALUES ('$name', '$lastName', $regionNumber, $socialNumber)";
    
    		print("$sqlInsert<br>");
    		
    		echo"('$name', '$lastName', $regionNumber, $socialNumber)";
    		echo"TEST";
    		
    		mysql_query($sqlInsert) or die(mysql_error());
    		//print ("Data Enter Ok :D");
    		$function="";
    		echo'<script type="text/javascript" language="javascript">alert("Data Added.");location.replace("index.php");</script>';
    			}
    	
    	$regSQL = "SELECT `regionName`,`regionNumber` FROM regions";
    	$resultQuery = mysql_query($regSQL) or die(mysql_error()) ;
    	
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>PHP/MySQL Programming Course Part 2</title>
    </head>
    
    <body>
    
    
    
    <form id="enterForm" name="dateEnter" method="post" action="index.php?function=add">
      Enter First Name
        <input type="text" name="name" id="name" />
        <br />
    Enter Last Name
    <input type="text" name="lastName" id="lastName" />
    <br />
    Select Region Number
    <select name="regionNumber" id="regionNumber">
      <?php while ( $myrow = mysql_fetch_array($resultQuery) ) { ?>
      
      	<option value="<?php echo $myrow['regionNumber']; ?>"><?php echo $myrow['regionNumber'] ."-". $myrow['regionName']; ?></option>
      
      <?php } ?>	
    </select>
    <br />
    Please Enter SS# No Dashes 
    <input type="text" name="socialNumber" id="socialNumber" />
    <br />
    <input type="submit" name="addData" id="addData" value="Ok" />
    </form>
    <?php
    
    	$selectQuery = "SELECT  * FROM statusValues
    							 WHERE 1
    							 ORDER BY LastName ASC";
    	$resultName = mysql_query($selectQuery) or die(mysql_error());
    
    ?>
    <table width="400" border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td>id#</td>
        <td>First Name</td>
        <td>Last Name</td>
        <td align="center">Region</td>
        <td>&nbsp;</td>
      </tr>
    
    <?php while ($myrowRes = mysql_fetch_array($resultName)) { ?>
    
      <tr><p></p>
        <td><?php echo $myrowRes['statVal_id']; ?></td>
        <td><?php echo $myrowRes['firstName']; ?></td>
        <td><?php echo $myrowRes['lastName']; ?></td>
        <td align="center"><?php echo $myrowRes['region']; ?></td>
        <td>&nbsp;</td>
      </tr>
    
    <?php } ?>
    </table>
    
    </body>
    </html>

  • #13
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Throw print_r($_GET); (another debugging tip: all those $_ are called 'superglobals'... they're just arrays you can access from anywhere in the script... so when in doubt, just print_r them so you can see whats in them) to the top of your page.

    Are you sure the form is submitting? Positive you're passing by GET and not POST?

    2009 shouldn't be too bad, I neglected how long 3 years is, but it just sounds like a bad tutorial... I donno, maybe I'm wrong. But no need to take a PHP course... personally, self teaching is the best, by doing stuff you're doing now, but also by making ample use of google! Google stuff like 'intro to php tutorial' and you'll find a plethora of material! I just recommend staying away from W3Schools... Nettuts is good! Of course, don't hesitate to keep asking questions here (but do remember, googling first may save you time and headaches for waiting on us to answer)

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by Keleth View Post
    2009 shouldn't be too bad, I neglected how long 3 years is, but it just sounds like a bad tutorial... I donno, maybe I'm wrong. But no need to take a PHP course... personally, self teaching is the best, by doing stuff you're doing now, but also by making ample use of google! Google stuff like 'intro to php tutorial' and you'll find a plethora of material! I just recommend staying away from W3Schools... Nettuts is good! Of course, don't hesitate to keep asking questions here (but do remember, googling first may save you time and headaches for waiting on us to answer)
    It sounds like a bad tutorial to me too. As I said earlier, tizag.com is good for tutorials, the php.net manual is incredibly useful for looking up functions and what they do and google is also very handy for solving problems.

    W3 schools... erm no!
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #15
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I put a print_r($_GET); in and here are 2 screen shots. You can see it is inserting new (blank) records every time I try the code. It is processing the database, just not passing the data that I input.





  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •