Where do I set the $id variable in this PHP code? - php

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'];

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) );

Updating data using PHP Edit form help needed

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());;

Update multiple rows in mysql with php

Here is my code below. The problem is when I try to update info it instead clears all records and does not update. How can I get this script to update and not clear. Also, I have used this before and it worked fine but all the sudden it doesn't.. I might have removed something important.
<strong>Update multiple rows in mysql</strong><br>
<?php
$mysql_host = "mysql.com";
$mysql_user = "username";
$mysql_pass = "password";
$mysql_database = "dbname";
$tbl_name="test_mysql"; // Table name
// Connect to server and select databse.
mysql_connect("$mysql_host", "$mysql_user", "$mysql_pass")or die("cannot connect");
mysql_select_db("$mysql_database")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
$id = array();
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" 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(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
echo "Good";
////header("location:update_multiple.php");
}
mysql_close();
?>
You have using wrong set of variables,
try
$name[$i] <-- access local variable, an array called $name
$_POST["name"][$i] <-- access $_POST, the form name instead
I would suggest you make use $row["id"] as index key (name[$row["id"]]),
instead of using sequential indexed (key (0, 1, 2...)
None of your variables are defined in your SQL ($name, $lastname, $email, $id).
In your for loop, use $_POST['name'][$i] and so forth.
Also, it seems like you have forgotten to put your id in some form (hidden) field?
try this:
<?php require_once('Connections/tlsc_conn.php');
mysql_select_db($database_tlsc_conn, $tlsc_conn);
$query_Recordset1 = "SELECT * FROM tbl_name";
$Recordset1 = mysql_query($query_Recordset1, $tlsc_conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
if(isset($_POST['submit'])) {
// $count = count($_POST['id']);
// $count=mysql_num_rows($Recordset1);
$submit = $_GET['submit'];
$i = ($_POST['count']);
$name = ($_POST['name']);
$lastname = ($_POST['lastname']);
$email = ($_POST['email']);
$id = ($_POST['id']);
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='{$_POST['name'][$i]}',
lastname='{$_POST['lastname'][$i]}',
email='{$_POST['email'][$i]}'
WHERE id='{$_POST['id'][$i]}'";
$row_Recordset1=mysql_query($sql1);
}
if($row_Recordset1){
header("location:lulu.php");
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form name="form2" method="post" action="">
<table width="634" border="1">
<tr>
<td>id</td>
<td>name</td>
<td>lastname</td>
<td>email</td>
</tr>
<?php do { ?>
<tr>
<td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?>
<input name="id[]" type="hidden" value="<?php echo $row_Recordset1['id']; ?>" /></td>
<td><input name="name[]" type="text" value="<?php echo $row_Recordset1['name']; ?>"></td>
<td><input name="lastname[]" type="text" value="<?php echo $row_Recordset1['lastname']; ?>"></td>
<td><input name="email[]" type="text" value="<?php echo $row_Recordset1['email']; ?>"> </td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<p>
</p>
</body>
</html>
Used this command to change multiple entries in the database:
$sql = "UPDATE users SET name = ?, lastname = ?, email = ? WHERE id = '{$_SESSION['id']}'";

PHP & MySQL does not update database

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.

php error Undefined variable: result1 help!

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.

Categories