php result within a date range - php

I am an absolute newbie and still working out the kinks. So spare me if the code looks amateurish.
I have this form.html
The user enters the date range.
form action="report.php" method="post"
FROM DATE <input type="DATE" class="textarea" name="dateStart"><br> </br>
TO DATE <input type="DATE" class="textarea" name="dateEnd">
<input type="submit" class="textarea">
</form>
This gets processed in Report.php now report.php looks like this:
php
$_POST["dater"]=$date;
$con=mysqli_connect("LOCALHOST","xxxxxxx","xxxxxx","xxxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `xxxxxxx`
dater);
echo "<table border='1'>
<tr>
<th>DATE</th>
<th>MBM</th>
<th>bulkTank</th>
<th>bagsAt40</th>
";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['bulkTank'] . "</td>";
echo "<td>" . $row['bagsAt40'] . "</td>";
echo "</tr>";
}
echo "</table>";
I have no clue what to do next....
its not working, i tried a few things but how can I echo the result within a specific date range. Thanks!
New edit ----
This is what the new code looks like... Form
START DATE
END DATE
Report.php
<?php
$date=$_POST["dateStart"];
$con=mysqli_connect("LOCALHOST","XXXXXXX","++++++","XXXXXX");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `WeeklyStockTake`
WHERE dateColumn=$date);
echo "<table border='1'> <!--line 32 starts here--!>
<tr>
<th>DATE</th>
<th>MBM</th>
<th>bulkTank</th>
<th>bagsAt40</th>
-----If I process it now I get this error -
Parse error: syntax error, unexpected T_STRING in /home/simples2/public_html/test/report.php on line 32 (I have commented above as to where it starts)
How to I declare the end date?
Thanks and regards!

Dont know if its a typo but php should be <?php
$_POST["dater"]=$date
This should be
$date=$_POST["dateStart"]
Also you should add a POST for dateEnd also
The query should be more like:
SELECT * FROM `xxxxxxx` WHERE dateColumn<=$date

Related

ODBC query's not working with PHO

