Form is failing to update mysql table - php

So I'm currently working with a form where the admin can select multiple users from the database via a tickbox system, then change the welcome message or general message to a client when they log in:
<?php
session_start();
include_once("isadmin.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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update Client Message</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
<form id="updateform" name="updateform" method="post" action="updateexec.php">
<table width="500" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th width="200">Select User</th>
<td>
<?php
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$useruploadids = mysql_query("SELECT member_id, firstname, lastname FROM members");
while ($row = mysql_fetch_assoc($useruploadids)) {
$userid = $row['member_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
?>
<input type="checkbox" name="userid_<?php echo $userid ?>" value="y" /><?php echo $firstname ?><?php echo $lastname ?><br />
<?php } ?>
</td>
</tr>
<tr>
<th>Message For Client </th>
<td>
<textarea input name="otherdeets" type="textarea" class="textfield" id="otherdeets" style="width: 356px; height: 176px">
</textarea>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Update" /></td>
</tr>
</table>
</form>
</body>
</html>
So this is the form, and it works fine, it calls all users from the database and displays them in tickbox fasion.
I can only assume my issue is in the exec script:
<?php
echo( "<pre>" );
print_r( $_POST );
echo( "</pre>" );
include ("config.php");
$tbl_name="members";
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//This gets all the other information from the form
$update = $_POST['otherdeets'];
$id = $_POST['userid'];
// Cycle through each member and check that it needs to be added to the db
$useruploadids = mysql_query( "SELECT member_id FROM members" );
while ($row = mysql_fetch_assoc($useruploadids))
{
// Check that the member was sent from the last form
if( isset( $_POST['userid_'.$row['member_id']] ) && $_POST['userid_'.$row['member_id']] == "y" )
{
// update data in mysql database
$sql="UPDATE $tbl_name SET otherdeets='$update' WHERE id='$id'";
$result=mysql_query($sql);
}
}
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='admin-welcome.php'>Admin Home</a>";
}
else {
echo "ERROR";
}
?>
When I run the script it simply says:
Array
(
[userid_1] => y
[otherdeets] => Blah Blah
[Submit] => Update
)
ERROR
Any idea what is wrong? Knowing my luck it would probabaly be a spelling mistake
Thank you

Hey your query is wrong there should be where condition with appropriate column name i.e. "member_id"
And one more thing
you are fetching the $id = $_POST['userid']; which is an error as there is no value exist with that key
rather you do in the if condition before doing the update query, i.e.
$id = $POST['userid'.$row['member_id']];

Related

How can i insert html table data into sql database using php?

This is my table html code. I tried sending the data using the normal insert but it only sends the last row data. I don't know how to send the full data . Can someone please help me with this.
<form action="admin_schedule_employee.php" id="schedule_employee" method="post" >
<input type="date" class="input-sm" name="scheduledate" style="margin:10px;">
<table class="table-responsive table table striped table-bordered">
<thead>
<tr>
<th style="width:20%">Employee First Name</th>
<th style="width:20%">Employee ID</th>
<th style="width:20%">Start Time</th>
<th style="width:20%">End Time</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)): ?>
<tr>
<td><input disabled name="employeename" type="text" value="<?php echo $row['fname']; ?>"></input></td>
<td><input disabled name="employeeid" type="number" value="<?php echo $row['employee_id']; ?>"></input></td>
<td><input name="starttime" type="time"></td>
<td><input name="endtime" type="time"></td>
</tr>
<?php endwhile; ?>
</thead>
<tbody>
</tbody>
</table>
<input type="submit" name="Schedule" value="Schedule">
</form>[This is how my table look like i want to send the whole data to sql database using php][1]
To start with, you will need to create multiple pages:
form.php
process.php
done.php
Creating your user form is simple, place the table in form tags like you have done above, here is an example. Save this page as form.php
<form id="new record" action="process.php" method="POST">
<table width="500px">
<tr>
<td width="50%">
<input type="text" name="fname" id="fname">
</td>
<td width="50%">
<input type="text" name="lname" id="lname">
</td>
</tr>
<tr>
<td width="50%">
</td>
<td width="50%">
<input type="submit" value="Add Record">
</td>
</tr>
</table>
</form>
Next, you will need to create a page which can process this data, and add it to your mysql database. For the following example, I have omitted my database details and substituted them, but you should add your own.
For this example, imagine my database has a table with only an fname and an lname column.
<meta http-equiv="refresh" content="0; url=/done.php" />
<?php
$servername = "your_server_name";
$username = "mysql_username";
$password = 'mysql_password';
$dbname = "database_name";
$fname = $_GET['fname'];
$lname = $_GET['lname'];
try {
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO online (fname, lname)
VALUES ('$fname', '$lname')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record inserted";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
Hopefully, that will work to insert the record. Now we need a table on the done.php page which can display all the records in the database. Use the following code:
<html lang="en">
<head>
<meta http-equiv="refresh" content="5; url=/done.php" />
<meta charset="utf-8" />
<title></title>
</head>
<body>
<?php
$servername = "your_server_name";
$username = "mysql_username";
$password = 'mysql_password';
$dbname = "database_name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * from table_name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["fname"]. ": ";
echo $row["lname"]. "<br /><br />";
}
} else {
echo "No messages";
}
mysqli_close($conn);
?>
</body>
</html>
Hopefully this will work for you.

