PDA

View Full Version : Anyone here do PHP


Wookie
11-02-04, 17:31
Needed to write myself something figured id learn php and mysql while im at it. Anyway ive done this before but im getting an error on this page for some reason.

Heres my Form
<form METHOD=POST ACTION="addnew.php">
<input type="hidden" name="id" value="NULL">
Start Place: <INPUT TYPE="TEXT" NAME="startvar"><br>
Destination: <INPUT TYPE="TEXT" NAME="DESTINATION"><br>
Miles: <INPUT TYPE="TEXT" NAME="MILES"><br>
Date: <INPUT TYPE="TEXT" NAME="DATE"><br>
<INPUT TYPE="SUBMIT" VALUE="SUBMIT">
</FORM>


Pretty simple, now heres the code that gets it.


$id = $_POST['id'];
$startvar1 = $_POST['startvar'];
$dest = $_POST['destination'];
$miles = $_POST['miles'];
$dt = $_POST['date'];


well ive done it before and it was pretty simple but I am getting this error
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in


which points right to the ID one, if I take the ID out and the NULL out of the form it does same thing except points to startvar1

all im doing is passing a bunch of letters and a date in the form.


if anyone has any ideas I would appreciate it. Just worked 9.5 hours now off to programming class for 2 more hours :)

anyway here is the entire thing


<?php
if (isset($_COOKIE["validated"])){
$cookieinfo = $_COOKIE["validated"];
}
else
{
echo 'Sorry you are not authorized to view this page';
}

if ($cookieinfo == 'myval'){
$DBhost = 'localhost';
$DBuser = 'localuser';
$DBpass = 'localpassword';
$DBName = 'localdb';
$table = 'miles';
mysql_connect($DBhost,$DBuser,$DBpass) or die('Unable to connect to database');
@mysql_select_db('$DBName') or die('Unable to select database $DBName');

$id = $_POST['id'];
$startvar1 = $_POST['startvar'];
$dest = $_POST['destination'];
$miles = $_POST['miles'];
$dt = $_POST['date'];

$sqlquery = "INSERT INTO $table VALUES('$id','$start','$dest','$miles','$dt')";
$results = mysql_query($sqlquery);

mysql_close():

header('location:myfile.php');
}
?>

patpawlowski
11-02-04, 19:24
I'm not exactly sure, and php is pretty forgiving, but I never used the single quotes
e.g.

I would use: $id = $_POST[id]; instead of $id = $_POST['id'];

hope this helps.

-pat

Wookie
11-02-04, 20:58
that worked thanks, still getting used to the syntax. getting another error now about having an extra $ but im done for the night. Thanks for help.

goldfish
11-03-04, 01:53
Hmm, I would use double quotes there actually.

$id = $_POST["id"];


The fully correct way to use $_POST variables is thus..

$text = isset($_POST["text"]) ? $_POST["text"] : null;

Wookie
11-03-04, 07:51
got it, but I cant connect to the database, gotta figure that out now. The double quotes does not work I get a error no matter how I do it.

Wookie
11-03-04, 08:09
I got it going thanks for help guys. Learning where I Need " and ' in php

goldfish
11-03-04, 08:26
Thats weird, we've bin using double quotes in our apps for yonks they seem to work fine?

Wookie
11-03-04, 11:28
dont ask me man, the singles quotes worked in another page but not this one. Heres my source so far, not finished yet and need to comment and clean up etc....This is my first PHP/MYSql Project.


miles.php
<?php
if (isset($_COOKIE["validated"])){
$cookieinfo = $_COOKIE["validated"];
}
else
{
echo 'Sorry you are not authorized to view this page';
}





