I'm trying to create a page which uses session data to find a user in a database and then sends the events that this user has signed up to. I'm a bit of a newbie and have got very confused with where I am at. I am using two different tables to get the data, and this is where I'm getting confused and where I believe the errors are occurring. Thanks in Advance.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
$username = $_SESSION['username'];
$email = $_SESSION['user_email'];
$con=mysqli_connect("localhost","emuas","******","EMUAS_signUp");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<table>
<tr>
<td> Logged in as:</td>
</tr>
<tr>
<th>" . $username . "</th>
</tr>
<tr>
<td>
<form action='logout.php' method='post'>
<input type='submit' value='Logout' >
</form>
</td>
</tr>
<tr>
<th>Events Attending:</th>
</tr>";
$find = mysqli_query($con,"SELECT * FROM SIGN_UP_TEST WHERE User = '$username'");
while($find_row = mysqli_fetch_array($find)){
//Get Event ID
$eventId = $find_row['EventID'];
//Use Event ID to get Event Name
$result = mysqli_query($con,"SELECT * TEST WHERE EventID = '$eventId'");
//Insert Event Name into table with link from Page Name
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <a href='http://www.emuas.co.uk/members/sign_up_sheets/S" . $row['PageName'] . ".php'>" . $row["EventName"] . "</a> </td>";
echo "</tr>";
}
}
echo "</table>";
?>
<body>
</body>
</html>
Related
I'm extracting data from a database with a SELECT statement.
I would like to put this data into something, which you can write in (and later put a button save, which uses a SQL statement to rewrite the rows data).
The current code is:
index.php with login logic
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="logik.php" method="POST">
Username: <input type="text" name="uname" />
Password: <input type="password" name="pwd" />
DB-Name: <input type="text" name="dbname" value="unternehmendb" />
<input type="submit" />
</body>
</html>
After the login the SQL logic:
logik.php
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="grafik.css">
<title>unternehmendb</title>
</head>
<body>
<h1>Mitarbeiter</h1>
</body>
</html>
<?php
//test2
$servername = "localhost";
$username = $_POST['uname'];
$pass = $_POST['pwd'];
$dbname = $_POST['dbname'];
// Create connection
$link = new mysqli($servername, $username, $pass, $dbname);
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
echo "<table>";
$sql = "SELECT * FROM mitarbeiter";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Name"]. "</td><td>" . $row["Vorname"]. "</td><td> " . $row["Strasse"]. "</td><td>" . $row["Position"] . "</td><td>" . $row["id"] . " </td></tr> ";
}
} else {
echo "0 results";
}
echo "</table>";
mysqli_close($link);
?>
You're on track but instead of echoing result in td only, you can add text input fields making the values for the name attribute an array since you're getting multiple rows.
Everything within this
if($result->num_rows > 0)
block should be changed to:
if ($result->num_rows > 0)
{
//output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>
<td><input type='text' name='Name[]' value='".$row['Name']."' />
</td>
<td><input type='text' name='Vorname[]'
value='".$row['Vorname']."' /> </td>
<td><input type='text' name='Strasse[]'
value='".$row['Strasse']."' /> </td>
<td><input type='text' name='Position[]' value='".
$row['Position']."' /> </td>
<td><input type='text' name='id[]' value='".$row['id']."' />
</td>
</tr> ";
} //while()
?>
<tr> <td colspan="5"><center> <input type="submit" value="Save Data" />
</center></td></tr>
<?php
}// if result rows > 0
?>
Notice that after the loop, we created an additional row to house the
submit button.
Assuming there are no records, you could surround your statement in the else with since it will be printed within the tag as in:
echo "<tr> <td colspan='5'> No results Found </td> </tr>";
Hope this helps.
I'm trying to edit and update a row using PHP.
Here is the code:
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<div class="header">
<?php include 'header.php';?>
</div>
<center>
<?php
/*
VIEW.PHP
Displays all data from 'players' table
*/
// connect to the database
include_once('../connection.php');
// get results from database
$query = "SELECT EmployeeName, DOB, Age, StreetAddress, City, State, ZipCode,
Email, HomePhone, Wireless, JobTitle, id, HomeDept, Manager FROM headcount ORDER BY `headcount`.`EmployeeName` ASC";
$response = #mysqli_query($dbc, $query);
// display data in table
echo "<p><b>View All</b> | <a href='../employees/rides.php'>Rides</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Employee Name</th> <th>Home Department</th> <th>Job Title</th> <th>Edit</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array($response)){
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['EmployeeName'] . '</td>';
echo '<td>' . $row['HomeDept'] . '</td>';
echo '<td>' . $row['JobTitle'] . '</td>';
echo '<td>Edit</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
</center>
edit.php
<?php
mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db("hwss") or die(mysql_error());
$UID = (int)$_GET['ID'];
$query = mysql_query("SELECT * FROM headcount WHERE id = '$UID'") or die(mysql_error());
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$EmployeeName = $row['EmployeeName'];
$DOB = $row['DOB'];
$Age = $row['Age'];
$email = $row['email'];
}
?>
<form action="update.php" method="post">
<input type="hidden" name="ID" value="<?=$UID;?>">
email: <input type="text" name="ud_email" value="<?=$email;?>"><br>
Name: <input type="text" name="ud_EmployeeName" value="<?=$EmployeeName?>"><br>
DOB: <input type="text" name="ud_dob" value="<?=$DOB?>"><br>
Age: <input type="text" name="ud_Age" value="<?=$Age?>"><br>
<input type="Submit">
</form>
<?php
}else{
echo 'No entry found. Go back';
}
?>
</body>
</html>
When I click edit it says "No entry found" with the back link on each ID url. I understand that I don't have any Update code yet. I'm simply just trying to get it to display the data before adding the rest.
Error Message
Notice: Undefined index: ID in C:\xampp\htdocs\Scheduling\Employees\edit.php on line 5
No entry found. Go back
Use: $_GET['id']
$UID = (int)$_GET['id'];
instead of
$UID = (int)$_GET['ID'];
This question already exists:
How can i edit the search and pagination because i don't want it to be echoed?
Closed 7 years ago.
I have this search with pagination, the search function is working fine and the pagination is also working fine but my real problem is that I don't know how to merge those two. Every time I try to search, the search result is showing but the pagination is not functioning normal. Please someone help me about this issue. I don't know where to start.
I have this code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("region_survey", $con);
$sql="SELECT * FROM municipality";
if(isset($_POST['search'])){
$search_term=mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE province_id LIKE '%{$search_term}%' ";
}
$query=mysql_query($sql) or die (mysql_error());
?>
<form name="search_form" method="POST" action="">
Search:<input type="text" name="search_box" value="" />
<input type="submit" name="search" value="search the table" />
</form>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//Include the PS_Pagination class
include('ps.php');
//Connect to mysql db
$conn = mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('region_survey', $conn);
if(!$status) die("Failed to select database!");
$sql = 'SELECT * FROM municipality';
//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 15, 17);
//The paginate() function returns a mysql result set for the current page
$rs = $pager->paginate();
?>
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//Count total number of records after search
mysql_select_db("region_survey", $con);
if(isset($_POST['search'])){
$search_term=mysql_real_escape_string($_POST['search_box']);
$sql="SELECT * FROM municipality WHERE province_id LIKE '%$search_term%'";
}
$result = mysql_query($sql, $con);
$row = mysql_num_rows($result);
echo "Total Number: ";
echo $row;
?>
<table border="1" cellpadding="0" cellspacing="0" id="resultTable">
<tr>
<th> <strong>ID</strong> </th>
<th> <strong>Province ID</strong> </th>
<th> <strong>Municipality Name</strong> </th>
</tr>
<?php
while($row = mysql_fetch_array($query))
{
?>
<tr>
<td> <?php echo $row["id"]; ?> </td>
<td> <?php echo $row["province_id"]; ?></td>
<td> <?php echo $row["municipality_name"]; ?></td>
<td><input name="selector[]" type="checkbox"
id="checkbox[]" value="<?php echo $row['id'];?>"></td>
<td>Edit </td>
</tr>
<?php
}
?>
</table>
<?php
//Display the navigation
//echo $pager->renderFullNav();
echo '<div style="text-align:center">'.$pager->renderFullNav().'</div>';
?>
</body>
</html>
Pages will have URL like example.com/search/term/?page=2 (or example.com/search.php?term=xyz&page=2, doesn't matter if you redirect URLs or not).
In PHP then will be something like:
$pagelimit = 10; // records per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // set current page
And LIMIT with OFFSET in your SQL query:
$sql = "SELECT *
FROM municipality
WHERE province_id LIKE '%$search_term%'
LIMIT " . ($page - 1) * $pagelimit . ", " . $pagelimit;
I am beginner to php.I want to select the department from the options and after the selection of the department, I want to display the roll no in the next drop down box belong to that department. Help me by providing some ideas related to my questions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Untitled Document</title>
</head>
<body>
<?php include('C:\wamp\www\fms\background.php'); ?>
<?php include('C:\wamp\www\fms\adminmenu.php'); ?>
<?php
if (isset($_POST['delete'])) {
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$cid = $_POST['cid'];
$sql = "DELETE from addpassenger WHERE rno='$rno'";
mysql_select_db('fms');
$retval = mysql_query($sql, $con);
if (!$retval) {
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($con);
} else {
?>
<p></p>
<p></p>
<p></p>
<p></p>
<center>
<form method="post">
<table width="344" border="0" cellspacing="1" cellpadding="2">
<tr>
<td>SELECT THE DEPARTMENT</td>
<td><?php
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = #mysql_select_db("fms", $con) or die(mysql_error());
$str = "select dept from addpassenger";
$res1 = #mysql_query($str);
echo '<select name="dept">';
echo '<option selected="----------"></option>';
while ($row = mysql_fetch_array($res1)) {
echo '<option value="' . $row['dept'] . '">' . $row['dept'] . '</option>';
}
echo '</select>';
?></td>
</tr>
<tr>
<td width="208">SELECT THE ROLL NO</td>
<td width="125">
<?php
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = #mysql_select_db("fms", $con) or die(mysql_error());
$str = "select rno from addpassenger ";
$res1 = #mysql_query($str);
echo '<select name="rno">';
echo '<option selected="----------"></option>';
while ($row = mysql_fetch_array($res1)) {
echo '<option value="' . $row['rno'] . '">' . $row['rno'] . '</option>';
}
echo '</select>';
?>
</select>
</td>
</tr>
<tr>
<td width="208"></td>
<td></td>
</tr>
<tr>
<td width="208"></td>
<td>
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</center>
<?php
}
?>
</body>
</html>
select rno from addpassenger
needs to have a where clause which selects based on previous selection
Warning this is lenghty! attack if you knowledagble. well at least more then a newb beginner like me.
This script uses three files as detailed below. It is suppoed to create the database and fields from the form input. It gets to the end and shows my_contacts has been created!. But when i go into phpMyadmin the table has not been created.
I have a file named show_createtable.html which is used to create a table in MySQL
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Step 1: Name and Number</h1>
<form method="post" action="do_showfielddef.php" />
<p><strong>Table Name:</strong><br />
<input type="text" name="table_name" size="30" /></p>
<p><strong>Number of fields:</strong><br />
<input type="text" name="num_fields" size="30" /></p>
<p><input type="submit" name="submit" value="go to step2" /></p>
</form>
</body>
</html>
This Form Posts to do_showfielddef.php
<?php
//validate important input
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
header( "location: show_createtable.html");
exit;
}
//begin creating form for display
$form_block = "
<form action=\"do_createtable.php\" method=\"post\">
<input name=\"table_name\" type=\"hidden\" value=\"$_POST[table_name]\">
<table cellspacing=\"5\" cellpadding=\"5\">
<tr>
<th>Field Name</th><th>Field Type</th><th>Table Length</th><th>Primary Key?</th><th>Auto-Increment?</th>
</tr>";
//count from 0 until you reach the number fo fields
for ($i = 0; $i <$_POST[num_fields]; $i++) {
$form_block .="
<tr>
<td align=center><input type=\"texr\" name=\"field name[]\"
size=\"30\"></td>
<td align=center>
<select name=\"field_type[]\">
<option value=\"char\">char</option>
<option value=\"date\">date</option>
<option value=\"float\">float</option>
<option value=\"int\">int</option>
<option value=\"text\">text</option>
<option value=\"varchar\">varchar</option>
</select>
</td>
<td align=center><input type=\"text\" name=\"field_length[]\" size=\"5\"></td>
<td aligh=center><input type=\"checkbox\" name=\"primary[]\" value=\"Y\"></td>
<td aligh=center><input type=\"checkbox\" name=\"auto_increment[]\" value=\"Y\"></td>
</tr>";
}
//finish up the form
$form_block .= "
<tr>
<td align=center colspan=3><input type =\"submit\" value=\"create table\">
</td>
</tr>
</table>
</form>";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create a database table: Step 2</title>
</head>
<body>
<h1>defnie fields for <? echo "$_POST[table_name]"; ?>
</h1>
<? echo "$form_block"; ?>
</body>
</html>
Which in turn creates the table and fields with this file do_showfielddef.php
//connect to database
$connection = #mysql_connect("localhost", "user", "pass")
or die(mysql_error());
$db = #mysql_select_db($db_name, $connection)
or die(mysql_error());
//start creating the SQL statement
$sql = "CREATE TABLE $_POST[table_name](";
//continue the SQL statement for each new field
for ($i = 0; $i < count($_POST[field_name]); $i++) {
$sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
if ($_POST[auto_increment][$i] =="Y") {
$additional = "NOT NULL auto_increment";
} else {
$additional = "";
}
if ($_POST[primary][$i] =="Y") {
$additional .= ", primary key (".$_POST[field_name][$i].")";
} else {
$additional = "";
}
if ($_POST[field_length][$i] !="") {
$sql .= " (".$_POST[field_length][$i].") $additional ,";
} else {
$sql .=" $additional ,";
}
}
//clean up the end of the string
$sql = substr($sql, 0, -1);
$sql .= ")";
//execute the query
$result = mysql_query($sql, $connection) or die(mysql_error());
//get a giid message for display upon success
if ($result) {
$msg = "<p>" .$_POST[table_name]." has been created!</p>";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create A Database Table: Step 3</title>
</head>
<body>
<h1>Adding table to <? echo "$db_name"; ?>...</h1>
<? echo "$msg"; ?>
</body>
</html>
I cant believe I went to all the trouble of wrinting this Question. I had another good look at the phpMYAdmin and it had worked. The table had been created under a database called testDB which I assumed had nothing it in.
How did the script decided to etner this as a child under the testDB database?
Once again thanks everyone for your input, This site is truely amazine and is so valuable for a beginner like my self.