How do I update a specific row in a table using php form?

Please bear with me, I'm not familiar yet with the language. I have a table that lists an applicant record such as applicant number, name and status. I want to update an applicant status either 'hired' or 'failed' on a specific row using a PHP form. However, I'm not sure how to get the specific submit name on its row upon submission. Or if you have a workaround I would appreciate that. Thank you so much for your help.
<!DOCTYPE html>
<html>
<h2>Applicant Records</h2>
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password ="";
$mysql_database = "applicantrecord";
// Create connection
$conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqli = "SELECT id, firstname, lastname, status FROM applicant";
$result = $conn->query($sqli);
if ($result->num_rows > 0) { ?>
<table class="table">
<thead>
<tr>
<th>Applicant No.</th>
<th>Lastname</th>
<th>Firstname</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<?php
// output data of each row
echo "<tbody>";
while($row = $result->fetch_assoc())
{ ?>
<tr>
<td>
<?php echo $row["id"];
$appid = $row["id"];
?>
</td>
<td>
<?php echo $row["lastname"]; ?>
</td>
<td>
<?php echo $row["firstname"]; ?>
</td>
<td>
<?php echo $row["status"]; ?>
</td>
<td>
</td>
<td>
<div>
<form action="" role="form" method="post" name="form<?php echo $appid; ?>">
<select name="applicant_status">
<option value="Hired">Hire</option>
<option value="Failed">Fail</option>
</select>
</p>
<button type="submit" class="btn btn-default" name = "submit<?php echo $appid; ?>" data-dismiss="modal">Submit</button>
</form>
<?php
if(isset($_POST["submit"])){
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$newappid = $appid;
$newapptstatus = $_POST['applicant_status'];
$connect = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $connect ) {
die('Could not connect: ' . mysql_error());
}
$sql_sub = "UPDATE applicant ". "SET status = '$newappstatus'".
"WHERE id = '$newappid'" ;
mysql_select_db('applicantrecord');
$retval = mysql_query( $sql_sub, $connect );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
echo "<script type= 'text/javascript'>alert('An error occured! Applicant status update failed!');</script>";
}
echo "<script type= 'text/javascript'>alert('Applicant status updated successfully!');</script>";
mysql_close($connect);
}
?>
</div>
</td>
</tr>
<?php }
echo "</tbody>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</html>
In your if statement where you check that $_POST['submit'] is set, the index 'submit' does not exist. Thus isset($_POST['submit']) evaluates to false and your query to update the table is never being executed.
The variable $appid is being changed with each row that is added, so when the page is done loading and the submit button is pushed on a certain row, $appid won't necessarily contain the correct row number.
To get around this, you could use a hidden input in your form:
<input name="id" value="<?php echo $appid ?>" type="hidden">
Then you can replace isset($_POST['submit']) with isset($_POST['id']) and set $newappid = $_POST['id'] to get the row number to be changed.

Search with pagination [duplicate]

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;

selection of query using multiple selection

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

can someone please help me with this update table

I would like to update my database table with first name and last name where email is equal to session email but I cant update the table where the email is equal to the signed in email of the user please help me.
<?php
session_start();
if(!session_is_registered(email)){
header("location: login.html");
}?>
<?php
echo"<a href = logout.php> Logout </a>";
?>
<?php
include('config.php');
session_start();
if(isset($_SESSION['email'])) {
echo "Welcome ".$_SESSION['email']."";
}
?>
<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql="UPDATE nametable SET fname='$fname', lname='$lname' WHERE email='" . $_SESSION ['email'] . "'";
echo $row['fname']." - ".$row['lname']. "<br />";
if($result) {
echo "success";
} else {
echo "no success";
}
mysql_select_db('db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
?>
this id the data that is going to be submitted depending on the first name and last name that is is going to be updated don't worry about the other tags I just put this together to get an idea of what is going to be updated on the above code please tell me what I did wrong
<html>
<head>
<title>Update a Record in MySQL Database</title>
</head>
<body>
<form method="post" action="update1.php">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100"> first name</td>
<td><input name="fname" type="text" id="fname"></td>
</tr>
<tr>
<td width="100">last name</td>
<td><input name="lname" type="text" id="lname"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="submit" type="submit" id="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
First, you need qoutes "" around strings in your SQL
$sql="UPDATE nametable SET fname='$fname', lname='$lname' WHERE email='" . $_SESSION ['email'] . "'";
should be
$sql='UPDATE nametable SET fname="'.$fname.'", lname="'.$lname.'" WHERE email="'.$_SESSION['email'].'"';
next, you are not executing the query at all. Think you in the following
echo $row['fname']." - ".$row['lname']. "<br />";
if($result) {
echo "success";
} else {
echo "no success";
}
really mean :
$result = mysql_query($sql);
if (mysql_affected_rows()>-1) {
// success
// do here what you want
}
I think session_is_registered function is already deprecated so instead of
session_is_registered('email')
You might want to use:
if ( isset( $_SESSION['email'] ) ){}
and in one page of php you can only have 1 session_start() function.
If it's still not working, try to debug your code by printing out the $sql variable.
Let me know if that help.

Categories