I've got a PHP form that displays current data on one side of the page and on the other side are fields that can be filled in to update the data. I have two files "modify.php" and "modify.config.php" to process the update. When hitting the "save" button on modify.php I get a success message (through the config page); the record id (site_id) passes through to the success message in the url but the data does not update in the mysql database. Any help is greatly appreciated!
Modify.php:
<?php
if(isset($_GET['id'])) {
require_once 'includes/connection.php';
$id = mysqli_real_escape_string($conn, $_GET['id']);
$sql = "SELECT * FROM sites WHERE site_id='$id' ";
$result = mysqli_query($conn, $sql) or die ("Bad Query: $sql");
$row = mysqli_fetch_array($result);
}
?>
<form action='modify.config.php?id=<?php echo $_GET['id']; ?>' method='POST'>
<table width='100%'>
<thead>
<th colspan='2'><strong>Current Details:</strong></th>
<th colspan='2' style='background-color:#2c9cd4; color:#ffffff;'><strong>New Details:</strong></th>
</thead>
<tr class='hover'>
<td width='15%'><strong>Site Code: </strong></td>
<td width='35%'><?php echo $row['site_code'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='site_code' class='input2' placeholder='Site Code (UCCE)' maxlength='4' size='20' id='site_code' value='<?php if (isset($trimmed['site_code'])) echo $trimmed['site_code']; ?>' autofocus /></td>
</tr>
</tr>
<tr class='hover'>
<td width='15%'><strong>Name: </strong></td>
<td width='35%'><?php echo $row['site_name'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='site_name' class='input2' placeholder='Site Name' maxlength='100' size='50' id='site_name' value='<?php if (isset($trimmed['site_name'])) echo $trimmed['site_name']; ?>' autofocus /></td>
</tr>
<tr class='hover'>
<td width='15%'><strong>Description: </strong></td>
<td width='35%'><?php echo $row['description'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='description' class='input2' placeholder='Description' maxlength='100' size='50' id='description' value='<?php if (isset($trimmed['description'])) echo $trimmed['description']; ?>' autofocus /></td>
</tr>
<tr class='hover'>
<td width='15%'><strong>Street Address: </strong></td>
<td width='35%'><?php echo $row['address_street'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='address_street' class='input2' placeholder='Street Address' maxlength='100' size='50' id='address_street' value='<?php if (isset($trimmed['address_street'])) echo $trimmed['address_street']; ?>' autofocus /></td>
</tr>
<tr class='hover'>
<td width='15%'><strong>City: </strong></td>
<td width='35%'><?php echo $row['address_city'] ?></td>
<td colspan='2' bgcolor='#f2f2f2'><input type='text' name='address_city' class='input2' placeholder='City' maxlength='100' size='50' id='address_city' value='<?php if (isset($trimmed['address_city'])) echo $trimmed['address_city']; ?>' autofocus /></td>
</tr>
</table>
</div>
<span style='float:right;'>
<button type='submit' class='save' id='submit' name='submit'>
<img src='images/save.png' height='13px' width='13px' style='vertical-align: sub;' /> Save </button>
<input type='hidden' name='submit' id='submit' value='TRUE' />
</span>
</form>
And Modify.Config.php:
<?php
if(isset($_GET['id'])) {
require_once 'includes/connection.php';
$id = mysqli_real_escape_string($conn, $_GET['id']);
$sql = "SELECT * FROM sites WHERE site_id='$id'";
$result = mysqli_query($conn, $sql) or die ("Bad Query: $sql");
$row = mysqli_fetch_array($result);
}
include 'includes/connection.php';
if (isset($_POST['submit'])) {
$site_code = mysqli_real_escape_string($conn, trim($_POST['site_code']));
$site_name = mysqli_real_escape_string($conn, trim($_POST['site_name']));
$description = mysqli_real_escape_string($conn, trim($_POST['description']));
$address_street=mysqli_real_escape_string($conn, trim($_POST['address_street']));
$address_city = mysqli_real_escape_string($conn, trim($_POST['address_city']));
$error = false;
if (!$error) {
if (!empty($site_code) && !empty($site_name) && !empty($description) && !empty($address_street) && !empty($address_city))
{
$sql = "UPDATE sites SET site_code='$site_code', site_name='$site_name', description='$description', address_street='$address_street', address_city='$address_city' WHERE site_id='$id'";
mysqli_query($conn, $sql);
}
$page_title = "Modify Success";
$page_content = "Site <strong><a href='site.php?id={$row['site_id']}'> {$row['site_code']} {$row['description']}</a></strong> has been successfully modified.";
include "header.php";
include "includes/box_success.php";
echo "<p> <p>";
include "footer.php";
mysqli_close($conn);
exit();
} else {
$page_title="Modify - ERROR";
$page_content = "<p><strong>The site {$row['site_code']} could not be updated.</strong></p><p>{$sql}<br>{mysqli_error($conn)}</p>";
include "header.php";
include "includes/box_error.php";
echo "<p valign='middle'>
<span style='float:left'>
<a href='modify.php?id={$row['site_id']}'>
<button type='submit' class='trans-left' value='submit-previous'>
<img src='images/chevron_back.png' height='15' width='18' style='vertical-align: sub;' />
Retry
</button></a>
</span></p>
<p> </p>";
echo "</div>";
include "footer.php";
}
}
mysqli_close($conn);
?>
Are you sure you are committing the changes to the database? I see you doing an update then closing the connection. Try adding the following before you close the connection.
mysqli_commit($conn);
Related
customer_register.php
<?php
session_start();
?>
<form action="customer_register.php" method="post" enctype="multipart/form-data">
<?php
if (isset($_POST['c_name']) && isset($_POST['c_email']) && isset($_POST['c_usrname']) && isset($_POST['c_password']) && isset($_POST['c_country']) && isset($_POST['c_city']) && isset($_POST['c_contact']) && isset($_POST['c_address']) && isset($_FILES['c_image']['name'])) {
$c_ip_add = getIp();
$c_name = $_POST['c_name'];
$c_email = $_POST['c_email'];
$c_username = $_POST['c_usrname'];
$c_password = $_POST['c_password'];
$c_country = $_POST['c_country'];
$c_city = $_POST['c_city'];
$c_contact = $_POST['c_contact'];
$c_address = $_POST['c_address'];
$c_image = #$_FILES['c_image']['name'];
$c_tmp_name = #$_FILES['c_image']['tmp_name'];
$location = 'customer/customer_images/';
if (!empty($c_name) && !empty($c_email) && !empty($c_username) && !empty($c_password) && !empty($c_country) && !empty($c_city) && !empty($c_contact) && !empty($c_address) && !empty($c_image)) {
move_uploaded_file($c_tmp_name, $location.$c_image);
$select_user = "SELECT customers_username FROM customers WHERE customers_username = '$c_username'";
$run_select_user = mysqli_query($conn, $select_user);
if (mysqli_num_rows($run_select_user) == NULL) {
$insert_customer = "INSERT INTO customers(customers_ip, customers_name, customers_email, customers_username, customers_pass, customers_country, customers_city, customers_contact, customers_address, customers_image) VALUES ('$c_ip_add', '$c_name', '$c_email', '$c_username', '$c_password', '$c_country', '$c_city', '$c_contact', '$c_address', '$c_image')";
$run_insert_customer = mysqli_query($conn, $insert_customer);
$select_cart = "SELECT * FROM cart WHERE ip_add = '$c_ip_add'";
$run_select_cart = mysqli_query($conn, $select_cart);
$check_cart = mysqli_num_rows($run_select_cart);
if ($check_cart == 0) {
$_SESSION['customer_email'] = $c_email;
$_SESSION['username'] = $c_username;
echo '<script>alert("Account has been successfully created")</script>';
echo '<script>window.open("customer/my_account.php","_self")</script>';
} else {
$_SESSION['customer_email'] = $c_email;
$_SESSION['username'] = $c_username;
echo '<script>alert("Account has been successfully created")</script>';
echo '<script>window.open("checkout.php","_self")</script>';
}
} else {
echo "<div align='center' style='color:white; font-size:20px; padding:20px 0px;'><b>The Username already exists. Please try another username.</b></div>";
}
} else {
echo "<div align='center' style='color:white; font-size:20px; padding:20px 0px;'><b>All Fileds are required</b></div>";
}
}
?>
<table align="center" width="750">
<thead>
<tr align="center">
<th colspan="3"><h1>Create an Account</h1></th>
</tr>
</thead>
<tbody>
<tr>
<td id="label" align="right">Name:</td>
<td><input type="text" name="c_name" size="40" required></td>
</tr>
<tr>
<td id="label" align="right">Email:</td>
<td><input type="text" name="c_email" size="40" required></td>
</tr>
<tr>
<td id="label" align="right">Username:</td>
<td><input type="text" name="c_usrname" size="40" required></td>
</tr>
<tr>
<td id="label" align="right">Password:</td>
<td><input type="password" name="c_password" size="40" required></td>
</tr>
<tr>
<td id="label" align="right">Image:</td>
<td><input type="file" name="c_image" required></td>
</tr>
<tr>
<td id="label" align="right">Country:</td>
<td>
<select name="c_country">
<option size="50">Select Country</option>
<?php countries(); ?>
</select>
</td>
</tr>
<tr>
<td id="label" align="right">City:</td>
<td><input type="text" name="c_city" size="40" required></td>
</tr>
<tr>
<td id="label" align="right">Contact:</td>
<td><input type="text" name="c_contact" size="40" required></td>
</tr>
<tr>
<td id="label" align="right">Address:</td>
<td><input type="text" name="c_address" size="40" required></td>
</tr>
<tr align="center">
<td colspan="3"><input type="submit" name="register" value="Create Account"></td>
</tr>
</tbody>
</table>
</form>
customer_login.php
<?php
session_start();
?>
<form method="post" action="">
<table width="500" align="center" bgcolor="skyblue">
<thead>
<tr align="center">
<th colspan="4"><h2>Login or Register to Buy!</h2></th>
</tr>
</thead>
<tbody>
<tr>
<td align="right"><b>Email:</b></td>
<td><input type="text" name="email" placeholder="Enter Email"></td>
</tr>
<tr>
<td align="right"><b>Password:</b></td>
<td><input type="password" name="pass" placeholder="Enter Password"></td>
</tr>
<tr align="center">
<td colspan="4">Forgot Password?</td>
</tr>
<tr align="center">
<td colspan="3"><input type="submit" name="login" value="Login"></td>
</tr>
</tbody>
</table>
<h2 style="float:right; padding:10px;">New? Register Here</h2>
</form>
<?php
if (isset($_POST['email']) && isset($_POST['pass'])) {
$email = $_POST['email'];
$pass = $_POST['pass'];
if (!empty($email) && !empty($pass)) {
$select_id = "SELECT * FROM customers WHERE customers_email = '$email' AND customers_pass = '$pass'";
$run_select_id = mysqli_query($conn, $select_id);
foreach ($run_select_id as $details) {
$usrname = $details['customers_username'];
$id = $details['customers_id'];
}
$num_run_select = mysqli_num_rows($run_select_id);
if ($num_run_select == NULL) {
echo '<script>alert("Invalid : Email/Password combination")</script>';
exit();
}
$c_ip_add = getIp();
$select_cart = "SELECT * FROM cart WHERE ip_add = '$c_ip_add'";
$run_select_cart = mysqli_query($conn, $select_cart);
$check_cart = mysqli_num_rows($run_select_cart);
if ($num_run_select > 0 AND $check_cart == 0) {
$_SESSION['customer_email'] = $email;
$_SESSION['username'] = $usrname;
$_SESSION['id'] = $id;
echo "<script>alert('You Have Logged In Succesfully')</script>";
echo "<script>window.open('customer/my_account.php','_self')</script>";
exit();
} else {
$_SESSION['customer_email'] = $email;
$_SESSION['username'] = $usrname;
$_SESSION['id'] = $id;
echo "<script>alert('You Have Logged In Succesfully')</script>";
echo "<script>window.open('checkout.php','_self')</script>";
}
} else {
echo 'Please enter valid email ID';
}
}
?>
my_account.php
<?php
session_start();
?>
<ul id="categories">
<?php
$location = 'customer_images/';
$usr_email = $_SESSION['customer_email'];
$user_name = #$_SESSION['username'];
$usr_id = $_SESSION['id'];
$select_image = "SELECT * FROM customers WHERE customers_id = '$usr_id'";
$run_image = mysqli_query($conn,$select_image);
foreach ($run_image as $select_all_data) {
$id = $select_all_data['customers_id'];
$name = $select_all_data['customers_name'];
$username = $select_all_data['customers_username'];
$email = $select_all_data['customers_email'];
$country = $select_all_data['customers_country'];
$city = $select_all_data['customers_city'];
$contact = $select_all_data['customers_contact'];
$address = $select_all_data['customers_address'];
$image = $select_all_data['customers_image'];
echo "<li style=''><img src='$location$image' height='150' width='174' style='text-align:center; border:3px solid black; padding:4px; border-radius: 109px;'></li>";
}
?>
<li>My Orders</li>
<li>Edit Account</li>
<li>Change Password</li>
<li>Delete Account</li>
<li>Logout</li>
</ul>
</div>
<div class="content_area2">
<?php cart(); ?>
<div id="my_account_menu">
<span>
<?php
if (isset($_SESSION['customer_email'])) {
echo "Welcome <i style='color:orange;'>" . $_SESSION['username']. ' </i>';
}
?>
<?php
if (!isset($_SESSION['customer_email'])) {
echo "<a href='../checkout.php' style='color:white;'>Login</a>";
} else {
echo "<a href='../logout.php' style='color:orange;'>Logout</a>";
}
?>
</span>
</div>
<?php getIp(); ?>
<div id="products_box">
<?php
if (!isset($_GET['my_orders']) && !isset($_GET['edit_account']) && !isset($_GET['change_pass']) && !isset($_GET['delete_account'])) {
?>
<div style="text-align:center;">
<table>
<tbody>
<tr>
<td id="label">Name : </td>
<td id="detail"><?php echo #$name; ?></td>
</tr>
<tr>
<td id="label">Username (Display Name) : </td>
<td id="detail"><?php echo #$username; ?></td>
</tr>
<tr>
<td id="label">Email : </td>
<td id="detail"><?php echo #$email; ?></td>
</tr>
<tr>
<td id="label">City : </td>
<td id="detail"><?php echo #$city; ?></td>
</tr>
<tr>
<td id="label">Contact : </td>
<td id="detail"><?php echo #$contact; ?></td>
</tr>
<tr>
<td id="label">Address : </td>
<td id="detail"><?php echo #$address; ?></td>
</tr>
</tbody>
</table>
</div>
<div style="padding:20px;"><b>You can see your orders by clicking this link</b></div>
<?php
}
?>
The problem is when I login it is working fine but when I register the user it gives the error undefined
$usr_id = $_SESSION['id'];
in my_account.php and all the information about the user is not displayed
I HAVE STARTED THE SESSION
I think you forget to call session_start before to do anything, even when there is no existing session.
<?php
session_start()
// your code...
You may want to look how it works with the basic example from the session_start function
As stated within the PHP session_start documentation:
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
So you may want to include this call after PHP opening tag <?php as I wrote above
You have to initiate the session by using session_start() on every page where you want to use the session. And this is missing on my_account.php
Ex:
<?php
session_start();
// your code
nowhere in your customer_register.php file do you actually set $_SESSION['id'].
in addition to this:
there are no form tags around your register form, so its hard to see how any data is getting into the script anyway.
your'e also silencing (#) errors. If this is a learning task, which from your comments I assume it is, thats a very counterproductive thing to do.
As you only set $_SESSION['id'] in the login handing script, its not going to be available across pages unless they go through that form. Its hard to tell from your code what your'e intended user journey is, but as it stands at the moment it looks like your'e expecting them to register, then be able to see the account page as a logged in user.
Just set $_SESSION['id'] in the register script, or dispense with it entirely and use the username (seeing as you validate it as unique anyway).
Also, your'e query in the my_account.php is looking for a column customer_id which (as far as I can see) you dont actually set in the insert statement.
This is the trouble with programming, it only does what you tell it to do.
I have problem with my buttons. I will try to explain.
1)I have users db from postgresql and new db in ms sql.
2)Created site with 2 columns in table ("SELECT * from users"-postgresql ): They are id/user
3)Then added new column "Operator" which contain submit buttons and functionality of submit buttons is updating "access" column from ms sql db.
PROBLEM:
It prints all buttons for all data that I have in ms sql(I have 7 rows data in ms sql, it prints 7 buttons for each row), I need to "echo" 1 button for each row which will be changable. If access==1 it should be named Active , else it should be named Diactive.
Here is my code and picture of what I got:
<?php
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
<th>View</th>
</tr>
<?php
while ($row = pg_fetch_array($result)) {
?>
<tr>
<td>
<?php
$id = $row["id"];
echo $id;
?>
</td>
<td>
<?php
$username = $row["username"];
echo $username;
?>
</td>
<td>
<form method="POST" action="oper.php">
<?php
include ("db.php");
$result2 = pg_query($db_connection, "SELECT * from users ORDER by id asc");
while ($row1 = pg_fetch_array($result2))
{
$iddrain= $row1['id'];
//echo $iddrain;
//echo $iddrain;
$q7= "Select access from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid= sqlsrv_query($link, $q7, $params, $options);
while($row7= sqlsrv_fetch_array($resultid))
{
//$rs7=$row7['ID_M'];
$rs8=$row7['access'];
//echo $rs8;
//break;
if($rs8==1)
{
echo "<p><input type=\"submit\" name=\"uid\" value=Operator-ON onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
else
{
echo "<p><input type=\"submit\" name=\"uid\" value=DIavtive onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
}
}
?>
</form>
</td>
<?php
}
?>
</tr>
</table>
?>
You are doing typo mistake na dnot giving quote for value attributes near :
try like this :
if($rs8==1)
{
echo '<p><input type="submit" name="uid" value="Operator-ON" onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value="'.$id.'" onchange=
"this.form.submit()"></p>';
}
else
{
echo '<p><input type="submit" name="uid" value="DIavtive" onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value="'.$id.' onchange="this.form.submit()"></p>';
}
}
I have reditted your code to remove some errors that were making it fail to work;
<?php include ("db.php"); ?>
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
<th>View</th>
</tr>
</thead>
<?php
//while ($row = pg_fetch_array($result)) {
<?php foreach( pg_fetch_array($result) as $row ) { ?>
?>
<tbody>
<tr>
<td<?php echo $row['id'] ?></td>
<td><?php echo $row['username'] ?></td>
<td>
<form method="POST" action="oper.php">
<?php
$result2 = pg_query($db_connection, 'SELECT * from users ORDER by id asc');
while ($row1 = pg_fetch_array($result2)) {
$iddrain = $row1['id'];
$q7 = "Select access from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid = sqlsrv_query($link, $q7, $params, $options);
while ($row7 = sqlsrv_fetch_array($resultid)) {
//$rs7=$row7['ID_M'];
$rs8 = $row7['access'];
if ($rs8 == 1) {
echo '<p><input type="submit" name="uid" value=Operator-ON onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value=$id onchange="this.form.submit()"></p>';
} else {
echo '<p><input type="submit" name="uid" value=DIavtive onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value=$id onchange="this.form.submit()"></p>';
}
}
}
?>
</form>
</td>
</tr>
</tbody>
</table>
I`ve changed code and now it is working, problem was additional useless fetch. Here is the code:
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
</tr>
<?php
while ($row = pg_fetch_array($result)) {
?>
<tr>
<td>
<?php
$id = $row["id"];
echo $id;
?>
</td>
<td>
<?php
$username = $row["username"];
echo $username;
?>
</td>
<td>
<form method="POST" action="oper.php">
<?php
include ("db.php");
$iddrain= $row['id'];
$q7= "Select * from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid= sqlsrv_query($link, $q7, $params, $options);
while($row7= sqlsrv_fetch_array($resultid))
{
$rs8=$row7['access'];
//echo $rs8;
if($rs8==1)
{
echo "<p><input type=\"submit\" name=\"uid\" value=Operator onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
else
{
echo "<p><input type=\"submit\" name=\"uid\" value=Nazeret onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
}
?>
</form>
</td>
</tr>
<?php
}
?>
</tr>
</table>
I am trying to add a captcha to my guestbook submission form but cannot get the if(($_POST['code']) == ($_SESSION['code'])) statement to work. Please see code below. Any help would be greatly appreciated.
if ($_POST['postbtn']){
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$comment = strip_tags($_POST['comment']);
if(($_POST['code']) == ($_SESSION['code'])) {
$code = strip_tags($_POST['code']);
}
if($name && $email && $comment && $code){
$time = date("h:i A");
$date = date("F d, Y");
$ip = $_SERVER['REMOTE_ADDR'];
// add to the database
mysqli_query($con,"INSERT INTO guestbook VALUES (
'', '$name', '$email', '$comment', '$time', '$date', '$ip'
)");
echo "Your post has been added.";
}
else
echo"You did not enter in all the required info.";
}
echo "<form action='./index.php' method='post'>
<table>
<tr>
<td>Name:</td>
<td><input type='text' name='name' style='width: 200px;' /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type='text' name='email' style='width: 200px;' /></td>
</tr>
<tr>
<td>Comment:</td>
<td><textarea name='comment' style='width: 197.5px; height: 50px;'>
</textarea></td>
</tr>
<tr>
<td><img src='captcha.php?'/></td>
<td><input type='text' name='code' style='width: 200px;' /></td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='postbtn' value='Post' /></td>
</tr>
</table>
</form>";
Can you try echo on both $_POST['code'] and $_SESSION['code'] so that you can debug it and see if you are getting the values correctly? I don't think you are getting them right. if not just add up a complete scenario here with proper field names and values.
I am working on a web based contact list for a friend. I have the html portion all done and working on the PHP scripts and such. I have the main page as a table in a while loop enclosed in form tags. I need two things to happen but not sure how to get this accomplished.
First, each row has to have two submit buttons, which one goes to edit and the other to details, and carries over the values in the global $_POST.
Second, the list will be about 300 rows, so i am using a while loop to create the table.
I have the form working and passing the data but it is always passing the last row of the table. Here is my main page with the table:
<?php
if
(!isset ($_SESSION['username']))
{
session_start();
}
?>
<html>
<head>
<title>Client Contact List</title>
</head>
<?php
$user1 = implode(',',$_SESSION);
//DB information
require_once('/includes/db.php');
//Declaring edit and details
$edit = "<INPUT type='image' src='/addressbook/images/edit.png' onclick='\addressbook\edit.php'>";
$details = "<INPUT type='image' src='/addressbook/images/contact.gif' name='details' onclick='f1.action='\addressbook\contact_details.php'>";
//Table declarations and such
mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
$result = mysql_query("SELECT * FROM contacts") or die(mysql_error());
$num=mysql_numrows($result);
$user1 = implode(',',$_SESSION);
$userresults = "SELECT first FROM i_user where userid IN $user1";
$user = mysql_query($userresults);
// print_r ($_SESSION);
// print_r ($_POST);
?>
<body style="background-image: url('Images/background_login.jpg');">
<br><br><br><br><br><br>
<table>
<br><br>
<tr><td width="500">Welcome Back, <?php echo $user; ?></td><td width="500"></td><td width="300"><form name="search" method="post" id="searchform" action="<?php echo $_SERVER['PHP_SELF'];?>"><label for="searchtext">Search: </label><input type="text" name="name" /> <input type="submit" name="submit" value="Search" /></form>
</td></tr>
</table>
<br>
<form name="f1" method="post" action="/addressbook/edit.php">
<table border="1">
<tr>
<?php
echo "<table border='1'>";
echo "<tr>
<th>First</th>
<th>Last</th>
<th>Company</th>
<th>Primary Email</th>
<th>Secondary Email</th>
<th>Primary Phone</th>
<th>Second Phone</th>
<th>Action</th>
</tr>";
$i=0;
while ($i<$num) {
$id = mysql_result($result,$i,"id");
$first = mysql_result($result, $i, "first");
$last = mysql_result($result,$i, "last");
$company = mysql_result($result, $i, "company");
$email1 = mysql_result($result,$i, "email1");
$email2 = mysql_result($result,$i, "email2");
$phone = mysql_result($result,$i, "phone");
$mobile = mysql_result($result,$i, "mobile");
// Print out the contents of each row into a table
echo "<tr><td width = '100'><center><input type='hidden' value='$first' name='first'>";
echo $first;
echo "</center></td><td width = '100'><center><input type='hidden' value='$last' name='last'>";
echo $last;
echo "</center></td><td width = '100'><center><input type='hidden' value='$company' name='company'>";
echo $company;
echo "</center></td><td width = '100'><center><input type='hidden' value='$email1' name='email1'>";
echo $email1;
echo "</center></td><td width = '100'><center><input type='hidden' value='$email2' name='email2'>";
echo $email2;
echo "</center></td><td width = '100'><center><input type='hidden' value='$phone' name='phone'>";
echo $phone;
echo "</center></td><td width = '100'><center><input type='hidden' value='$mobile name='mobile'>";
echo $mobile;
echo "</center></td><td width = '100'><center>";
echo $edit;
echo "    ";
echo $details;
echo "</td></center></tr>";
echo "<input type='hidden' value='$id name='id'></td>";
$i++;
}
?>
</tr>
</table>
</form>
</body>
</html>
This get directed to either the details or edit page. Below is the edit page....
<?php
if
(!isset ($_SESSION['username']))
{
session_start();
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Contact Information</title>
</head>
<?php
//DB information
require_once('/includes/db.php');
mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$company = $_POST['company'];
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$phone = $_POST['phone'];
$mobile = $_POST['mobile'];
//pulling the record id from the main login page.
$first=$_POST['first'];
$query="SELECT * FROM contacts where last=$last";
$result=mysql_query($query);
print_r($_POST);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body style="background-image: url('Images/background_login.jpg');">
<br><br><br><br><br>
<!-- First Table with the back and search option but disabled for now -->
<table>
<br>
<tr>
<td width="500">
<input type='button' value='Back' onClick='history.go(-1)'>
</td>
<td width="500"></td>
<td width="300">
<!-- <form name="search" method="post" id="searchform" action="<?php echo $_SERVER['PHP_SELF'];?>">
<label for="searchtext">Search: </label>
<input type="text" name="name" /> <input type="submit" name="submit" value="Search" />
</form> -->
</td>
</tr>
</table>
<br><br>
<center>
<!-- Second Table with form data pulled out for Identify -->
<table>
<tr>
<th>
<table>
<tr>
<td bgcolor="silver" colspan="4"><center>Identify</center></td>
</tr>
<tr>
<td width="100"><center><b>Title</b></center></td>
<td width="100"></td>
<td width="150"><center><b>Company Name</b></center></td>
</tr>
<tr>
<td width="100"><input value="Title"></td>
<td width="100"></td>
<td width="100"><? echo $company ?></td>
</tr>
<tr><td colspan="4"></td></tr>
<tr>
<td width="100"><center><b>First Name</b></center></td>
<td width="100"></td>
<td width="100"><center><b>Last Name</b></center></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
</table>
</th> <!-- Space between the contact info and Indenty -->
<td width="100">
</td>
<th>
<td> <!-- Third Table with form data pulled out -->
<table>
<tr>
<td bgcolor="silver" colspan="4"><center>Contact Information</center></td>
</tr>
<tr>
<td width="100"><center><b>Office Phone</b></center></td>
<td width="100"></td>
<td width="150"><center><b>Mobile Name</b></center></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
<tr>
<td colspan="4"></td>
</tr>
<tr>
<td width="100"><b>Primary Email</b></td>
<td width="100"></td>
<td width="150"><b>Secondary Email</b></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
</table>
</td>
</th>
<tr height="100"> <td colspan="9"></td> </tr>
<th class="style2">
<table>
<tr width="400"></tr>
<tr>
<td bgcolor="silver" colspan="4"><center>Applications Used</center></td>
</tr>
<tr>
<td width="100"></td>
</tr>
<tr>
<td colspan="4"></td>
</tr>
<tr>
<td width="100"></td>
</tr>
<tr>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
</tr>
</table>
</th>
<td width="200"></td>
<td>
<th class="style2">
<table>
<tr>
<td bgcolor="silver" colspan="4"><center>Internal Information</center></td>
</tr>
<tr>
<td width="100"><center><b>Account Mgr</b></center></td>
<td width="100"></td>
<td width="150"><center><b>Client Relations</b></center></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
<tr><td colspan="4"></td></tr>
<tr>
<td width="200"><center><b>Acct Development</b></center></td>
<td width="100"></td>
<td width="100"><center><b>Project Mgr</b></center></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
</table>
</th>
</td>
</table>
</center>
</body>
</html>
Any thoughts on how to get this done?
Put the form tag inside the loop. And place the submit button inside the form tag.
It sounds like you need a lot of form, instead of a form with a lot of field.
Yet another suggestion.. jqgrid may be a good thing to use here. look it up if you got a chance.
The problem is you are creating a ton of elements that have the same name... Every row has a input by the name of email1, email2, phone, mobile, etc.
when you submit the form it just takes the value of the last html element with the given name. So it will alwyas give you the last row.
What you can do is only have 1 hidden input for each attribute. Then when you select a row, you can set the values for the hidden inputs using javascript.
HOWEVER, to make it more simple... You are already storing all of the users data in a database so you don't need to pass it all to the next page. Just pass the id to the next page, then when you get to the next page perform a select query to get all of that user's data.
This way you can have 1 hidden input for the id. When the user selects the row they want to edit use Javascript to set the value of that input.
Each button should look something like this:
echo "<input type=\"button\" onclick=\"document.form.id=$id\">";
The input should look something like this:
echo "<input type=\"hidden\" name=\"id\"/>";
Then on the edit page use :
$id = $_POST['id'];
$query = "SELECT id, first, last, company, email1, email2, phone, mobile
FROM contacts WHERE ID=$id"
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$id = $row['id'];
$first = $row['first'];
etc...
I am new here and I have a question. I have a problem that I can't figure it out with _POST. I have been searching for hours before start writing! As far as I can see I haven't done any of the mistakes that are posted for other similar question (form action..., name attribute...,etc). Please, can you check my code below to tell me what am I doing wrong??
I use xampp 1.7.3 on windows 7.
<?php require("includes/header.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?><head>
<script type="text/javascript">
function theChecker()
{
if(document.getElementById('checker').checked){
document.getElementById('submitter').disabled=false;
}
else{
document.getElementById('submitter').disabled=true;
}
}
</script>
</head>
<?php require("includes/body_no_menus.php"); ?>
<div align="center">
<form name="signup" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
echo "<hr/>
<table width='600' border='0'>
<tr>
<td width='237'>Κωδικός οικοδομής</td>
<td width='351'><input name='building_id' type='text' id='building_id' size='30' maxlength='40' />*</td>
</tr>
<tr>
<td>Κωδικός διαμερίσματος</td>
<td><input name='apartment_id' type='text' id='apartment_id' size='30' maxlength='40' />*</td>
</tr>
<tr>
<td></td>
<td height='31' colspan='2' ><label>
<input name='send' type='submit' value='Αποστολή' />
</label></td>
</tr>
</table>
";
?>
</form>
</div>
<p>
<?php
if(isset($_POST['send'])) {
// Check input / Required fields
$building_id = check_input($_POST['building_id'],"Εισάγετε τον κωδικό της οικοδομής!");
$apartment_id = check_input($_POST['apartment_id'],"Εισάγετε όνομα χρήστη!");
$query = "SELECT idTENANT,FNAME,LNAME,BUILDING_ADMIN,PHONE FROM TENANT,APARTMENT, BUILDING
WHERE TENANT.APARTMENT_ID = APARTMENT.idAPARTMENT
AND APARTMENT.BUILDING_ID = BUILDING.idBUILDING
AND idAPARTMENT = '$apartment_id'
AND idBUILDING = '$building_id'";
$result=mysql_query($query) or die ("Couldn't execute query.");
$row = mysql_fetch_array( $result );
$id = $row['idTENANT'];
$fname = $row['FNAME'];
$lname = $row['LNAME'];
$apartment = $row['APARTMENT_ID'];
$phone = $row['PHONE'];
if($row['BUILDING_ADMIN'] == 0)
$admin = "ΟΧΙ";
else
$admin = "ΝΑΙ";
echo " <hr />
<table width='300' border='0'>
<tr>
<td>Όνομα</td>
<td>$fname</td>
</tr>
<tr>
<td>Επίθετο</td>
<td>$lname</td>
</tr>
<tr>
<td>Όνομα χρήστη</td>
<td><input name='username' type='text' size='30' maxlength='20' />*</td>
</tr>
<tr>
<td>Κωδικός χρήστη</td>
<td><input name='password' type='password' size='30' maxlength='20'/>*</td>
</tr>
<tr>
<td>Επαλήθευση κωδικού</td>
<td><input name='verify_password' type='password' size='30' maxlength='40'/> *</td>
</tr>
<tr>
<td>Διαχείριση οικοδομής</td>
<td>$admin</td>
</tr>
<tr>
<td>Τηλέφωνο</td>
<td>$phone</td> </tr>
<tr>
<td></td>
<td><input name='checkterms' type='checkbox' id='checker' onclick='theChecker()' value='Ναι'/>
<label>Έχω διαβάσει και αποδέχομαι τους όρους χρήσης.</label> *</td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='complete' id='submitter' value='Ολοκλήρωση εγγραφής' disabled/></td>
</tr>
</table>
";
}
if(isset($_POST['complete'])) {
// Password match
if ($password != $verify_password)
{
echo '<font color="red">Οι κωδικοί δεν ταιριάζουν</font>';
}//if
else
{
// Execute MySQL commands
$query = "UPDATE TENANT SET USERNAME = '$un', PASSWD='$pw' WHERE idTENANT='$id'";
$result=mysql_query($query) or die ("Couldn't execute query.");
header("Location: main_login.php");
}//else
}//if
?>
</p>
<?php require("includes/footer.php"); ?>
The first _POST (if(isset($_POST['send']))...) works perfectly. But if(isset($_POST['complete'])) {... does nothing. I ve tried to echo some data to see if my connection doesn't work, but its the _POST...
Please help me!!!!
Thanks for your time!
what you could try:
use vardump to see what $_POST contains: var_dump($_POST);.
use firebug (or something similar for another browser) to lookup the request and see which POST-Parameters are sent.
The second set of form elements (username, password, verify_password, checkterms, complete) are not inside any html form element. Clicking the second button does not post the form to server.
header("Location: main_login.php");
Is not going to work, when $_POST["complete"] is reached. You already sent heaps of output before that. Enable more error_reporting.