Updated data revert back to original value at MYSQL - php

I have weird problem which is when i run update function it was successfully executed & i check at mysql database it was updated. However, after a few minutes the value at mysql database reverting to original value (before update).
As i can see my codes seems OK because i can update the value at mysql database but somehow maybe its related with $_SESSION which is i am not familiar with. Below is my code:-
<?php session_start();
$staff_no=$_SESSION['staff_no'];
$user_name=$_SESSION['user_name'];
$pass=$_SESSION['pass'];
$level=$_SESSION['level'];
include("../connect.php");
include ("../session.php");
$today=date('Y-m-d');
$VendorID=$_GET['vendorid'];
if($submit=isset($_POST['submit']))
{
$VendorID=isset($_POST['VendorID']) ? $_POST['VendorID'] : '';
$CompanyName=isset($_POST['CompanyName']) ? $_POST['CompanyName'] : '';
$Address=isset($_POST['Address']) ? $_POST['Address'] : '';
$TelephoneNo=isset($_POST['TelephoneNo']) ? $_POST['TelephoneNo'] : '';
$FacsimileNo=isset($_POST['FacsimileNo']) ? $_POST['FacsimileNo'] : '';
$URL=isset($_POST['URL']) ? $_POST['URL'] : '';
$RegisterDate=isset($_POST['RegisterDate']) ? $_POST['RegisterDate'] : '';
$status=isset($_POST['status']) ? $_POST['status'] : '';
$region=isset($_POST['region']) ? $_POST['region'] : '';
$sql2 = "UPDATE vendor SET VendorID='$VendorID',CompanyName='$CompanyName',Address='$Address',TelephoneNo='$TelephoneNo',
FacsimileNo='$FacsimileNo',URL='$URL',RegisterDate='$RegisterDate',status='$status',region='$region'
WHERE VendorID='$VendorID'";
if(#mysqli_query($con, $sql2))
{
echo "<script type='text/javascript'>\n";
echo "alert('Successfully edit data');\n";
echo "</script>";
echo "<script>window.navigate('vendordetails_add.php?vendorid=$VendorID')</script>";
}
else
echo 'the data failed to edit'.mysqli_error();
}//if isset submit
$sql =mysqli_query($con, "SELECT * FROM vendor WHERE VendorID='$VendorID'");
$row = mysqli_fetch_array($sql);
$id=$row['id'];
$VendorID=$row['VendorID'];
$CompanyName=$row['CompanyName'];
$Address=$row['Address'];
$TelephoneNo=$row['TelephoneNo'];
$FacsimileNo=$row['FacsimileNo'];
$URL=$row['URL'];
$RegisterDate=$row['RegisterDate'];
$status=$row['status'];
$region=$row['region'];
?>
<!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" />
</head>
<body>
<form name="vendor_edit" enctype="multipart/form-data" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<input type="hidden" name="VendorID" value="<?php echo $VendorID; ?>" />
<input type="hidden" name="status" value="<?php echo $status; ?>" />
<table width="784" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="80" valign="top">
<table width="100%" border="0" cellpadding="0" cellspacing="0" background="../image/center.jpg">
<tr>
<td height="20" valign="top"> </td>
<td width="81" valign="top" class="txt">Company</td>
<td colspan="2" valign="top"><input name="CompanyName" type="text" class="text" id="CompanyName" size="50" value="<?php echo $CompanyName; ?>"/></td>
</tr>
<tr>
<td height="60" valign="top"> </td>
<td valign="top" class="txt">Address</td>
<td colspan="2" valign="top"><textarea name="Address" cols="40" rows="4" class="txt" id="Address"><?php echo $Address; ?></textarea></td>
</tr>
<tr>
<td height="21" valign="top"> </td>
<td valign="top" class="txt">Telephone No</td>
<td colspan="2" valign="top"><input name="TelephoneNo" type="text" class="text" id="TelephoneNo" size="15" value="<?php echo $TelephoneNo; ?>"/></td>
</tr>
<tr>
<td height="21" valign="top"> </td>
<td valign="top" class="txt"> Fax No</td>
<td colspan="2" valign="top"><input name="FacsimileNo" type="text" class="text" id="FacsimileNo" size="15" value="<?php echo $FacsimileNo; ?>"/></td>
</tr>
<tr>
<td height="21" valign="top"> </td>
<td valign="top" class="txt">Website</td>
<td colspan="2" valign="top"><input name="URL" type="text" class="text" id="URL" size="30" value="<?php echo $URL; ?>"/></td>
</tr>
<tr>
<td height="21" valign="top"> </td>
<td valign="top" class="txt">Region</td>
<td colspan="2" valign="top">
<select name=region id="region">
<option value="Malaysia"<?php if ($region=='Malaysia'){ ?> selected="yes" <?php } ?> >Malaysia </option>
<option value="International"<?php if ($region=='International'){?> selected="yes" <?php } ?> >International</option>
</select></td>
</tr>
<tr>
<td height="21" valign="top"> </td>
<td valign="top" class="txt">Status</td>
<td colspan="2" valign="top">
<select name=status id="status">
<option value="Active"<?php if ($status=='Active'){ ?> selected="yes" <?php } ?> >Active </option>
<option value="KIV"<?php if ($status=='KIV'){?> selected="yes" <?php } ?> >KIV</option>
<option value="Reconsider"<?php if ($status=='Reconsider'){?> selected="yes" <?php } ?> >Reconsider</option>
</select></td>
</tr>
<tr>
<td height="21" valign="top"> </td>
<td valign="top" class="txt">Register Date</td>
<td colspan="2" valign="top"><input name="RegisterDate" type="text" class="text" id="RegisterDate" value="<?php echo $RegisterDate; ?>" size="15"/></td>
</tr>
<tr>
<td height="21" valign="top"> </td>
<td valign="top"> </td>
<td colspan="2" width="613" valign="top">
<input type="submit" name="submit" class="text" value="Update" />
<input type="button" name="back" value="Back" class="text" onClick="window.history.back(); return false;" /></td>
</tr>
</td>
</table>
</form>
</body>
</html>
Another page that involve $status is vendor_add.php :-
<?php
session_start();
$staff_no=$_SESSION['staff_no'];
$user_name=$_SESSION['user_name'];//call session back
$pass=$_SESSION['pass'];
$level=$_SESSION['level'];
include("../connect.php");
include ("../session.php");
$today=date('Y/m/d');
if($submit=isset($_POST['submit']))
{
$sql=mysqli_query($con, "SELECT VendorID FROM vendor ORDER BY VendorID DESC LIMIT 1");
$row=mysqli_fetch_array($sql);
$temp=$row['VendorID'];
$VendorID=$temp + 1;
$today=date('Y-m-d');
$CompanyName=isset($_POST['CompanyName']) ? $_POST['CompanyName'] : '';
$Address=isset($_POST['Address']) ? $_POST['Address'] : '';
$TelephoneNo=isset($_POST['TelephoneNo']) ? $_POST['TelephoneNo'] : '';
$FacsimileNo=isset($_POST['FacsimileNo']) ? $_POST['FacsimileNo'] : '';
$URL=isset($_POST['URL']) ? $_POST['URL'] : '';
$RegisterDate=isset($_POST['RegisterDate']) ? $_POST['RegisterDate'] : '';
$region=isset($_POST['region']) ? $_POST['region'] : '';
$sql2 = "INSERT INTO vendor(VendorID,CompanyName,Address,TelephoneNo,FacsimileNo,URL,RegisterDate,region,status)VALUES
('$VendorID','$CompanyName','$Address','$TelephoneNo','$FacsimileNo','$URL','$RegisterDate','$region','active')";
if(mysqli_query($con, $sql2))
{
echo "<script type='text/javascript'>\n";
echo "alert('Successfully add data');\n";
echo "</script>";
echo "<script>window.navigate('vendordetails_add.php?vendorid=$VendorID')</script>";
}
else
echo 'the data could not be added'.mysqli_error();
}//if isset submit
mysqli_close($con);
?>
And also my connect.php :-
<?php
//connection to database
//must include this file in every file to undeny data access
$dbuser = "root";
$dbserver = "localhost";
$dbpass = "mypass123";
$dbname = "vendor";
$con= new mysqli($dbserver, $dbuser, $dbpass) or die ("UNABLE TO CONNECT TO DATABASE");
mysqli_select_db($con, $dbname) or die ("UNABLE TO SELECT DATABASE");
?>

sorry for the trouble. i found the culprit of my headache. The script was developed by ex-staff. I discovered there was autorun.php script included in index.php.
autorun.php will automatically put the status of vendor either "Active", "KIV" & "Reconsider" based on timeframe specified in the script. Thats why everytime i update the status it will revert back to "Active" after a few minutes.
Anyhow thanks for ur help..!!

If you use InnoDB-tables, which support transactions, each operation on one of these tables starts an implicit transaction if you do not have an explicit transaction started.
https://dev.mysql.com/doc/refman/5.7/en/commit.html
As long as you are within the same session (keywords "connection pooling" and "connection timeout"), your changes are visible to you, but to nobody else. When your connection finally times out, the transaction is rolled back instead of commited. This is true because you never close your session here.
To make your changes permanent, you have 3 options:
Cast a commit after your operation
Set implicit commit and close your session properly
Use for example MyISAM tables (though they lack the use of foreign key constraints)
In general, you should:
close your result sets properly (mysqli_free_result)
make sure your connection gets closed properly whenever your script terminates
make sure all of your transaction details are set up properly, otherwise you will run into locking issues
This should solve your problem, at the end of your whole php script part (before the ?>), do the following:
mysqli_free_result($con, $sql);
mysqli_query($con, "COMMIT");
mysqli_close($con);

Related

Can't update/change data in MySQL from PHP

I was almost there, but the update is not functioning well especially on the bottom part.
<?php
require('dbconnect.php');//Connects to the database
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysqli_query($link,"SELECT username FROM members WHERE username='$user_check'");
$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$loggedin_session=$row['username'];
if(!isset($loggedin_session))
{
header("Location: login.php");
}//To ensure that you must be logged in to access this page
?>
<html>
<head>
<title>Healing Food Form</title>
<meta charset="iso-8859-1"> <!--charset specifies characters available-->
<meta name="author" content="Klarenz Kristoffer M. Qui;ntildeones">
<meta name="description" content="form to update healing food">
<meta name="keywords" content="healing food,form">
</head>
<body>
<?php
$id=$_GET['hf_id'];
$query = "SELECT * FROM healingfood WHERE hf_id='$id'";
if(!mysqli_query($link,$query))
{
die("Sorry. There's a problem with the query.");
}
//stores the result of the query
$result = mysqli_query($link,$query);
while($record = mysqli_fetch_assoc($result))
{
$hf_id=$record['hf_id'];
$hf_title=$record['hf_title'];
$a_id=$record['a_id'];
$hf_image=$record['hf_image'];
$hf_description=$record['hf_description'];
$hf_benefits=$record['hf_benefits'];
$hf_source=$record['hf_source'];
?>
<form action="updatehealingfood.php?hf_id=<?php echo $record['hf_id']; ?>" method="POST">
<table id="container" align="center">
<caption>Update healing food</caption>
<tr>
<td>Title:</td>
<td><input name="hf_title" type="text" value="<?php echo $hf_title; ?>"><br></td>
</tr>
<tr>
<td>Author ID:</td>
<td><input name="a_id" type="text" value="<?php echo $a_id; ?>"><br></td>
</tr>
<tr>
<td>Image URL:</td>
<td><input name="hf_image" type="url" value="<?php echo $hf_image; ?>"><br></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea name ="hf_description" rows="18" cols="60"><?php echo $hf_description; ?></textarea><br></td>
</tr>
<tr>
<td>Benefits:</td>
<td><input name="hf_benefits" type="text" value="<?php echo $hf_benefits; ?>"><br></td>
</tr>
<tr>
<td>Source:</td>
<td><input name="hf_source" type="text" value="<?php echo $hf_source; ?>"><br></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="update" value="Update Healing Food"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
$id=$_GET['hf_id'];
if(isset($_POST['update']))
{
$hf_title=$_POST['hf_title'];
$a_id=$_POST['a_id'];
$hf_image=$_POST['hf_image'];
$hf_description=$_POST['hf_description'];
$hf_benefits=$_POST['hf_benefits'];
$hf_source=$_POST['hf_source'];
$query2="UPDATE healingfood SET hf_title='$hf_title', a_id='$a_id', hf_image='$hf_image', hf_description='$hf_description', hf_benefits='$hf_benefits', hf_source='$hf_source' WHERE hf_id='$id'";
$result2=mysql_query($query2) or die();
echo "Updated";
}
?>
When I was supposed to update the data, the data remains the same. No one changed. I don't get the $id=$_GET['hf_id']; .
What are my errors?
You are mixing mysqli_* and mysql_*.
At the first part you use mysqli_query(), later you use mysql_query() which has no connection to the database yet.
Stick to mysqli_*.
Change:
$result2=mysql_query($query2) or die();
to:
$result2=mysqli_query($link, $query2) or die( "MySQL error: " . mysqli_error($link) );

increment variable on submit to update mysql query

I am new to PHP(loving it already)
I have a form that looks up a table that sends 'golf hole' info back and allows a golfer to input their score of the hole. Problem I have is that I can present the first hole by looking up the hole_detail table but then cant figure out how loop through the table for hole 2, 3.....18 when the form is submitted. I have searched stackoverflow but cant find anything that specific about it. I have tried an if statement, if (isset($_POST['Submit'])) to try increment the $hole_id. Am I completely going about it the wrong way? Thanks in advance.
<?php
include ('../scripts/dbconfig.php');
# get the most recent course name:
$get_course_name = mysql_query("SELECT course_name FROM comp ORDER BY PID DESC LIMIT 1");
$show_course_name = mysql_fetch_array($get_course_name);
if (isset($_POST['Submit'])) {
$hole_id =1;
else {
$hole_id = $hole_id + 1;
}
}
# get the hole yardage and SI from most recent selected golf course:
$get_course_detail = mysql_query("SELECT * FROM `course_detail` WHERE course_name = '". $show_course_name['course_name'] . "'");
$show_course_detail = mysql_fetch_array($get_course_detail);
$get_hole_detail = mysql_query("SELECT * FROM `course_detail`,`phoenix_hole` WHERE Course_ID = 6 AND hole_id = $hole_id");
$show_hole_detail = mysql_fetch_array($get_hole_detail);
?>
</head>
<body>
<table width="300" cellspacing="0" cellpadding="0">
<tr>
<td width="40"><?php echo $show_course_name['course_name'];?></td>
</tr>
<tr>
<td width="20">HOLE <?php echo $show_hole_detail['hole_id']?></td>
<td width="5"> PAR <?php echo $show_hole_detail['hole_par'];?></td>
</tr>
<tr>
<td width="20">Yards</td>
<td width="20">S.I</td>
</tr>
<tr>
<td bgcolor="yellow"><?php echo $show_hole_detail['yellow_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
<tr>
<td border="1px" bgcolor="white"><?php echo $show_hole_detail['white_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
<tr>
<td bgcolor="red"><?php echo $show_hole_detail['red_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
</table>
</p>
<form id="game_form" name="game_form" method="post" action="game_form.php">
<table width="300" border="0" align="left" cellpadding="2" cellspacing="0">
<tr>
<td><b>Hole Shots</b></td>
<td><input name="hole_shots" type="text" class="textfield" id="hole_shots" maxlength="2" size="3" ></td>
<td><b>Putts</b></td>
<td><input name="putts" type="text" class="textfield" id="putts" maxlength="2" size="3"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Next Hole" align="center" /></td>
</tr>
</table>
</form>
</body>
</html>
Or you can use a hidden field that keeps the hole number and you can increment it from php.
$hole_id, in this scenario, will always be 1, because when a user clicks the Submit button, $_POST['Submit'] will always have a value. What you should do instead is have $_POST['Submit'] contain the value of $hole + 1. PHP is not going to "remember" what $hole_id was last time around; it's up to you to remind it. As soon as a request is sent to the browser--unless you're using sessions--PHP forgets everything about that request (HTTP is "stateless").
<?php
if (isset($_POST['Submit'])) {
$hole_id = (int)$_POST['Submit'];
} else {
$hole_id = 1;
}
# other code here
?>
You are on hole #<?php echo $hole_id; ?>.
<form>
<!-- form stuff here -->
<button type="submit" name="Submit" value="<?php echo $hole_id + 1; ?>">Next hole</button>
</form>

Unlocked Forum Post saying it's locked?

All right, sorry for so many posts. Anyway, I have created an entire file of code; view_topic.php, that is just showing a forum post. I know it is messy and not in mysqli, I will be rewriting the ENTIRE code, once I finish this page. Anyway, on to the problem. When you visit any topic, locked or unclicked, it will ALWAYS say, "Sorry, this post is locked." There are no error messages. I have spent all day trying to find the error in my code, and I have turned to the internet for guidance. Here is the whole code, tell me if you need anything else:
<?php
require_once 'core/init.php';
// get value of id that sent from address bar
$id=$_GET['id'];
$sql="SELECT * FROM `forum_question` WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
if (!$result) { // add this check.
die('Invalid query: ' . mysql_error());
}
$thisql = "SELECT `locked` FROM `forum_question` WHERE `id`='$id'";
$mythisql = mysql_query($thisql);
$mythisql1 = mysql_fetch_array($mythisql);
if ($mythisql1 === false) { // add this check.
die('Invalid query: ' . mysql_error());
}
?>
<table width="700" align="center" class="outer">
<tr>
<td><table width="100%">
<tr>
<td class="back">Back to Forum Home?</td>
</tr>
<tr>
<td><center><h3>
<?php
echo $rows['topic'];
?>
</h3></center></td>
</tr>
<tr>
<td align="right"><?php
if ($user_data['username'] === $rows['name']) {
?>
<form action="lock.php" method="post">
Lock? <input type="checkbox" name="lock" value="1" />
<input type="hidden" name="id" value="<?php echo $rows['id']; ?>" />
<input type="submit" value="Submit">
</form>
<?php
} ?>
</td>
</tr>
<tr>
<td><?php echo $rows['detail']; ?></td>
</tr>
<tr>
<td class="forumreply">By <?php echo $rows['name']; ?>, On <?php echo $rows['datetime']; ?>
</tr>
</table></td>
</tr>
</table>
<BR>
<?php
$tbl_name2="forum_answer"; // Switch to table "forum_answer"
$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
?>
<table width="700" align="center" class="outer">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr><tr>
<td><?php echo $rows['a_answer']; ?></td>
</tr>
<td class="forumreply">By <?php echo $rows['a_name']; ?>, On <?php echo $rows['a_datetime']; ?></td>
</tr>
</table></td>
</tr>
</table><br>
<?php
}
$sql3="SELECT view FROM `forum_question` WHERE id='$id'";
$result3=mysql_query($sql3);
$rows=mysql_fetch_array($result3);
$view=$rows['view'];
// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$sql4="INSERT INTO `forum_question`(view) VALUES('$view') WHERE id='$id'";
$result4=mysql_query($sql4);
}
// count more value
$addview=$view+1;
$sql5="update `forum_question` set view='$addview' WHERE id='$id'";
$result5=mysql_query($sql5);
?>
<?php
if (logged_in() === true) {
if ($mythisql1['locked']===0) {
?>
<BR>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="add_answer.php">
<input type="hidden" value="<?php echo $user_data['username']; ?>" name="a_name">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td valign="top"><strong>Reply</strong></td>
<td valign="top">:</td>
<td><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" value="<?php echo $id; ?>"></td>
<td><input type="submit" name="submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
} else {
echo "Sorry, this post is locked.";
}
}
ob_end_flush();
?>
If anyone can figure out my problem, I will be eternally grateful. Thanks.
I can not see you data so it's a guess
use onyl ==0 not ===0
if (logged_in() === true) {
if ($mythisql1['locked']==0) {
?>
or if $mythisql1['locked'] is a string
if (logged_in() === true) {
if ($mythisql1['locked']=='0') {
?>
This code has multiple problems. SQL Injection is one. This is a bad case alone. Also, when updating the views counter: do it properly in SQL! update forum_question set view=view+1 WHERE id='".mysql_real_escape_string($id)."'". This would eliminate race conditions... Also, the view column is not string (I hope), so you don't need the single quotes around the value... –

PHP table with loop and passing form data to next page

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 " &nbsp&nbsp ";
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...

You have an error in your SQL syntax;

G'Day
I have a php page that I want to edit an entry but for the life of me I can not figure out why it is coming up with this erro.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= po_postcode = '4060', email ='-', phone = '732997688', fax = '' WHERE id='1'' at line 1
HELP I am desperate and going insane. (Similar Code works on another page but not this one)....
Can someone PLEASE HELP.
{
<?php
/*
EDIT.PHP
Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($id, $name, $po_street, $po_suburb, $po_state, $po_postcode, $email, $phone, $fax, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<table width="347" border="0" align="center">
<tr valign="baseline">
<td align="right" nowrap="nowrap"><p align="center"><img src="hartwell_banner.JPG" width="624" height="134" /></p>
</tr>
</table>
<table align="center">
<tr valign="baseline">
<td width="290" align="right" nowrap="nowrap"><div align="left"><h2 align="left"><p align="left">Enter a New Contact</p></h2></div></td>
<td width="290" align="center" nowrap="nowrap"><div align="left"><h2 align="center"><p align="center">Return to Index</p>
</h2>
</div></td>
</tr>
</table>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<table align="center">
<tr valign="baseline">
<td width="98" align="right" nowrap="nowrap"><div align="left">ID:</div></td>
<td width="329"><input type="text" name="id" value="<?php echo $id; ?>" size="40" readonly = "readonly" /> * </td>
</tr>
<tr valign="baseline">
<td width="98" align="right" nowrap="nowrap"><div align="left">Name:</div></td>
<td width="329"><input type="text" name="name" value="<?php echo $name; ?>" size="40" /> * </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="left">Postal Street </div></td>
<td><input type="text" name="po_street" value="<?php echo $po_street; ?>" size="40" /> * </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="left">Postal Suburb</div></td>
<td><Input type ="text" name="po_suburb" value="<?php echo $po_suburb; ?> " size="30" maxlength="50" >*</td>
<tr valign="baseline">
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="left">State</div></td>
<td><Input type ="text" name="po_state" value="<?php echo $po_state; ?>" size="5" maxlength="3" /> * </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="left">Postal Postcode</div></td>
<td><Input type ="text" name="po_postcode" value="<?php echo $po_postcode; ?>" size="5" maxlength="4"/> * </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="left">Email:</div></td>
<td><input type="text" name="email" value="<?php echo $email; ?>" size="40" /> * </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="left">Phone:</div></td>
<td><input name="phone" type="text" value="<?php echo $phone; ?>" size="12" maxlength="10" /> * </td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="left">Fax:</div></td>
<td><input name="fax" type="text" value="<?php echo $fax; ?>" size="12" maxlength="10" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td> <input type="submit" name="submit" value="Submit"> * Denotes Required Field<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?> </td>
</tr>
<tr valign="baseline">
<td colspan="2" align="right" nowrap="nowrap"><div align="center"><img src="hartwell_costs.JPG" alt="" width="340" height="147" /></div></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (isset($_POST['id']))
{
// get form data, making sure it is valid
$id = mysql_real_escape_string(htmlspecialchars($_POST['id']));
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$po_street = mysql_real_escape_string(htmlspecialchars($_POST['po_street']));
$po_suburb = mysql_real_escape_string(htmlspecialchars($_POST['po_suburb']));
$po_state = mysql_real_escape_string(htmlspecialchars($_POST['po_state']));
$po_postcode = mysql_real_escape_string(htmlspecialchars($_POST['po_postcode']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
$phone = mysql_real_escape_string(htmlspecialchars($_POST['phone']));
// check that firstname/lastname fields are both filled in
if ($id == '' || $name == '' || $po_street == '' || $po_suburb == ''|| $po_state == '' || $po_postcode == ''|| $email == '' || $phone == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $name, $po_street, $po_suburb, $po_state, $po_postcode, $email, $phone, $fax, $error);
}
else
{
// save the data to the database
mysql_select_db($database_hartwell, $hartwell);
mysql_query("UPDATE contact SET id= '$id', name='$name', po_street ='$po_street', po_suburb = '$po_suburb', po_state = '$po_state', = po_postcode = '$po_postcode', email ='$email', phone = '$phone', fax = '$fax' WHERE id='$id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
{
//if the 'id' isn't valid, display an error
echo 'ID Not Valid!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
mysql_select_db($database_hartwell, $hartwell);
$result = mysql_query("SELECT * FROM contact WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$id = $row['id'];
$name = $row['name'];
$po_street = $row['po_street'];
$po_suburb = $row['po_suburb'];
$po_state = $row['po_state'];
$po_postcode = $row['po_postcode'];
$email = $row['email'];
$phone = $row['phone'];
$fax = $row['fax'];
// show form
renderForm($id, $name, $po_street, $po_suburb, $po_state, $po_postcode, $email, $phone, $fax,'');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'No ID Value!';
}
}
?>
The error is right there in your query, just like the error message says:
, = po_postcode = '$po_postcode',
^
|
+ this doesn't belong here
remove the equal sign here:
'$po_state', = po_postcode
mysql_query("UPDATE contact SET id= '$id', name='$name', po_street ='$po_street', po_suburb = '$po_suburb', po_state = '$po_state', po_postcode = '$po_postcode', email ='$email', phone = '$phone', fax = '$fax' WHERE id='$id'")
So the problem is here = po_postcode = '$po_postcode',

Categories