PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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!
<?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
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!
<?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>';
}
?>