start end date & dropdown menu from mysql in one html page - php

I am trying to make a html or php page (for some own learning process) which can input 3 selection and then display there results in next page.
1- input Start Date
2- input End Date
3- Show Service Name in drop down menu via mysql query to get services names from the table
So far I have managed to get the start and end table and drop down menu which successfully query the services table and shows the name, but the problem is that when i click submit i can see the results of start and end date but i am unable to see how can i add services selection in the posting.
This is my code.
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#start_datepicker").datepicker();
$("#end_datepicker").datepicker();
});
</script>
</head>
<body style="font-size:62.5%;">
<form action="test.php" method="post">
Start Date: <input type="text" name="startdate" id="start_datepicker"> <br />
End Date: <input type="text" name="enddate" id="end_datepicker"><br />
<select name="srvname">
<?php
$conn = new mysqli('localhost', 'root', 'SQLPASS', 'radius')
or die ('Cannot connect to db');
$result = $conn->query("select srvname name from rm_services");
while ($row = $result->fetch_assoc()) {
echo "<option value=\"" . $row["id"] . "\">" . $row["name"] . "</option>";
}
?>
</select>
<input type="submit" value="Submit:">
</form>
</body>
</html>
and this is test.php which will per form action shows the date
<?php
$STARTDATE = $_POST['startdate'];
$ENDDATE = $_POST['enddate'];
$SRVNAME = $_POST['srvname']; //gets the value -> $row["id"]
echo "<h2>You have entered the following information:</h2>";
echo "<pre>$STARTDATE</pre> ";
echo "<pre>$ENDDATE</pre>";
echo "<pre>$SRVNAME</pre>";
?>

This one should work, and for the future: if you post something on stackoverflow, please post formatted code, it's way easier to edit and especially to read it...
your index.php or whatever...
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#start_datepicker").datepicker();
$("#end_datepicker").datepicker();
});
</script>
</head>
<body style="font-size:62.5%;">
<form action="test.php" method="post">
Start Date: <input type="text" name="startdate" id="start_datepicker"> <br />
End Date: <input type="text" name="enddate" id="end_datepicker"><br />
<select name="srvname">
<?php
$conn = new mysqli('localhost', 'root', 'SQLPASS', 'radius')
or die ('Cannot connect to db');
$result = $conn->query("select id, name from rm_services");
while ($row = $result->fetch_assoc()) {
echo "<option value=\"" . $row["id"] . "\">" . $row["name"] . "</option>";
}
?>
</select>
<input type="submit" value="Submit:">
</form>
</body>
</html>
your test.php
<?php
$STARTDATE = $_POST['startdate'];
$ENDDATE = $_POST['enddate'];
$SRVNAME = $_POST['srvname']; //gets the value -> $row["id"]
echo "<h2>You have entered the following information:</h2>";
echo "<pre>$STARTDATE</pre> ";
echo "<pre>$ENDDATE</pre>";
echo "<pre>$SRVNAME</pre>";
?>
I didn't test it, but actually it should work...

Related

Passing a mysql row inside of a PHP/HTML Form

Im becomming quite a regular on here...
I am trying to dynamicly print out a table in PHP depending on what results are found with a MYSQL Statement.
See the below code, I am getting the below error
[Fri Jun 09 18:51:32.478737 2017] [fcgid:warn] [pid 63368] [client 5.69.190.95:64631] mod_fcgid: stderr: PHP Parse error: syntax error, unexpected 'showhistory' (T_STRING), expecting ',' or ';' in /home/tools/public_html/searchhistory.php on line 84, referer: http://tools.cidetech.co.uk/history.php
It seems to have a problem with just the "form" building inside of the loop I have no problems up until this line -
echo " <td><form method="POST" action="showhistory.php">
<input type="hidden" name="id_director" value=".$row["id"]"
</form></td> ";
I cant seem to figure out where I am going wrong, this would work fine in just pure HTML, however it needs to be inside of the mysql/php part as i need to pass the row id through inside of the button.
To be specific it is this part of the code I am struggling with
for ($i = 0; $i < count($idArray); $i++)
{
$sql="SELECT * FROM history WHERE id LIKE '%{$idArray[$i]}%'";
$result=$con->query($sql);
while($row=$result->fetch_assoc())
{
echo "<tr>";
echo "<td><pre>".$row["id"]."</pre></td>";
echo "<td><pre>".$row["date"]."</pre></td>";
echo "<td><pre>".$row["domain"]."</pre></td>";
echo " <td><form method="POST" action="showhistory.php">
<input type="hidden" name="id_director" value=".$row["id"]"
</form></td> ";
}
}
echo "</table>";
mysqli_close($conn);
?>
The full code can be seen here
<DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>CWCS Domain Checker Tool</title>
</head>
<body>
<div class="header">
<a href="index.php">
<img src="cwcs-logo.png">
</a>
</div>
<hr/>
<div class="searchform">
<form action="searchhistory.php" method="post">
<label for="domain"> <input class="submit" type="text" name="domain" /> </label>
<input class="submitbutton" type="submit" name="search" value="Search for Domain" />
</form>
</div>
<?php
#define connection info/variables needed
$servername = "localhost";
$username = "";
$password = "";
$dbname = "domainhistory";
$domain = $_POST['domain'];
$idArray = array();
#creates mysql connection
$con=new mysqli($servername,$username,$password,$dbname);
if($con->connect_error)
{
echo 'Connection Faild: '.$con->connect_error;
}
else
{
$sql="SELECT * FROM history WHERE domain LIKE '%{$domain}%'";
$result=$con->query($sql);
#Pushes the ID of the mysql row into an array
while($row=$result->fetch_assoc())
{
array_push($idArray,$row["id"]);
}
}
mysqli_close($conn);
?>
<!---prints out the ID's stored in the array -->
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "domainhistory";
$con=new mysqli($servername,$username,$password,$dbname);
if($con->connect_error)
{
echo 'Connection Faild: '.$con->connect_error;
}
else
{
}
echo "<table>";
echo "<tr>";
echo "<th> ID </th>";
echo "<th> Domain</th>";
echo "<th> Date </th>";
echo "</tr>";
## - loops through the ID array, and then prints out the data relating to that ID.
for ($i = 0; $i < count($idArray); $i++)
{
$sql="SELECT * FROM history WHERE id LIKE '%{$idArray[$i]}%'";
$result=$con->query($sql);
while($row=$result->fetch_assoc())
{
echo "<tr>";
echo "<td><pre>".$row["id"]."</pre></td>";
echo "<td><pre>".$row["date"]."</pre></td>";
echo "<td><pre>".$row["domain"]."</pre></td>";
echo " <td><form method="POST" action="showhistory.php">
<input type="hidden" name="id_director" value=".$row["id"]"
</form></td> ";
}
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
You should use ' instead of " for your string in php, because you have the " used in the html markup and the " you use for echo.

Can someone please explain why my code below doesn't work when i hit the submit button?

It does not display the selected year from the drop down menu - which is what I am trying to accomplish. It prints up to "You choose" then it doesnt print the year. which is what i want to display.
<html>
<head><title> Hello </title>
<?php
//code for connection
include 'connect.php';
?>
</head>
<body>
<center>
<br>
<b>welcome</b>
<br>
<?php
$sql1 = "SELECT DISTINCT Year FROM dbnames ORDER BY Year";
$runsql1 = mysql_query($sql1);
if (!$runsql1)
{
die( "Could not execute sql:" . mysql_error());
}
while($row = mysql_fetch_array($runsql1))
{
$options .="<option>".$row['Year']."</option>";
}
$years = "<form id='filter1' name='filter1' method='post' action=''>
<select name='filter2' id='filter2'>".$options."</select></form>";
echo "Please choose an year".$years;
?>
<!-- submit button -->
<p><input type="submit" value="Submit"></p>
<?php
echo 'You choose'. $_POST[filter2];
?>
</center>
</body>
</html>
Try something like this:
<?php
// Connect to DB
include 'connect.php';
// Load Select Options
$year_results = mysql_query("SELECT DISTINCT Year FROM dbnames ORDER BY Year ASC") or die(mysql_error());
$year_options = array();
while ($row = mysql_fetch_assoc($year_results)) {
$year_options[] = '<option value="'. $row['year'] .'">'. $row['year'] .'</option>';
}
// Handle Post/Selected Year
$selected_year = (!empty($_POST['filter2']) ? $_POST['filter2'] : 'No Year Selected');
?>
<html>
<head>
<title> Hello </title>
</head>
<body>
<center>
<br>
<b>welcome</b>
<br>
<form id='filter1' name='filter1' method='post' action=''>
Please choose an year: <select name='filter2' id='filter2'>
<?php echo implode("\r\n", $year_options) ?>
</select>
<input type="submit" value="Submit">
</form>
You choose: <?php echo $selected_year ?>
</center>
</body>
</html>
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Unable to change database

My aim to is to Update Value in Database By using Update Query . On my first page i have just displayed database table in webpage. Then by using hyperlink i have to click on Edit to second page "edit.php".While on first page i have to get the value of id and send it to second page. Where a input form is displayed which gets Value casually but Id through hidden tag. On third page getting the values query is implented but the value of id is missing.
First Page
<html>
<head>
<title>Assignment</title>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","");
// Check connection
if (!mysql_connect()) {
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$db=mysql_select_db("assignment",$con);
$result = mysql_query("SELECT * FROM teacher ",$con);
?><table cellpadding="2px" border="2px"><?php
while($row = mysql_fetch_array($result)) {
?> <tr>
<td><a href="edit.php?id=<?php
echo $row['id']; ?>">Edit</a > Delete
</td><td>
<?php
echo $row['id']; ?></td><td> <?php echo $row['name'];?></td><td><?php echo $row['program']; ?></td>
<?php }
?></table><?php
mysql_close($con);
?>
</body>
</html>
Secnod Page edit.php
<html>
<head>
<title>Assignment Edit</title>
</head>
<body>
<?php
$id = $_GET['id'];
?>
<form action="update.php" method="get">
Address <input type="text" name="program"><br>
<input type="hidden" name="id" value='<?php $id?>'>
<input type="submit" name="submit">
</form>
</body>
</html>
Third Page update.php
<html>
<head>
<title>Update Page</title>
</head>
<body>
<?php
$add=$_GET['program'];
$id=$_GET['id'];
$con=mysql_connect("localhost","root","");
// Check connection
if (!mysql_connect()) {
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$db=mysql_select_db("assignment",$con);
$query = "UPDATE teacher SET program='$add' WHERE id =".$id;
echo $query;
$result = mysql_query($query,$con);
/* while($row = mysql_fetch_array($result)) {
echo $row['id'] ." " . $row['name']." ". $row['address']."<br>";
}
mysql_close($con);
*/
?>
</body>
</html>
output
UPDATE teacher SET program='openSource' WHERE id =
you need to change this
<input type="hidden" name="id" value='<?php $id?>'>
to
<input type="hidden" name="id" value='<?php echo $id?>'>
(or)
<input type="hidden" name="id" value='<?=$id?>'>

add a jquery date picker to php list

I have a mysql database table which contains a error code, date and mail address.
My script below displays a basic list as per the screenshot,
I would like to be able to filter by date, I am hoping to use jquery date picker.
The idea being, only show entries where the date matches that in the jquery date picker.
The php code used to display the list:
<?php
// Add Logo
$image = "logo.png";
$width = 300;
$height = 280;
echo '<img src="'.$image.'" style=width:"' . $width . 'px;height:' . $height . 'px;">';
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("pmta_reporting") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM address")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Error</th> <th>Date</th> <th>Mail Address</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['code'];
echo "</td><td>";
echo $row['date'];
echo "</td><td>";
echo $row['address'];
echo "</td></tr>";
}
echo "</table>";
// disconnect from the database
mysql_close();
?>
The code I am using for the date picker is
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" /></p>
</body>
</html>
How can I add the jquery date picker to the script so that when a user selects a date the results shown on the page only display the date selected by the user?
On value change in datepicker field, reload url with GET variable date:
$( "#datepicker" ).change(function(){
window.location.href = window.location.href + '?date=' + $(this).val();
})
in php, if get variable is suplied add a where statement to the query:
$query = "SELECT * FROM address"
if (isset( $_GET['date']) && ($date = $_GET['date'])){
$query .= " WHERE date = '$date'";
}
$result = mysql_query($query)
NOTE! this wont protect against sql injection!
This would be a bit complicated to write up, so forgive me for not writing an example, but I'll give you a good idea of how to do this. First of all, you should make a separate PHP file that returns only the table and takes a POST variable argument for the date that it uses to filter the results in the SQL query. Next, use jQuery's .change() on the input field that is the datepicker to make an $.ajax call with $('#datepicker').val() set in the data argument to the PHP file that returns your data and load it into a specified <div>.
You can read more about $.ajax here: http://api.jquery.com/jQuery.ajax/
Might not be very efficient, but you can do this.
$("#date").datepicker({"dateFormat": "yy-mm-dd"});
$("#date").change(function() {
var date_from_date_picker = $(this).val();
$('td.date').each(function() {
if ($(this).text() != date_from_date_picker) {
$(this).parent().hide();
} else {
$(this).parent().show();
}
});
});
Working demo at http://jsfiddle.net/djhPN/2/
In your HTML:
<body>
<form method="get" action="yourphpscript">
<p>Date: <input type="text" name="date" id="datepicker" /></p>
<input type="submit" value="Search" />
</form>
</body>
In your PHP you can use PDO or mysqli, that way you can use prepared statements and parameterized queries that protect you against SQL-injection.
Check out this post for more information:
Examples of PDO & mysqli
You could also escape the bad sql with the function "mysql_real_escape_string($bad_variable)"
I'll just adjust the code of Joel Harkes to make it work against SQL-injection also:
$query = "SELECT * FROM address"
if (isset( $_GET['date']) && ($date = mysql_real_escape_string($_GET['date']))){
$query .= " WHERE date = '$date'";
}
$result = mysql_query($query)
You want to use ajax to load the content like this.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
jQuery(document).ready(function() {
$("#datepicker").change(function(){
var new_date = $(this).val();
jQuery.ajax({
type: "POST",
url: "http://www.url.php",
data: { date:new_date },
success: function( data ) {
$('#displaycontent').val(data);
}
});
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" /></p>
<div id="displaycontent"> </div>
</body>
</html>
and ajax file is ex. url.php is like this.
// Add Logo
$image = "logo.png";
$width = 300;
$height = 280;
echo '<img src="'.$image.'" style=width:"' . $width . 'px;height:' . $height . 'px;">';
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("pmta_reporting") or die(mysql_error());
$newdate = $_REQUEST['date'];
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM address WHERE date=".$newdate) or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Error</th> <th>Date</th> <th>Mail Address</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['code'];
echo "</td><td>";
echo $row['date'];
echo "</td><td>";
echo $row['address'];
echo "</td></tr>";
}
echo "</table>";
// disconnect from the database
mysql_close();
?>
Ok made some changes. I have created 2 files in the directory.
index.php - this contains the date picker and a submit
file2.php - this contains the database query and tables.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker(({ dateFormat: "yy-mm-dd" }));
});
</script>
</head>
<body>
<form action="file2.php" method="post">
<p>Date: <input type="text" name="datepicker" id="datepicker" /></p>
<div id="displaycontent"> </div>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Then file2.php looks like this:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error());
$newdate = $_POST['datepicker'];
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM table_name WHERE date='$newdate'") or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Error</th> <th>Date</th> <th>Mail Address</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['code'];
echo "</td><td>";
echo $row['date'];
echo "</td><td>";
echo $row['address'];
echo "</td></tr>";
}
echo "</table>";
// disconnect from the database
mysql_close();
?>
This allows me to filter by date.
Thanks everyone for your contributions

Form not recognizing post variables

I have a simple html form and some php that input the POST variables into a mysql database. However, I noticed that the form would not input the data when
if (isset($_POST['submit'])){
insert stuff in here
}
was included. I then removed the if statement above and ran the code. All of the variables were imputed except the ones from the form using POST (ex $_POST['var1']). It seems like the POST variables are not being recognized and I dont know what's wrong.
ALL CODE:
<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
<?php include '../nav.php';
?>
<?php
if (isset($_POST['submit'])){
include '../connect.php';
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
$put=mysql_query("INSERT INTO questions VALUES ('','$question','$detail','$date','$time','$user','subject','0')");
$result=mysql_query("SELECT * FROM questions WHERE user='$user' AND time='$time'");
while ($row = mysql_fetch_assoc($result)){
$q=$row['id'];
}
}
?>
<form method='POST' action='question.php?q=<?php echo $q ?>'>
<p>Question:</p>
<p><input type='text' name='question' id='question' maxlength='200'></p>
<p>Add some detail (optional):</p>
<p><textarea id='detail' name='detail' ></textarea></p>
<p>Tags:</p>
<p><input type='submit' value='submit' name='submit'></p>
</form>
</div>
<?php include '../footer.php'; ?>
</body>
</html>
TESTPAGE:
<?php
include 'connect.php';
if (isset($_POST['submit'])){
$hhh=mysql_real_escape_string($_POST['hhh']);
$put=mysql_query("INSERT INTO questions VALUES ('','$hhh','','','','','','')");
}
?>
<form action='test.php' method='post'>
<input type='text' name='hhh'>
<input type='submit' name='submit' value='submit'>
</form>
You have two PHP pages - ask.php and question.php. I (guess)think the ask.php is used to store questions and other details and you want to open a question.php with question id.
ask.php
<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
<?php
include '../nav.php';
/*--- If submit button is pressed ---- */
if (isset($_POST['submit']))
{
include '../connect.php';
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
/* SELECT column names you want to use with INSERT statement */
$put=mysql_query("INSERT INTO questions
(`question`,`detail`,`date`,`time`,`user`,`subject`,`name_of_last_col` )
VALUES
('$question','$detail','$date','$time','$user','subject','0')");
//for debug purpose
if($put)
{
echo "Record added";
}
else
{
echo "Can't add record " . mysql_error();
}
}
/*----- End submit block -----------*/
/*----List the questions and select it----------*/
$time=time();
$user=$_SESSION['id'];
//I think this wont work. Try to remove time comparison from the SELECT statement.
$result=mysql_query("SELECT * FROM questions WHERE `user`='$user' AND `time`='$time'");
//$result=mysql_query("SELECT * FROM questions WHERE `user`='$user'");
if($result)
{
echo "<table>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>$row[question]</td>";
echo "<td><a href='question.php?qid=$row[id]'>Show a question</a></td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No questions!!!";
}
?>
<form method='POST' action="ask.php">
<p>Question:</p>
<p><input type='text' name='question' id='question' maxlength='200'></p>
<p>Add some detail (optional):</p>
<p><textarea id='detail' name='detail' ></textarea></p>
<p>Tags:</p>
<p><input type='submit' value='submit' name='submit'></p>
</form>
question.php should be:
<?php
$qid=$_GET["qid"];
echo "$qid is selected...";
?>
You may split code into two PHP pages - one for save records and another to list and select rows.
addquestion.php
<?php
session_start();
if(isset($_POST['submit']))
{
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("your_db_name") or die(mysql_error());
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
$put=mysql_query("INSERT INTO questions
(`question`,`detail`,`date`,`time`,`user`,`subject`,`name_of_last_col` )
VALUES
('$question','$detail','$date','$time','$user','subject','0')");
//for debug purpose
if($put)
{
echo "Record added";
}
else
{
echo "Can't add record " . mysql_error();
}
}
?>
<form method='post' action="addquestion.php">
<p>Question:</p>
<p><input type='text' name='question' id='question' maxlength='200'></p>
<p>Add some detail (optional):</p>
<p><textarea id='detail' name='detail' ></textarea></p>
<p>Tags:</p>
<p><input type='submit' value='submit' name='submit'></p>
</form>
questionlist.php
<?php
session_start();
$user=$_SESSION['id'];
mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("your_db_name") or die(mysql_error());
$result=mysql_query("SELECT * FROM questions WHERE `user`='$user'");
if($result)
{
echo "<table>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>$row[question]</td>";
echo "<td><a href='question.php?qid=$row[id]'>Show a question</a></td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No questions!!!";
}
?>

Categories