Page 1 of 3
Get Method
Posted: Wed Oct 29, 2003 3:24 pm
by Mactek
Hello everyone,
I cannot seem to get this working. I have the follow "if" followed by a GET FORM:
Code: Select all
if (isset($HTTP_GET_VARS['addcomputer'])):
Now i'm trying to read my variables into an insert statement but i can't figure out how to call the viarbles from the GET METHOD:
Code: Select all
if ("SUBMIT" == $submitcomputer) {
$sql = "INSERT INTO mactrix SET " .
"assetnum='$assetnum', " .
"cpu='$cpu', " .
"os='$os', " .
"loc='$loc', " .
"sn='$sn', " .
"macaddress='$macaddress', " .
"warrentee='$warrentee'" ;
I've tried all sorts of different ways including: "assetnum=($HTTP_GET_VARS['assetnum']), " .
Nothing seems to be working. any help would be greatly appreciated, thanks!!
Posted: Wed Oct 29, 2003 3:35 pm
by d3ad1ysp0rk
Posted: Wed Oct 29, 2003 3:37 pm
by volka
maybe a small example tells you more than a dozen lines of explaination
Code: Select all
<?php
$MYGET = array(
'keyA'=>'valA',
'keyB'=>'valB'
);
$string = "lalala $MYGET[keyA] lalala";
echo $string;
?>
but if you're outside a double quoted (or heredoc) string you have to quote the key if it's a string
Code: Select all
<?php
$MYGET = array(
'keyA'=>'valA',
'keyB'=>'valB'
);
echo 'lalala ', $MYGET['keyA'], ' lalala'; // this time not only $MYGET[keyA]
?>
Posted: Wed Oct 29, 2003 3:44 pm
by Mactek
OK, switching submit with the variable didn't work.
I'm not sure how an array is going to help me either.
I have recently updated to panther 10.3 and i had to reconfigure but i noticed that i needed to change my variables for the GET METHOD. Now i cannot figure out how to read them in form the submit. Can anyone help, thanks!
Posted: Wed Oct 29, 2003 3:58 pm
by Fredix
first, use $_GET['name'] instead of what you have.
and write
Code: Select all
<?php
$sql = "INSERT INTO mactrix SET assetnum='".$_GET['assetnum']."'";
?>
or make the content of $_GET available as normal variables.
Posted: Wed Oct 29, 2003 4:16 pm
by volka
just do as Fredix wrote
But to explain my example:
$_GET is an array, $MYGET is an array. I thought the transfer would be obvious

Posted: Thu Oct 30, 2003 10:00 am
by Mactek
Hmmm. I still must be doing something wrong!!
Posted: Thu Oct 30, 2003 10:28 am
by Mactek
Ok here is the code, please help, before i go crazy, thanks!!
Code: Select all
<?php
if (isset($HTTP_GET_VARS['addcomputer'])):
?>
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=GET align="center">
<table width="60%" border="10" color="blue" cellpadding="3" cellspacing="5" align="center">
<tr>
<th colspan="2">Enter your data here:</th>
</tr>
<tr>
<td width="50%" align="right">Assest Number:</td>
<td width="50%"><input type="text" name="assetnum" size="50"></td>
</tr>
<tr>
<td width="30%" align="right">CPU:</td>
<td width="70%"><input type="text" name="cpu" size="50"></td>
</tr>
<tr>
<td width="30%" align="right">OS:</td>
<td width="70%"><input type="text" name="os" size="50"></td>
</tr>
<tr>
<td width="30%" align="right">Location:</td>
<td width="70%"><input type="text" name="loc" size="50"></td>
</tr><tr>
<td width="30%" align="right">Serial Number:</td>
<td width="70%"><input type="text" name="sn" size="50"></td>
</tr><tr>
<td width="30%" align="right">Mac Address:</td>
<td width="70%"><input type="text" name="macaddress" size="50"></td>
</tr><tr>
<td width="30%" align="right">Warrentee:</td>
<td width="70%"><input type="text" name="warrentee" size="50"></td>
</tr>
</table>
<BR>
<INPUT TYPE=SUBMIT NAME="submitcomputer" VALUE="SUBMIT">
</FORM>
<?php
else:
//Connect to the database server
$dbcnx = @mysql_connect(
"localhost", "root", "wheelie");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}
// Select the mfsmsd database
if (! @mysql_select_db("mfsmsd") ) {
echo( "<P>Unable to locate the mfsmsd " .
"database at this time.</P>" );
exit();
}
// If a computer has been submitted,
// add it to the database.
if ( "SUBMIT" = $submitcomputer ) {
$sql = "INSERT INTO mactrix SET " .
"assetnum='".$_GET['assetnum']."'," .
"cpu='".$_GET['cpu']."'," .
"os='".$_GET['os']."'," .
"loc='".$_GET['loc']."'," .
"sn='".$_GET['sn']."'," .
"macaddress='".$_GET['macaddress']."'," .
"warrentee='".$_GET['warrentee']."'" ;
if (mysql_query($sql)) {
echo ("<P>Your computer has been added.</P>");
} else {
echo("<P>Error adding submitted computer: " .
mysql_error() . "</P>");
}
}
//Delete goes here
echo("<P> Here are all the computers " .
"in our database: </P>");
// Request the ID and text of all the computers
$result = mysql_query(
"SELECT assetnum, cpu, os, loc, sn, macaddress, warrentee FROM mactrix");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
// Display the text of each computer in a table
echo "<table border=10\n";
echo '<tr><td>Asset Number<td>CPU<td>OS<td>Location<td>Serial Number<td>Mac Address<td>Warrentee</td></tr>';
while ($myrow = mysql_fetch_row($result)) {
printf("<tr><td>%s</td><td>%s<td>%s<td>%s<td>%s<td>%s<td>%s</td></tr>\n",
$myrow[0], $myrow[1], $myrow[2], $myrow[3], $myrow[4], $myrow[5], $myrow[6]);
}
echo "</table>\n";
// When clicked, this link will load this page
// with the computer submission form displayed.
echo("<P><A HREF='$PHP_SELF?'addcomputer'=1>" .
"Add a Computer!</A></P>");
endif;
?>
Posted: Thu Oct 30, 2003 3:22 pm
by Fredix
while I search for other errors I'd like to note, that I can't find any
{
}
in your if statements!!!(at least in the first if/else)
Code: Select all
<?php
if ( "SUBMIT" = $submitcomputer ) {
?>
nonsense!!
assign the value of $submitcomputer to the string "Submit"!!!
instead write:
Code: Select all
<?php
if ($var == "string")
{
//write {} below the if
//and tab the codeblock for better reading
}
?>
[/quote]
...and some further tipps:
lowercase your html and double-quote attributes eg: method="get"
make your source code readable by using TAB (or several whitespaces)
and put curly braces {} that belong together one under another (see my if-example). Finally don't make that echo line break stuff like
you can insert as many line breaks in your echo as you want.
Posted: Thu Oct 30, 2003 4:13 pm
by jason
A note:
This is correct. In fact, there is nothing wrong with this, and it will work the same as
Indeed, the first way is just slightly better. The difference is that people are used to doing it the second way.
The technical reason why the first way is better is because of problems like this:
It's a common problem, but one that is not easily caught, because syntatically, it's correct.
However, something like this:
will throw an error.
You may now return to your regularly scheduled program.
Posted: Thu Oct 30, 2003 4:41 pm
by Mactek
that is not making a difference in my script. The problem is the vars in the insert statement. I cannot figure out how to call the variables. I believe i am doing it the wrong way. Maybe my " or ' are misplaced or i am just doing it wrong with the $_GET. Thanks!
Posted: Fri Oct 31, 2003 3:06 am
by twigletmac
Are you setting $submitcomputer anywhere, shouldn't it be $_GET['submitcomputer'] ?
What do you get as output from running the following code in a separate script:
Code: Select all
<?php
echo '<pre>';
echo 'PHP Version: '.phpversion()."\n";
echo 'Display Errors: '.ini_get('display_errors')."\n";
echo 'Error Level: '.(ini_get('error_reporting') == '2047' ? 'E_ALL' : 'Not E_ALL')."\n";
echo 'Register Globals: '.(ini_get('register_globals') == '' ? 'Off' : 'On')."\n";
echo '</pre>';
?>
Mac
Posted: Fri Oct 31, 2003 3:34 am
by twigletmac
Try this code, I'm not able to test it properly because I don't have your database so just post back with any problems:
Code: Select all
<?php
// a hidden field has been added to the form and now we can test it to
// see if the form has been submitted, note the comments within the
// form for reasons.
// use proper bracing, not the alternative if: else: structure as it is
// not standard and will cause issues with code debugging.
if (!isset($_POST['action'])) {
?>
<!-- $_SERVER['PHP_SELF'] needs to be used instead of $PHP_SELF -->
<!-- Use the POST method to avoid issues with the amount of data that
can be sent using GET -->
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post" align="center">
<table width="60%" border="10" color="blue" cellpadding="3" cellspacing="5" align="center">
<tr>
<th colspan="2">Enter your data here:</th>
</tr>
<tr>
<td width="50%" align="right">Assest Number:</td>
<td width="50%"><input type="text" name="assetnum" size="50" /></td>
</tr>
<tr>
<td width="30%" align="right">CPU:</td>
<td width="70%"><input type="text" name="cpu" size="50" /></td>
</tr>
<tr>
<td width="30%" align="right">OS:</td>
<td width="70%"><input type="text" name="os" size="50" /></td>
</tr>
<tr>
<td width="30%" align="right">Location:</td>
<td width="70%"><input type="text" name="loc" size="50" /></td>
</tr><tr>
<td width="30%" align="right">Serial Number:</td>
<td width="70%"><input type="text" name="sn" size="50" /></td>
</tr><tr>
<td width="30%" align="right">Mac Address:</td>
<td width="70%"><input type="text" name="macaddress" size="50" /></td>
</tr><tr>
<td width="30%" align="right">Warrentee:</td>
<td width="70%"><input type="text" name="warrentee" size="50" /></td>
</tr>
</table>
<br />
<!-- Use a hidden field which will be checked to ensure that the form has
been submitted as it is more reliable than the submit button due
to browser implementations -->
<input type="hidden" name="action" value="post" />
<input type="submit" name="submitcomputer" value="submit" />
</form>
<?php
} else {
//Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', 'wheelie');
if (!$dbcnx) {
// parenthesis are uneccessary around echo statements, try not to have them
// spanning multiple lines as you were, it will cause issues with debuggin
// as it makes the code very difficult to read. Indenting will help too.
echo '<p>Unable to connect to the database server at this time.</p>';
exit();
}
// Select the mfsmsd database
if (!@mysql_select_db('mfsmsd')) {
echo '<p>Unable to locate the mfsmsd database at this time.</p>';
exit();
}
// You've already tested that the form has been submitted so there is
// no need to do it again.
// never ever insert data directly into a database without first
// escaping it to avoid problems later (mainly around security)
// you may want to add additional code to ensure that information
// has infact been entered and that the fields aren't all blank
foreach ($_POST as $key => $value) {
$_POST[$key] = addslashes(trim($value));
}
$sql = "INSERT INTO mactrix";
$sql .= "SET assetnum='".$_POST['assetnum']."', cpu='".$_POST['cpu']."', os='".$_POST['os']."', loc='".$_POST['loc']."', sn='".$_GET['sn']."', macaddress='".$_POST['macaddress']."', warrentee='".$_POST['warrentee']."'";
$insert = @mysql_query($sql);
if ($insert) {
echo '<p>Your computer has been added.</p>';
} else {
echo '<p>Error adding submitted computer: '.mysql_error().'</p>';
}
echo '<p>Here are all the computers in our database:</p>';
// Request the ID and text of all the computers
$sql = "SELECT assetnum, cpu, os, loc, sn, macaddress, warrentee FROM mactrix";
$result = mysql_query($sql);
if (!$result) {
echo '<p>Error performing query: '.mysql_error().'<br />'.$sql.'</p>';
exit();
}
// Display the text of each computer in a table
// heredoc can be very helpful for things like this:
echo <<<END
<table border="10">
<tr>
<th>Asset Number</th>
<th>CPU</th>
<th>OS</th>
<th>Location</th>
<th>Serial Number</th>
<th>Mac Address</th>
<th>Warrentee</th>
</tr>
END;
// using mysql_fetch_assoc() means that you can refer to array
// elements by name instead of number.
while ($row = mysql_fetch_assoc($result)) {
echo <<<END
<tr>
<td>{$row['assetnum']}</td>
<td>{$row['cpu']}</td>
<td>{$row['os']}</td>
<td>{$row['loc']}</td>
<td>{$row['sn']}</td>
<td>{$row['macaddress']}</td>
<td>{$row['warrentee']}</td>
</tr>
END;
}
echo <<<END
</table>
END;
// When clicked, this link will load this page with the computer
// submission form displayed.
// don't need to add the addcomputer bit (which really shouldn't be
// in single quotes) as the form will always load if it has not
// been submitted.
echo '<p><a href='.$_SERVER['PHP_SELF'].'>Add a Computer!</a></p>';
}
?>
Mac
Posted: Fri Oct 31, 2003 10:37 am
by Mactek
Here is the results for that:
PHP Version: 4.3.3
Display Errors:
Error Level: E_ALL
Register Globals: Off
Thanks!!!
Posted: Fri Oct 31, 2003 10:40 am
by Mactek
This code isn't working nothing will evem load up. I just turned my globals to OFF, and that didn't make difference either.