PHP & MYSQL

Upadating multiple row in mysql with PHP

Page 1 of 1

4 Replies - 1965 Views - Last Post: 22 October 2009 - 10:13 AM Rate Topic: -----

#1 yemi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 08

PHP & MYSQL

Posted 15 October 2009 - 07:41 AM

Hello fellow programmer, i ' m having a little problem updating multiple row in MYSQL with PHP Below is the 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]');

//update_multiple.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);
?>
<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($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();
?>



Is This A Good Question/Topic? 0
  • +

Replies To: PHP & MYSQL

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: PHP & MYSQL

Posted 15 October 2009 - 03:08 PM

Where do you set $Submit? It looks to me like it will always be false.
Was This Post Helpful? 0
  • +
  • -

#3 jaql   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 19-October 09

Re: PHP & MYSQL

Posted 21 October 2009 - 06:15 AM

mysql_query() does not support multiple queries.
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: PHP & MYSQL

Posted 21 October 2009 - 07:26 AM

View Postjaql, on 21 Oct, 2009 - 05:15 AM, said:

mysql_query() does not support multiple queries.

No but it does support a single query like he's trying to do, that will update multiple rows.
Was This Post Helpful? 0
  • +
  • -

#5 ahmedelhamahmy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-October 09

Re: PHP & MYSQL

Posted 22 October 2009 - 10:13 AM

Where is the problem ??? if you can attache the php file it would be easier to read it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1