...

View Full Version : isset issues learning PHP



consult1
07-12-2012, 05:39 PM
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:


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:


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:



<?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

tangoforce
07-12-2012, 05:44 PM
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.


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..

Keleth
07-12-2012, 05:55 PM
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.

consult1
07-12-2012, 07:19 PM
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

consult1
07-12-2012, 07:23 PM
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

Keleth
07-12-2012, 07:39 PM
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.

tangoforce
07-12-2012, 08:11 PM
Uh, tango, I don't see anything wrong with his if statement...

And looking at it again, neither do I :o

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.

Keleth
07-12-2012, 08:55 PM
Hehe, we all make that mistake :p

consult1
07-12-2012, 10:43 PM
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.


$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

consult1
07-12-2012, 10:45 PM
Let me clarify, the database is adding records, but there is no data in the records.

Ric

Keleth
07-12-2012, 11:52 PM
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



$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



$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.

consult1
07-13-2012, 04:37 PM
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?


<?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>

Keleth
07-13-2012, 04:42 PM
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)

tangoforce
07-13-2012, 05:23 PM
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!

consult1
07-13-2012, 06:44 PM
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.

http://webportalservices.net/SS1.jpg

http://webportalservices.net/SS2.jpg

Keleth
07-13-2012, 06:51 PM
I'm trying to avoid telling you everything of whats going on; you need to learn!

Think about it... you printed out the content of $_GET and it didn't have any of your input values. Thus, your form isn't submitting by GET. Its obvious that your form isn't submitting the data. Its probably going by POST, but we can't know without seeing the form code.

consult1
07-13-2012, 06:56 PM
I JUST SOLVED IT!!!!!!! :)

I needed $_POST

Not $_GET

(I think that was what tangoforce alluded to in his first post.)

Thank you tangoforce. I went to tizag.com found code examples of Retrieving Form Data - Setting up Variables

Thank you Keleth for pointing me in the right direction.

Here is the code I changed:


$name = strval($_POST['name']);
$lastName = strval($_POST['lastName']);
$regionNumber = intval($_POST['regionNumber']);
$socialNumber = intval($_POST['socialNumber']);


$_POST retrieves our form data and output's it directly to our browser. The best way to do this, is to make variables for each element in our form, so we can output this data at will, using our own variable names. Place the following lines of code at the top of your form file using the correct PHP syntax.

I am goign to change the status to SOLVED.

Thank you all again!

-Cheers,

Ric

Keleth
07-13-2012, 07:23 PM
Now mind you, you can have your form submit by GET! It can be useful sometimes, based on your form's purpose, but in general, you will be using get. To make sure, you'd set that in your form tag with method="post" or method="get"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum