Can't update/change data in MySQL from PHP - 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) );

Related

i want to try fetch data on other page and than update but always show me an error

here is my index page.inserted all the data to the database and also show on the same page but the main problem is that on update.php page I can not retrieve the data
//that main problem is here and I can't be retrieved the data on this page and always sow that: Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\wamp\www\phonebook\update.php on line 12
index.php
<?php require_once('dbconnect.php'); ?>
<html>
<head>
<title> </title>
</head>
<body>
<h1> phone book </h1>
<form method="post">
<table>
<tr>
<td>fname </td><td> <input type="text" name="firstname" required /> </td>
</tr>
<tr>
<td>lname </td><td> <input type="text" name="lastname" required /> </td>
</tr>
<tr>
<td>mobile </td><td> <input type="text" name="mobile" required /> </td>
</tr>
</table>
<input type="submit" name="submit" value="submit" >
</form>
<!-- $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ show $$$$$$$$$$$$$$$$$$$$$$$$$$ -->
<br> data </br>
<table border="1">
<tr>
<th>id</th> <th>firstname</th> <th>lastname</th> <th>mobile</th><th>update</th><th>delete</th>
</tr>
<?php
$conn = mysqli_connect('localhost','root','','phonebook');
$show = mysqli_query($conn,"SELECT * FROM contacts");
while($row = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td>update</td>
<td><a href="delete.php?id=<?php echo $row['id']; ?>" onclick="return confirm('sure want to delete')" >delete</a></td>
</tr>
<?php } ?>
</table>
</body>
</html>
<?php
//require_once("function.php");
//$obj = new data();
if(isset($_POST{"submit"}))
{
//echo "<pre>";print_r($_POST);die;
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$mobile = $_POST['mobile'];
//$obj->insert($fname,$lname,$mobile);
$connect = mysqli_connect('localhost','root','','phonebook');
$insert = mysqli_query($connect,"insert into contacts(firstname,lastname,mobile) values('".$fname."','".$lname."','".$mobile."')");
if ($insert)
{ ?>
<script> alert('record inserted'); </script>
<?php
}
else
{ ?>
<script> alert('record not inserted'); </script>
<?php
}
header('Location:index.php');
}
?>
update.php
//check the code here
<?php require_once('dbconnect.php');
if(isset($_GET['id']) && is_numeric($_GET['id']) )
{
$id=$_GET['id'];
}
?>
<?php
$conn = mysqli_connect('localhost','root','','phonebook');
$result=mysqli_query($conn,"SELECT * FROM contacts WHERE id='$id'");
$fetch=mysql_fetch_array($result);
//$conn = mysqli_connect('localhost','root','','phonebook');
//$show = mysqli_query($conn,"SELECT * FROM contacts");
//while($row = mysqli_fetch_array($show))
?>
<html>
<head>
<title>update page</title>
</head>
<body>
<form method="post" name="update" action="update.php">
<table>
<tr>
<td>fname </td><td> <input type="text" name="firstname" value= "<?php echo $fetch['firstname']; ?>" required /> </td>
</tr>
<tr>
<td>lname </td><td> <input type="text" name="lastname" value="<?php echo $fetch['lastname']; ?>" required /> </td>
</tr>
<tr>
<td>mobile </td><td> <input type="text" name="mobile" value= "<?php echo $fetch['mobile']; ?>" required /> </td>
</tr>
</table>
<input type="submit" name="submit" value="submit" >
</form>
</body>
</html>
Switch to using mysqli_fetch_array() (note the i) instead of mysql_fetch_array
try this:
$conn = mysqli_connect('localhost','root','','phonebook');
$result=mysqli_query($conn,"SELECT * FROM contacts WHERE id='$id'");
$fetch=mysqli_fetch_array($result);
You must not use mysql_*, it's deprecated. Use PDO or MySQLi instead
You shouldn't mix mysql_* and mysqli_*
Just create ONE mysqli instance instead of creating it for every file you have.
Maximize the use of variables too. This way you only have to change something once.
Please sanitize/escape user input before passing it into your SQL query. Otherwise your application is vulnerable to SQL injection attacks.

