i am trying to update my table with the correct information linking to the follwing ID. I will have posted the code so you can all take a look.
Here is my problem: Once i submit the form with all the details recieved from the database, everything works successfull. But when i click submit it re-directs to my other page called update_ac.php. Everything works fine, apart from the data in the mysql tables do not get updated.
I wonder if anyone could take a look at the code to see what they think:much appreciated and feedback would be fantastic. PS I am not the best PHP programmer, still learning!
So here is edit.php - where teh user edits their information:
<?php
session_start();
$UserName = $_SESSION['UserName'];
require("checkLoginSession.php");
$adminid = $_GET['id'];
//CONNECTION CODE WAS HERE
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
echo("Logged In As: $UserName");
echo "<br />";
echo("We are editing Data for ID: $adminid");
echo "<br />";
echo "<a href=test.php>Go back to panel</a>";
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM admin WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Main Content</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
mysql_close();
?>
<!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>Edit Page</title>
</head>
<body>
<h2>Edit Page (<?php echo ("$adminid"); ?>)</h2>
</body>
</html>
And here is the update_ac.php:
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$firstName = $_POST["name"];
$mainText = $_POST["mainContent"];
$sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());;
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='test.php'>Back to panel</a>";
}
else {
echo "ERROR";
}
?>
OK GUYS EDIT HERE:
If in the Update_ac.php if i change the following line to this:
$sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='1'";
The information now gets updated, therefore means something is going wrong with my ID variable
Looks like the typical omission of database escaping. You need to apply mysql_real_escape_string on any string that you concat in your sql query.
The lazy version is:
$_POST = array_map("mysql_real_escape_string", $_POST);
$firstName = $_POST["name"];
$mainText = $_POST["mainContent"];
Otherwise you will oftentimes get an invalid syntax error. Use print mysql_error(); after the query to find out what went wrong in your case.
See also String escaping for each database or read up on pdo for less fiddly database interaction.
From just a quick glance, it's because you are using variables and not the $_POST variables
$sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='$id'";
Should be the post variables with the names of your form fields
$sql="UPDATE admin SET name='$_POST['firstName'], mainContent='$_POST['mainText']' WHERE id='$_POST['id']'";
If you then put your $id variable within a hidden input field within your form, that file will also pick it up.
Just a quick answer cos' I'm out to get lunch.
It is just a syntax error in the update_ac.php
You used 2 semicolons in the following line
$result=mysql_query($sql) or die(mysql_error());;
Related
I try to use the below code to update multiple rows, the below code can view the results of rows but it can not be updated, where is wrong place? How to modify it ?
<?php
$host="localhost"; // Host name
$username="abc"; // Mysql username
$password="abc123"; // Mysql password
$db_name="abc"; // Database name
$tbl_name="BRAddress"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("Cannot connect");
mysql_select_db("$db_name")or die("Cannot select Database");
$sql="SELECT * FROM $tbl_name WHERE br_no='62779457'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>BR No.</strong></td>
<td align="center"><strong>Date of Register</strong></td>
<td align="center"><strong>Address</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<? $br_no[]=$rows['br_no']; ?><? echo $rows['br_no']; ?>
</td>
<td align="center">
<input name="br_date_of_register[]" type="date" id="br_date_of_register" value="<? echo $rows['br_date_of_register']; ?>">
</td>
<td align="center">
<input name="br_address[]" type="text" size="60" id="br_address" value="<? echo $rows['br_address']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET
br_date_of_register='$br_date_of_register[$i]',
br_address='$br_address[$i]'
WHERE br_no='$br_no[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:update_sample.php");
}
mysql_close();
?>
Thank you very much for your help & support !
I think that you need to change this part
if($Submit){
to
if($_POST('Submit')){
I haven't run the whole or looked at entire code, but you have nothing that defines $Submit variable though from what I see.
Or you can put in
$Submit = $_POST('Submit');
before the if statement.
Let me know how you go.
Cheers
Your POST variable are empty. $br_date_of_register has no value. You must use this like following
$br_date_of_register = $_POST[br_date_of_register];
$br_address = $_POST[br_address];
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET
br_date_of_register='$br_date_of_register[$i]',
br_address='$br_address[$i]'
WHERE br_no='$br_no[$i]'";
$result1=mysql_query($sql1);
}
Edit
if($Submit)
To
if($_SERVER['REQUEST_METHOD'] == "POST")
Considering your Submit check,
you can use this,
if(isset($_POST["Submit"]))
{
}
Further in your SQL statement, do this,
$sql1='UPDATE ' . $tbl_name . ' SET
br_date_of_register = ' . $br_date_of_register[$i] .
' , br_address = ' . $br_address[$i] .
' WHERE br_no = ' . $br_no[$i];
I am creating a user panel in which users can register and upload a image. Once they login they may decide to edit that image therefore I am wanting to perform a update query to add a new image to the location and attach the new image to their mysql data.
So whats happening when i submit the code: I get a error saying:: Sorry, there was a problem uploading your file.
Hopefully someone can let me know why this is giving me a error. PS I am already using this location when they first register to upload a file. But surely this shouldnt have any problem. Another issue i found is that nothing is being successfully inserted into the MYSQL Table for the photo.
So here is the code:
Table: admin
CREATE TABLE IF NOT EXISTS `admin` (
`id` int(3) NOT NULL auto_increment,
`UserName` varchar(30) default NULL,
`PassWord` varchar(30) default NULL,
`name` varchar(30) default NULL,
`mainContent` varchar(2000) default NULL COMMENT 'maincontent test',
`photo` varchar(400) NOT NULL COMMENT 'Picture1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=64 ;
So lets imagine the user has logged into their user panel: and they click the edit button linking to their ID.
So they appear at the edit page:
EDIT.PHP
<?php
session_start();
$UserName = $_SESSION['UserName'];
require("checkLoginSession.php");
$adminid = $_GET['id'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
echo("Logged In As: $UserName");
echo "<br />";
echo("We are editing Data for ID: $adminid");
echo "<br />";
echo "<a href=test.php>Go back to panel</a>";
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM admin WHERE id='$id'";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Main Content</strong></td>
<td align="center"><strong>Image Locatoin</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" size="15"></td>
<td align="center"><input name="photo" type="file" id="photo">
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
mysql_close();
?>
<!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>Edit Page</title>
</head>
<body>
<h2>Edit Page (<?php echo ("$adminid"); ?>)</h2>
<img src="backtopanel.jpg" alt="back to panel" width="454" height="85" border="0" longdesc="back to panel" />
</body>
</html>
update_ac.php
<?php
session_start();
$UserName = $_SESSION['UserName'];
require("checkLoginSession.php");
include "common.php";
DBConnect();
$Link = mysql_connect($Host, $User, $Password);
$id = $_POST['id'];
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$_POST = array_map("mysql_real_escape_string", $_POST);
$firstName = $_POST["name"];
$mainText = $_POST["mainContent"];
$pic=($_FILES['photo']['name']);
//
$sql="UPDATE admin SET name='$firstName', mainContent='$mainText', photo='$pic' WHERE id='$id'";
echo $sql;
//
if (mysql_db_query ($DBName, $sql, $Link)){
print ("A record was created <br><a href=index.php> return to index </a>\n");
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
} else {
print ("Record not created");
}
mysql_close($Link);
?>
A few things:
If nothing is coming through in $_FILES then you have probably missed out enctype="multipart/form-data" in the form tag of the submitting page where the image is being specified.
When handling the uploaded file you need to use $_FILES['inputName']['tmp_name'] to get the temp location of the file then move this using move_uploaded_file($_FILES['inputName']['tmp_name'], 'destination'); and store the destination value in the DB record.
On the edit page where you want to display the image you will need to have the image itself displayed (using and img tag) as well as a file input for changing the image if required.
I could go on, but I really think you need to go read some tutorials on form / file handling in PHP as would most which is why you have not received any replies...
I'm troubleshooting this code, I am very new to PHP, so any help would be appreciated.
I think the error is coming from not having the $id variable set anywhere, so where do I set it?
Here is the code:
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE depot = 'plainview'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
//error reporting
error_reporting(E_ALL); ini_set('display_errors', '1');
$result1 = false;
//update
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET
available='".mysql_real_escape_string($_POST['available'][$i])."',
rent='".mysql_real_escape_string($_POST['rent'][$i])."',
corp_ready='".mysql_real_escape_string($_POST['corp_ready'][$i])."',
down='".mysql_real_escape_string($_POST['down'][$i])."',
gfs='".mysql_real_escape_string($_POST['gfs'][$i])."',
dateTime = NOW()
WHERE id='".$id[$i]."'";
$result1 = mysql_query($sql1) or die(mysql_error());
}
}
//redirect
if($result1){
header("location: success.php");
}
else
header("location: fail.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=UTF-8" />
<script language="JavaScript1.1" type="text/javascript">
<!--
function mm_jumpmenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<title>Untitled Document</title>
</head>
<body>
<div>
<p>Plainview, North East Region</p>
<p>Select a different region: <select onchange="mm_jumpmenu('parent',this,0)" name="lostlist">
<option value="" selected="selected">Choose Your Depot</option>
<option value="plainview.php">Plainview</option>
<option value="worcrester.php">Worcrester</option>
</select></p>
</div><Br />
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="700" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>ID</td>
<td align="center"><strong>Product Name</strong></td>
<td align="center"><strong>Available</strong></td>
<td align="center"><strong>Rent</strong></td>
<td align="center"><strong>Corp Ready</strong></td>
<td align="center"><strong>Down</strong></td>
<td align="center"><strong>GFS</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="left"><?php echo $rows['product']; ?></td>
<td align="center"><input name="available[]" type="text" id="available" value="<?php echo $rows['available']; ?>" size="5"></td>
<td align="center"><input name="rent[]" type="text" id="rent" value="<?php echo $rows['rent']; ?>" size="5"></td>
<td align="center"><input name="corp_ready[]" type="text" id="corp_ready" value="<?php echo $rows['corp_ready']; ?>" size="5"></td>
<td align="center"><input name="down[]" type="text" id="down" value="<?php echo $rows['down']; ?>" size="5" /></td>
<td align="center"><input name="gfs[]" type="text" id="gfs" value="<?php echo $rows['gfs']; ?>" size="5"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
mysql_close();
?>
</body>
</html>
Per the statement:
if($result1){
header("location: success.php");
}
else
header("location: fail.php");
Every time I try to access the page it redirects to fail.php, so why does it fail?
Either $_POST['Submit'] is not set, or $count is 0.
if($result1 === true){
header("location: success.php");
} else {
header("location: fail.php");
}
Maybe I missed a line, But I don't see where you put a value to $id... The first time you mention it is where you expect to receive a value from it.
If you don't post anything, $result1 will remain false.
If you post something, the test will only be done on the last iteration of your loop, so every other UPDATE could fail but the last one, it would be considered as a success.
Btw, why do you have some code after your redirection? It will obviously never be displayed.
You also need to exit() after any header('Location') to avoid unwanted code execution (code that would remain after the header call).
I think you might want to include the header()instructions INSIDE the if(isset($_POST['Submit'])) test to avoid being redirected just by loading the page without posting anything.
Location should also be a complete URL with protocol and domain, even if most browser will accept a partial own, this is violating some RFCs.
You also need to loop through results to defined $id:
while ($row = mysql_fetch_assoc($result)) $id[] = $row['primary_column_name'];
could anyone look this code over and tell me why it doesnt update the database? I know its hacked together, I am a novice at PHP
thanks in advance
<?php
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE depot = 'plainview'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
//update
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET
available='{$_POST['available'][$i]}',
rent='{$_POST['rent'][$i]}',
corp_ready='{$_POST['corp_ready'][$i]}',
down='{$_POST['down'][$i]}',
gfs='{$_POST['gfs'][$i]}',
dateTime = NOW()
WHERE id='$id[$i]'";
$result1 = mysql_query($sql1) or die(mysql_error());
}
}
//redirect
if($result1){
header("location: plainview.php");
}
mysql_close();
?>
======================
entire code
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE depot = 'plainview'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
//update
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET
available='".mysql_real_escape_string($_POST['available'][$i])."',
rent='".mysql_real_escape_string($_POST['rent'][$i])."',
corp_ready='".mysql_real_escape_string($_POST['corp_ready'][$i])."',
down='".mysql_real_escape_string($_POST['down'][$i])."',
gfs='".mysql_real_escape_string($_POST['gfs'][$i])."',
dateTime = NOW()
WHERE id='".$id[$i]."'";
$result1 = mysql_query($sql1) or die(mysql_error());
}
}
//redirect
if($result1){
header("location: plainview.php");
}
mysql_close();
?>
<!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" />
<script language="JavaScript1.1" type="text/javascript">
<!--
function mm_jumpmenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<title>Untitled Document</title>
</head>
<body>
<div>
<p>Plainview, North East Region</p>
<p>Select a different region: <select onchange="mm_jumpmenu('parent',this,0)" name="lostlist">
<option value="" selected="selected">Choose Your Depot</option>
<option value="plainview.php">Plainview</option>
<option value="worcrester.php">Worcrester</option>
</select></p>
</div><Br />
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="700" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>ID</td>
<td align="center"><strong>Product Name</strong></td>
<td align="center"><strong>Available</strong></td>
<td align="center"><strong>Rent</strong></td>
<td align="center"><strong>Corp Ready</strong></td>
<td align="center"><strong>Down</strong></td>
<td align="center"><strong>GFS</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="left"><?php echo $rows['product']; ?></td>
<td align="center"><input name="available[]" type="text" id="available" value="<?php echo $rows['available']; ?>" size="5"></td>
<td align="center"><input name="rent[]" type="text" id="rent" value="<?php echo $rows['rent']; ?>" size="5"></td>
<td align="center"><input name="corp_ready[]" type="text" id="corp_ready" value="<?php echo $rows['corp_ready']; ?>" size="5"></td>
<td align="center"><input name="down[]" type="text" id="down" value="<?php echo $rows['down']; ?>" size="5" /></td>
<td align="center"><input name="gfs[]" type="text" id="gfs" value="<?php echo $rows['gfs']; ?>" size="5"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
echo "$sql1";
?>
</body>
</html>
Try echo "id='$id[$i]'<br />"; and see if you are getting the ID's that should be sent.
Always escape all user input. Your update query should look like
$sql1 = "UPDATE $tbl_name SET
available='".mysql_real_escape_string($_POST['available'][$i])."',
rent='".mysql_real_escape_string($_POST['rent'][$i])."',
corp_ready='".mysql_real_escape_string($_POST['corp_ready'][$i])."',
down='".mysql_real_escape_string($_POST['down'][$i])."',
gfs='".mysql_real_escape_string($_POST['gfs'][$i])."',
dateTime = NOW()
WHERE id='".$id[$i]."'";
Has $tbl_name been set outside of the code you've provided? If not, set it so your sql knows what table to work with.
I'm trying to troubleshoot this code and I'm getting this error and I do not know how to fix it: Undefined variable: result1 in plainview.php on line 44
here is the code:
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE depot = 'plainview'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
//error reporting
error_reporting(E_ALL); ini_set('display_errors', '1');
//update
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET
available='".mysql_real_escape_string($_POST['available'][$i])."',
rent='".mysql_real_escape_string($_POST['rent'][$i])."',
corp_ready='".mysql_real_escape_string($_POST['corp_ready'][$i])."',
down='".mysql_real_escape_string($_POST['down'][$i])."',
gfs='".mysql_real_escape_string($_POST['gfs'][$i])."',
dateTime = NOW()
WHERE id='".$id[$i]."'";
$result1 = mysql_query($sql1) or die(mysql_error());
}
}
//redirect
if($result1){
header("location: plainview.php");
}
mysql_close();
?>
<!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" />
<script language="JavaScript1.1" type="text/javascript">
<!--
function mm_jumpmenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<title>Untitled Document</title>
</head>
<body>
<div>
<p>Plainview, North East Region</p>
<p>Select a different region: <select onchange="mm_jumpmenu('parent',this,0)" name="lostlist">
<option value="" selected="selected">Choose Your Depot</option>
<option value="plainview.php">Plainview</option>
<option value="worcrester.php">Worcrester</option>
</select></p>
</div><Br />
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="700" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>ID</td>
<td align="center"><strong>Product Name</strong></td>
<td align="center"><strong>Available</strong></td>
<td align="center"><strong>Rent</strong></td>
<td align="center"><strong>Corp Ready</strong></td>
<td align="center"><strong>Down</strong></td>
<td align="center"><strong>GFS</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="left"><?php echo $rows['product']; ?></td>
<td align="center"><input name="available[]" type="text" id="available" value="<?php echo $rows['available']; ?>" size="5"></td>
<td align="center"><input name="rent[]" type="text" id="rent" value="<?php echo $rows['rent']; ?>" size="5"></td>
<td align="center"><input name="corp_ready[]" type="text" id="corp_ready" value="<?php echo $rows['corp_ready']; ?>" size="5"></td>
<td align="center"><input name="down[]" type="text" id="down" value="<?php echo $rows['down']; ?>" size="5" /></td>
<td align="center"><input name="gfs[]" type="text" id="gfs" value="<?php echo $rows['gfs']; ?>" size="5"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
</body>
</html>
Please help! I'm an amateur at PHP and any help would be appreciated....
$result1 is only getting set inside a for loop, inside an if statement. If the if fails, it's never defined, so the if($result1) gives that error. Set $result1 = false before your if statement. Better yet, move the if statement to surround the entire code block, including the database connection and disconnection stuff.
In line 44 $result is undefined, if
if ( isset( $_POST['Submit'] )) {
...
$result1 = mysql_query($sql1) or die(mysql_error());
}
deflects its block, since you didn't posted the request or $_POST['Submit'] isn't defined.
Preset $result1 with a default value of FALSE.
You only define $result1 if the script was invoked via a POST operation. As such,
if($result1){
header("location: plainview.php");
}
will issue that warning anytime you're NOT in a POST situation.
As well, on another note, you're closing the mysql connection BEFORE you fetch rows to generate your HTML. This will kill your page output, as the results have not been "fetched" yet at the time you close the connection.