if ($cookieinfo == "mine") {

//Find out the current month to display current mileage records
//This checks for a month number being passed in the URL, if it hasnt been it default to the current month
//Otherwise it puts in the month numbers that have been passed in URL
//Todo: Add Error Checking for incorrect months
$search = 0;
if (ISSET($_GET['search'])){
$search = $_GET['search'];
}

if ($search == 0){
$beginmonth = date("Y") . "-" . date("n") . "-01";
$endmonth = date("Y") . "-" . date("n") . "-31";
}
else
{
$beginmonth = date("Y") . "-" . $search . "-01";
$endmonth = date("Y") . "-" . $search . "-31";
}
echo "<center>Welcome to Jeffs Mileage control";
?>
<br><br>
Would you like to add new Mileage?
<br>
<form METHOD=POST ACTION="addnew.php">
<input type="hidden" name="id" value="NULL">
Start Place: <INPUT TYPE="TEXT" NAME="startvar"><br>
Destination: <INPUT TYPE="TEXT" NAME="DESTINATION"><br>
Miles: <INPUT TYPE="TEXT" NAME="MILES"><br>
Date: <INPUT TYPE="TEXT" NAME="DATE"><br>
<INPUT TYPE="SUBMIT" VALUE="SUBMIT">
</FORM>
<br><br>
<center>


<center>

<p align="center">View a Different Month?
</center>

<center>

<p align="center"><a href="miles.php?search=1">Jan</a> <a href="miles.php?search=2">Feb</a> <a href="miles.php?search=3">Mar</a> <a href="miles.php?search=4">Apr</a> <a href="miles.php?search=5">May</a> <a href="miles.php?search=6">June</a><br>
<a href="miles.php?search=7">Jul</a> <a href="miles.php?search=8">Aug</a> <a href="miles.php?search=9">Sep</a> <a href="miles.php?search=10">Oct</a> <a href="miles.php?search=11">Nov</a> <a href="miles.php?search=12">Dec</a></td>

</center>
</tr>
</table>
</center>
<?
$DBhost = "yourhost";
$DBuser = "youruser";
$DBpass = "ypurpass";
$DBName = "yourdb";
$table = "miles";
mysql_connect($DBhost,$DBuser,$DBpass) or die('Unable to connect to database');
@mysql_select_db("$DBName") or die('Unable to select database $DBName');



$query = "SELECT * FROM miles WHERE datehappend BETWEEN '$beginmonth' AND '$endmonth';";
$result=mysql_query($query);

$num=mysql_numrows($result);
mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

?>
<table border="1" width="577" height="18">
<tr>
<td height="18" width="145">StartPlace</td>
<td height="18" width="144">Destination</td>
<td height="18" width="140">MilesThere</td>
<td height="18" width="128">Date</td>
</tr>
</table>
<?

$i=0;
$totalmonthmiles = 0;
if ($num==0) {
echo "This month contains no records yet";
} else {
while ($i < $num) {

$startplace=mysql_result($result,$i,"startplace");
$dest=mysql_result($result,$i,"destination");
$milesthere=mysql_result($result,$i,"milesthere");
$dt=mysql_result($result,$i,"datehappend");
?>
<table border="1" width="577" height="19">
<tr>
<td height="19" width="143"><? echo "$startplace";?></td>
<td height="19" width="140"><? echo "$dest";?></td>
<td height="19" width="140"><? echo "$milesthere";?></td>
<td height="19" width="126"><? echo "$dt";?></td>
</tr>
</table>
<?
$totalmonthmiles = $totalmonthmiles + $milesthere;


$i++;

} //End of While loop
$totalcost = ($totalmonthmiles * .375);
?>
<table border="0" width="577" height="19">
<tr><td width="300"></td><td><align="right">Total Miles: <? echo "$totalmonthmiles";?></td>
<td>Total Cost: $<? echo "$totalcost";?></td>
</align></tr></table>
<?
} // End the if num = 0 else

} //End The

?>


addnew.php

<?php
if (isset($_COOKIE["validated"])){
$cookieinfo = $_COOKIE["validated"];
}
else
{
echo 'Sorry you are not authorized to view this page';
}

if ($cookieinfo == 'mine'){
$DBhost = "yourhost";
$DBuser = "youruser";
$DBpass = "ypurpass";
$DBName = "yourdb";
$table = "miles";
mysql_connect($DBhost,$DBuser,$DBpass) or die('Unable to connect to database');
@mysql_select_db("$DBName") or die('Unable to select database $DBName');

$id = NULL;
$startvar1 = $_POST[startvar];

$dest = $_POST[DESTINATION];
$miles = $_POST[MILES];
$dt = $_POST[DATE];

$sqlquery = "INSERT INTO `$table` (`id`, `startplace`, `destination`, `milesthere`, `datehappend`) VALUES ('', '$startvar1', '$dest', '$miles', '$dt');";
$results = mysql_query($sqlquery);

mysql_close();

header('location:miles.php');
}

?>