I'm working with an ODBC connection rather than MySQL. I have a search function which I have more or less copied over to apply with my ODBC connection, however they are not working. Here is my code except for the connection:
<!doctype html>
<html>
<title> Quoting System </title>
<head>
</head>
<body>
<form class="form" method="POST" action='try31.php'>
Quote Number <input class="form-control" type="text" name="quote" id="quote" placeholder="Enter Quote Number">
<br> &nbsp <input class="btn btn-default" type="submit" name="search" value="Search">
</form>
<table>
<tr>
<th>Company Name</th>
<th>Address1</th>
<th>Address2</th>
</tr>
<?php
if (!$conn){
if (phpversion() < '4.0'){
exit("Connection Failed: . $php_errormsg" );
}
else{
exit("Connection Failed:" . odbc_errormsg() );
}
}
if(isset($_POST['search'])){
$quote = $_POST['quote'];
$query = "SELECT * FROM dbo.tblVersions2 WHERE QuoteNumber LIKE '".$quote."'";
}
$result = odbc_exec($conn,$query);
while($row =odbc_fetch_row($result)){
echo "<tr>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
// Disconnect the database from the database handle.
//odbc_close($conn);
?>
</table>
</body>
</html>
As I don't get an error message, I know my connection is working, however currently, when I select the button, data does not appear as expected... Please help! Thanks
Found that with MS SQL ODBC connection the query syntax is different than MySQL. I changed where I was calling my columns in the table from:
$result = odbc_exec($conn,$query);
while($row =odbc_fetch_row($result)){
echo "<tr>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
To this:
$result = odbc_exec($conn, $stmt);
while (odbc_fetch_row($result)) // while there are rows
{
echo "<tr>";
echo "<td>" . odbc_result($result, "CompanyName") . "</td>";
echo "<td>" . odbc_result($result, "Address1") . "</td>";
echo "</tr>";
}
The odbc_result function was crucial here.

Parse error: syntax error, unexpected 'text' (T_STRING), expecting ',' or ';' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am having trouble getting this to work. I have been able to call data from a dropdown menu and place it into a table and have it actively update without reloading the page. I am now trying to get the database information that is called to appear within a text input field or another drop down menu.
So basically I have a drop down menu that will call up user information, I am trying to get that information that is called to appear within another form so I can update it. Here is the code I am working with;
table7.php
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtDisp").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtDisp").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser2.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form method="post" action="localhost/table7.php">
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<?php
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['uname'] . "</option>";
mysqli_close($con);
}
?>
</select>
</form>
<br>
<div id="txtDisp"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser2.php
<?php
$q = intval($_GET['q']);
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM users WHERE id = '".$q."'";
$result1 = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Username</th>
<th>E-Mail</th>
<th>Info 1</th>
</tr>";
echo "<form action="getuser2.php" method="post">";
while($row1 = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td><input type="text" name="info1" value=" . $row1['uname'] . "></td>";
echo "<td>" . $row1['email'] . "</td>";
echo "<td>" . $row1['info1'] . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
mysqli_close($con);
?>
Within the getuser2.php code if you strip out the form input section and replace it with a call for uname using the format directly below for email and info it will display the data called from the database in standard text format.
However, I am encountering this error:
Parse error: syntax error, unexpected 'text' (T_STRING), expecting ',' or ';' in C:\wamp\www\getuser2.php on line 25
The problem is the following:
echo "<td><input type="text" name="info1" value=" . $row1['uname'] . "></td>";
You have double quotes inside a double-quoted string. PHP doesn't know where the string ends.
An easy fix (since you're not using variables inside the string anyway) is to change the double quotes to single quotes:
echo '<td><input type="text" name="info1" value="' . $row1['uname'] . '"></td>';
Update these lines as shown below
$sql = "SELECT * FROM users WHERE id = " . $q . "";
// ...
echo "<table border='1'><tr><th>Username</th><th>E-Mail</th><th>Info 1</th></tr>";
// ...
echo "<form action=\"getuser2.php\" method=\"post\">";
// ...
while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
// ...
echo "<td><input type=\"text\" name=\"info1\" value=" . $row1['uname'] . "></td>";

PHP Datepicker in SQL query

so I have this page that shows today's matches by default, it has this datepicker form
<form method="post">
<p> Select Date:<input id="datepicker" type="text" size="8" /> </p>
<input type="submit" value="Submit" name="usub" />
</form>
<?php
if(isset($_POST["usub"])){ $date = $_POST["datepicker"]; }
else{ $date = date('Y-m-d'); }
$data = mysql_query("SELECT * FROM todaysmatches where matchdate='$date'") or die(mysql_error());
echo $_POST["usub"];
echo "<h4>$today Matches</h4> </br>";
//table
if (mysql_num_rows($data)==0){
echo " No Matches";
echo "</br>";
echo "<h4> Sorry About That Check For other Days Or you can Check the Library</h4>";
}
else{
echo "<table border='1'>
<tr>
<th>Match</th>
<th>Tourmanet</th>
<th>Date</th>
</tr>";
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<td>" . $info['curmatch'] . "</td>";
echo "<td>" . $info['tournamentname'] . "</td>";
echo "<td>" . $info['matchdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
what I want is if the user choose a date in the form it would go in the query and bring the data associated with that date while keeping today as the default one when they first load the page
You need to quote the datevalue in the query as
SELECT * FROM todaysmatches where matchdate= '$today'
$data = mysql_query("SELECT * FROM todaysmatches where matchdate='$today'")
Now to get the datepicker value you need to change a bit in the form and use name attributes as
<form method="post">
<p> Select Date:<input id="datepicker" type="text" size="8" name="datepicker"/> </p>
<input type="submit" value="Submit" name="usub"/>
</form>
Then in PHP you have do as
if(isset($_POST["usub"])){
$date = $_POST["datepicker"];
}
And use it in the query and in the else part you can have the query to get the data from today's date
NOTE : Make sure that the date you are passing from date picker to query is in proper format.

Need assistance displaying current mysql record and modifying in php form

Thanks in advance for any light shed.
I have a mysql database consisting of customers with some fields pertaining to each customer. currently running on one of my lamp servers. There is security risks with my code at the moment, but I plan to get the functionality i'm looking for and then reconfigure the code for a tighter security. At the moment I have an html index file that calls on php script to search mysql database by firstname or lastname. Upon this query it displays a list of users and allows me to modify the user. When I click modify it pulls the correct customer id number, but it is not displaying any current information, nor allowing me to update the info.
To summarize, I would like to search a customer, and it pull up selected fields and show the content and allow me to actively change the data and resend it to the database.
My search.html code:
<html>
<body>
<form action="scripts/search.php" method="post">
Firstname: <input type="text" name="firstname">
<input type="submit">
</form>
<form action="scripts/lastnamesearch.php" method="post">
Lastname: <input type="text" name="lastname">
<input type="submit">
</form>
<form action="scripts/phonenumbersearch.php" method="post">
Phone Number: <input type="text" name="phone">
<input type="submit">
</form>
</body>
</html>
MY search.PHP Script:
//this script allows me to search the database by filling out one of the forms and clicking submit. Each of the forms calls upon it's own individual script, I realize that this is probably cumbersome, due to my lack of coding knowledge.
<?php
$con=mysqli_connect("localhost","root","*****","*******");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers WHERE `firstname` LIKE '$_POST[firstname]'");
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>phone</th>
<th>address</th>
<th>notes</th>
<th>additional notes</th>
<th>passwords</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['addnotes'] . "</td>";
echo "<td>" . $row['passwords'] . "</td>";
echo "Modify User";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
My modify.php script:
//this is where I believe one of my problems lie. when I click modify user on the search.php script it calls on this script and it loads the correct user/customer id in the address bar, but it doesn't show any existing data, nor does it update the data that I fill in the cells.
<?php
$con=mysqli_connect("localhost","root","crapola1","Computition");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$mysqli_query = "SELECT * FROM customers WHERE ID = $_get[id]";
$mysqli_result = mysqli_query($mysqli_query);
$customer = mysqli_fetch_array($mysqli_result);
?>
<h1> You are modifying a user</h1>
<form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
Firstname<input type="text" name="inputFirstname" value="<?php echo $row['firstname']; ?>" /><br />
Notes<input type="text" name="inputNotes" value="<?php echo $row['notes']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify" />
</form>
Thanks again,
I've been searching on this topic for about a week now and have pieced together this much, but can't seem to get over this "hump"
$_GET is a super global array . It should be in UPPERCASE.
Change the query on your modify.php here
SELECT * FROM customers WHERE ID = $_get[id] to upper case.
Must be..
SELECT * FROM customers WHERE ID = ".$_GET['id']
Also, It is strictly not advised to pass the $_GET or $_POST parameters directly to your query as it leads to SQL injection. You need to switch over to PreparedStatements

Updating mysql database using While loop php

This has been bugging me for 3 days now.. I'm new to this and trying to get my head round something. I have a form which involves 3 fields. Firstname, Surname, Marks. I have used a while loop to generate the table from a mysql table. I have used a text box and used the loop to call the text box after the 'ID' so each text box is named uniquely. I am then using a post method to send values to a second page which will update the 'marks' column with the value the user has just put in.. this is where I am finding my problem!
This is the initial page.
<html>
<head><title>Please Enter Your Surname</title></head>
<body>
<center>
<h2><font color=blue>Please Enter Your Surname</font></h2><p>
<form action="insert.php" method="POST">
<?php
$db = mysql_connect("localhost","root","");
if (!$db)
{
do_error("Could not connect to the server");
}
mysql_select_db("session6",$db)or do_error("Could not connect to the database");
$result = mysql_query("SELECT * FROM members ORDER BY id",$db);
$rows=mysql_num_rows($result);
if(!$rows)
{
do_error("No results found");
}
else
{
echo "<table border=3 cellspacing=1 cellpadding=1
align=center bgcolor=lightblue>\n";
echo "<caption><h2><font color=blue> Members Details
</font></h2></caption>\n";
echo "<tr><th>Member Id</th><th>Firstname</th><th>Mark</th></tr>\n";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td strong>" . $row['Id'] . "</td>";
echo "<td strong>" . $row['Firstname'] . "</td>";?>
<td strong><input type="text" name="<?php echo $row['Id']; ?>" size="20"></td>
<tr>
<?php
}
?><input type="hidden" name="no_of_rows" value="<?php echo $rows; ?>">
<?php
echo "</table>\n";
}
mysql_close($db) or do_error("Could not close connection");
function do_error($error)
{
echo $error;
die();
}
?>
<input type="submit" value="Search">
<input type="reset" value="Reset">
</form>
</body></html>
`
Then the update is done here which is where I seem to be having a problem:
<html>
<body>
<?php
$db = mysql_connect("localhost","root","");
if (!$db)
{
do_error("Could not connect to the server");
}
mysql_select_db("marks",$db)or do_error("Could not connect to the database");
$i=1;
while ($i <= $_POST["no_of_rows"])// or $_POST["No_of_Rows"] from form
{
$insertsql = "UPDATE members SET mark = " . $_POST[$i] . " WHERE Id = " . $row['Id'] . ";";
echo $_POST['$i'];
$i++;
}
?>
</body></html>
When I echo $_POST[$i'] it shows the correct values but does not update the DB, and I'm not about ready to throw my laptop in the bin! ha! I know it is prob going to be something stupid I just can't see what, so any help would be appreciated.
You're missing the single quotes in your update query. This would help:
$insertsql = "UPDATE `members` SET `mark` = '" . $_POST[$i] . "' WHERE `Id` = '" . $row['Id'] . "' ;";
you are also not running the mysql_query query command for the update
lastly you are using the mysql php commands which are deprecated. Use mysqli or pdo instead. and don't forget to escape data in your queries to prevent sql injections
Problem is the single quotes here, forcing to literal '$i' which probably isnt a key in $_POST
echo $_POST["$i"];
No need to use quotes when variable is used:
$_POST[$id];

Categories