instead of insert data to 3 table, only the first table get the newly inserted datas ($add_lakas), why?

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="hu">
<title>ADD R.E.</title>
<link rel="stylesheet" href="../style/style.css" media="all" />
</head>
<body>
<form action="Add.php" method="post" enctype="multipart/form-data">
JUST A BIG TABLE FOR INSERT ALL MY DATA FOR THE DB_TABLES
<table>
<tr>
<td>Alap terület(m^2):</td>
<td><input type="text" name="area_title"></td>
</tr>
<tr>
<td>Ár(Ft):</td>
<td><input type="text" name="ar_title"></td>
</tr>
<tr>
<td>Város:</td>
<td><input type="text" name="varos_title"></td>
</tr>
<tr>
<td>Cím(xy utca házszám):</td>
<td><input type="text" name="cim_title"></td>
</tr>
<tr>
<td>ház/társasház/sorház:</td>
<td><input type="text" name="tipus1_title"></td>
</tr>
<tr>
<td>tégla/panel/más:</td>
<td><input type="text" name="tipus2_title"></td>
</tr>
<tr>
<td>Erkély(van=1;nincs=0):</td>
<td><input type="text" name="erkely_title"></td>
</tr>
<tr>
<td>Lift(van=1;nincs=0):</td>
<td><input type="text" name="lift_title"></td>
</tr>
<tr>
<td>Emelet:</td>
<td><input type="text" name="emelet_title"></td>
</tr>
<tr>
<td>Szobák száma:</td>
<td><input type="text" name="szobanum_title"></td>
</tr>
<tr>
<td>Név:</td>
<td><input type="text" name="nev_title"></td>
</tr>
<tr>
<td>Kor:</td>
<td><input type="text" name="kor_title"></td>
</tr>
<tr>
<td>Telefon:</td>
<td><input type="text" name="telefon_title"></td>
<td></td>
</tr>
<tr>
<td><input type="submit" name="click" value="betölt"></td>
</tr>
</table>
** Table ends **
</form>
</div>
</body>
</html>
** PHP **
echo $Terulet=$_POST['area_title'];
$Ar=$_POST['ar_title'];
$Varos=$_POST['varos_title'];
$Cim=$_POST['cim_title'];
$Tipus1=$_POST['tipus1_title'];
$Tipus2=$_POST['tipus2_title'];
$Erkely=$_POST['erkely_title'];
$Lift=$_POST['lift_title'];
$Emelet=$_POST['emelet_title'];
$SzobaNum=$_POST['szobanum_title'];
$Nev=$_POST['nev_title'];
$Kor=$_POST['kor_title'];
$telefon=$_POST['telefon_title'];
if($Terulet=='' OR $Ar=='' OR $Varos=='' OR
$Cim=='' OR $Tipus1=='' OR $Tipus2=='' OR
$Erkely=='' OR $Lift=='' OR $Emelet=='' OR
$SzobaNum=='' OR $Nev=='' OR $Kor=='' OR
$telefon==''){
echo "<script>alert('Fill out all fields!')</script>";
exit();
}
this is what i dont understand how it works:
else
{
$add_lakas="insert into lakas (Terulet,Ar,Varos,Cim,Tipus1,Tipus2,Erkely,Lift,Emelet,SzobaNum)
values ('$Terulet','$Ar','$Varos','$Cim','$Tipus1','$Tipus2','$Erkely','$Lift','$Emelet','$SzobaNum')";
$run_lakas = mysql_query($add_lakas);
mysql_free_result($run_lakas);
$add_elado="insert into elado (Nev,Kor,telefon) values ('$Nev','$Kor','$telefon')";
$run_elado = mysql_query($add_elado);
mysql_free_result($run_elado);
$add_eladas="insert into eladas (Tulajdonos_telefon) values ('$telefon')";
$run_eladas = mysql_query($add_eladas);
if($run_eladas )
{
echo "<script>alert('upload done!')</script>";
echo "<script>window.open('Add.php','_self')</script>";
}
mysql_free_result($run_eladas);
}
}
?>
** I see new rows in phpmyadmin whitin table lakas, but only there**
try n debug your code with
$run_lakas = mysql_query($add_lakas) or die('Invalid query: <br/>'.$add_lakas.' <br/>' . mysql_error());
$run_elado = mysql_query($add_elado) or die('Invalid query: <br/>'.$add_elado.' <br/>' . mysql_error());
$run_eladas = mysql_query($add_eladas) or die('Invalid query: <br/>'.$add_eladas.' <br/>' . mysql_error());
Please be informed that mysql functions are deprecated and not recommended. USE MySQLi or PDO instead. have a reference from following queries.
http://php.net/manual/en/book.mysqli.php
http://php.net/manual/en/book.pdo.php

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

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

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

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