Jump to content

UPDATE many records from one page - doesn't work...


zurih

Recommended Posts

Hi all,

 

This might related to my earlier post post (http://www.phpfreaks.com/forums/index.php/topic,224617.0.html), but I think it's something else..

 

when I try to update from the code below, it aint doing a thing...

Can you please make something of it?

 

Thanks

 

<?php
$host="localhost"; // Host name
$username="test"; // Mysql username
$password="test"; // Mysql password
$db_name="test1"; // Database name
$tbl_name="menu"; // 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 DB");
mysql_query("SET NAMES utf8;");

// Check if button name "Submit" is active, do this
$cat_id = $_POST['cat_id'];
$menu_id = $_POST['menu_id'];
$cat_title = $_POST['cat_title'];
$file_name = $_POST['file_name'];
$class_name = $_POST['class_name'];
$id = $_POST['id'];

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);
?>
<html>
<head>
	<title>עריכת קטיגוריות</title>
	<style>
	* {font-family: Arial; font-size: 12px;}
	input {direction:ltr; width: 200px}
	h1 {font-size: 20px;}
	</style>
</head>

<body>
	<center>
		<h1>Update All Records</h1>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<tr>
<td>
<table width="750" border="0" cellspacing="1" cellpadding="5">


<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Cat ID</strong></td>
<td align="center"><strong>Menu ID</strong></td>
<td align="center"><strong>Cat Title</strong></td>
<td align="center"><strong>File Name</strong></td>
<td align="center"><strong>Class Name</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="cat_id[]" type="text" id="cat_id" value="<? echo $rows['cat_id']; ?>"></td>
<td align="center"><input name="menu_id[]" type="text" id="menu_id" value="<? echo $rows['menu_id']; ?>"></td>
<td align="center"><input name="cat_title[]" type="text" id="cat_title" value="<? echo $rows['cat_title']; ?>" style="direction:rtl"></td>
<td align="center"><input name="file_name[]" type="text" id="file_name" value="<? echo $rows['file_name']; ?>"></td>
<td align="center"><input name="class_name[]" type="text" id="class_name" value="<? echo $rows['class_name']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="6" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php


if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET cat_id='$cat_id[$i]', menu_id='$menu_id[$i]', cat_title='$cat_title[$i]', file_name='$cat_title[$i]', class_name='$class_name[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:update_all.php");
}
mysql_close();
?>
		</center>
	</body>
</html>

You are checking for if($Submit), yet you have not defined what $Submit is. You probably are looking to define this first.

 

$Submit = $_POST['Submit'];

 

Just tried it, but still the data isn't saved....

 

<?php

// Check if button name "Submit" is active, do this
$Submit = $_POST['Submit']; 
$id = $_POST['id'];
$cat_id = $_POST['cat_id'];
$menu_id = $_POST['menu_id'];
$cat_title = $_POST['cat_title'];
$file_name = $_POST['file_name'];
$class_name = $_POST['class_name'];

if($Submit){
$sql1 = "UPDATE $tbl_name SET cat_id='$cat_id', menu_id='$menu_id', cat_title='$cat_title', file_name='$file_name', class_name='$class_name' WHERE id='$id'";
$result1 = mysql_query($sql1);
}

if($result1){
echo "Saved!";
}

mysql_close();
?>

Do you just get a white page? Do you get an error? Make sure you have your error reporting on E_ALL to make sure all errors will show.

 

With E_ALL I get this before post:

 

Notice: Undefined index: Submit in /home/test/domains/test.com/public_html/test1/update_all.php on line 76

Notice: Undefined index: id in /home/test/domains/test.com/public_html/test1/update_all.php on line 77

Notice: Undefined index: cat_id in /home/test/domains/test.com/public_html/test1/update_all.php on line 78

Notice: Undefined index: menu_id in /home/test/domains/test.com/public_html/test1/update_all.php on line 79

Notice: Undefined index: cat_title in /home/test/domains/test.com/public_html/test1/update_all.php on line 80

Notice: Undefined index: file_name in /home/test/domains/test.com/public_html/test1/update_all.php on line 81

Notice: Undefined index: class_name in /home/test/domains/test.com/public_html/test1/update_all.php on line 82

Notice: Undefined variable: result1 in /home/test/domains/test.com/public_html/test1/update_all.php on line 89

 

And this after post I get no errors...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.