Script won't update in my db

Hello,
I’m currently testing a script (Update multiple rows in mysql form) which i found on a website, but I can’t get to work.
It shows the form perfectly, but I want to be able to update the values (as it’s supposed to with this script)

When I press “Submit” the old values pops back on the form.

Any ideas what could be wrong with this script?

SQL

[code]CREATE TABLE test_mysql (
id int(4) NOT NULL auto_increment,
name varchar(65) NOT NULL default ‘’,
lastname varchar(65) NOT NULL default ‘’,
email varchar(65) NOT NULL default ‘’,
PRIMARY KEY (id)
) TYPE=MyISAM AUTO_INCREMENT=7 ;

– Dumping data for table test_mysql

INSERT INTO test_mysql VALUES (1, ‘Billly’, ‘Blueton’, ‘[email protected]’);
INSERT INTO test_mysql VALUES (2, ‘Jame’, ‘Campbell’, ‘[email protected]’);
INSERT INTO test_mysql VALUES (3, ‘Mark’, ‘Jackson’, ‘[email protected]’);
INSERT INTO test_mysql VALUES (4, ‘Linda’, ‘Travor’, ‘[email protected]’);
INSERT INTO test_mysql VALUES (5, ‘Joey’, ‘Ford’, ‘[email protected]’);
INSERT INTO test_mysql VALUES (6, ‘Sidney’, ‘Gibson’, ‘[email protected]’);[/code]

PHP
[php]<?php

$host=“localhost”; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=“test”; // Database name
$tbl_name=“test_mysql”; // 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”);

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

// Count table rows
$count=mysql_num_rows($result);
?>

<?php while($rows=mysql_fetch_array($result)){ ?> <?php } ?>
Id Name Lastname Email
<? $id[]=$rows['id']; ?><? echo $rows['id']; ?>
<?php // Check if button name "Submit" is active, do this if($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){ header("location:update_multiple.php"); } mysql_close(); ?>[/php]

Thanks alot in advance!

Robert

Personally I would find a different script to use. The coding is sloppy and I don’t trust sloppy code.

but to fix it try changing the update query to this…

[php]
$sql1 = “UPDATE $tbl_name SET name=’”.$_POST[‘name’][$i]."’, lastname=’".$_POST[‘lastname’][$i]."’, email=’".$_POST[‘email’][$i]."’ WHERE id=’".$_POST[‘id’][$i]."’";
[/php]

Its not sloppy, just not coded right.

You can’t use the $count from the top of the page. It doesn’t necessarily represent everyone that’s being updated

you’re not going to update anything with that script, it doesn’t know who is who. add a hidden field in that form with the person’s id. Assuming the hidden field is named id, the bottom piece should be
[php]if(isset($_POST[‘submit’])) {
$count = count($_POST[‘id’]); // now $count actually equals something
for($i=0; $i<$count; $i++) {
$result1 = mysql_query(“UPDATE $tbl_name SET name=’$_POST[name][$i]’, lastname=’$_POST[lastname][$i]’, email=’$_POST[‘email’][$i]’ WHERE id = $_POST[‘id’][$i]”);
}
if(!result1) {
$error = mysql_error();
}
if(!$error){
header(“location:update_multiple.php”);
}
mysql_close();
}
[/php]

Personally, doing this:
$host=“localhost”; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=“test”; // Database name
$tbl_name=“test_mysql”; // Table name

is pretty useless unless you’re on a large site or using templates. It saves a lot of headaches to just put the information directly in the needed spots. saves lines too.
[php]mysql_connect(“localhost”, “”, “”) or die(“cannot connect”);
mysql_select_db(“test”) or die(“cannot select DB”);

$sql=“SELECT * FROM test_mysql”[/php]

Thanks guys, worked fine! You’re the best. :smiley:

Another question, i’d like to split up the rows into different sections. Like the table below.
I tried with having e.g. 3 tables, one for each group. But I couldnt get the script to update all three tables with 1 form.

Is this possible somehow?

Thanks again!

[table]
[tr]
[td]Group 1
Emails Name
Phone 1 Name
Phone 2 Name
Fax Name
Orders Name
[/td]
[td]Group 2
Emails Name
Phone 1 Name
Phone 2 Name
Fax Name
Orders Name
[/td]
[td]Group 3
Emails Name
Phone 1 Name
Phone 2 Name
Fax Name
Orders Name
[/td]
[/tr]
[/table]

Its possible.

Anyone who can guide me?

<?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // 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"); $sql="SELECT * FROM '$tbl_name'"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?>
<?php $i = 1; while($rows=mysql_fetch_array($result)){ ?> <?php $i = ++; } ?>
Id Name Lastname Email
<? echo $rows['id']; ?>
<?php // Check if button name "Submit" is active, do this if(isset($_POST["Submit"])){ $ti = $_POST["ti"]; for($i=1;$i<=$ti;$i++){ $name = $_POST["name".$i]; $lastname = $_POST["lastname".$i]; $email = $_POST["email".$i]; $id = $_POST["h".$i]; $sql1="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'"; $result1=mysql_query($sql1); } } if($result1){ header("location:update_multiple.php"); } mysql_close(); ?>

I have tested this code. it works for me. I have modified your code a litle. may work for u as well if YEs. Let me know.

Sponsor our Newsletter | Privacy Policy | Terms of Service