468,247 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

what is wrong with my PHP script for MySQL updating?

Dear all,
my PHP aims to update a MySQL database by selecting record
one by one and modify then save.

Here are my PHP, but I found that it doesnt work as it supposed to be,
for example, when Record (i) is shown and modified, the change will
come to Record (i+1).

Can anyone provide suggestion?
thanks.
<?php
$connect = mysql_connect("andy","kiong","Password")or
die("Could not connect : " . mysql_error()); ;
mysql_select_db("kiong_userinfo",$connect)or die("Could not
connect : " . mysql_error());
$result = mysql_query("SELECT * FROM putonServer where id2=1",
$connect);
printf("%s<br>\n", mysql_result($result,0,"body"));

?>
<table width="556" height="70" border="0"
align="center">
<tr>
<td width="59" height="66">&nbsp;</td>
</tr>
</table>
<p align="center">
<?php

$this_id=mysql_result($result,0,"id");
$next_id=$this_id+1;
$cat=mysql_result($result,0,"category");
$newline = "<br />";

if($cat==1)
{
echo "Process".$newline;
echo $newline;
}elseif($cat==2)
{
echo "Product".$newline;
echo $newline;
}else{
echo "People".$newline;
echo $newline;
}

$ori=mysql_result($result,0,"orientation");

echo "$ori";

?>

<?php
$connect = mysql_connect("Server","kiong","Password")or
die("Could not connect : " . mysql_error());
$db_selected = mysql_select_db("kiong_userinfo",
$connect);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
if($_POST[B2]){
$query_B2 ="UPDATE putonServer SET
category='$_POST[R2]',orientation='$_POST[sliderinput22]' where
id='$this_id'";
echo $this_id.$newline;
$result_B2=mysql_query($query_B2,$connect);
if (!$result_B2) {
die ('Puke, Wrong2 ' . mysql_error());
}
$query_B2_1="update putonServer SET id2=4 where
id='$this_id'";
echo $this_id.$newline;
$query_B2_2="update putonServer SET id2=1 where
id='$next_id'";
echo $this_id.$newline;
$result_B2_2=mysql_query($query_B2_2,$connect);
if (!$result_B2_2) {
die ('Puke, Wrong ' . mysql_error());
}
$result_B2_1=mysql_query($query_B2_1,$connect);
if (!$result_B2_1) {
die ('Puke, Wrong2 ' . mysql_error());
}

}elseif($_POST[B0]){
$query_B0="update putonServer SET id2=7 where id='$this_id'";
$query_B0_2="update putonServer SET id2=1 where
id='$next_id'";

$result_B0=mysql_query($query_B0,$connect);
if (!$result_B0) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B0_2=mysql_query($query_B0_2,$connect);
if (!$result_B0_2) {
die ('Puke, Wrong ' . mysql_error());
}

}elseif($_POST[B1]){

$query_B1="update putonServer SET id2=0 where id='$this_id'";
$query_B1_2="update putonServer SET id2=1 where
id='$next_id'";

$result_B1=mysql_query($query_B1,$connect);
if (!$result_B1) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B1_2=mysql_query($query_B1_2,$connect);
if (!$result_B1_2) {
die ('Puke, Wrong ' . mysql_error());
}
}
?>

Apr 2 '07 #1
3 1925
Si*******@gmail.com wrote:
Dear all,
my PHP aims to update a MySQL database by selecting record
one by one and modify then save.

Here are my PHP, but I found that it doesnt work as it supposed to be,
for example, when Record (i) is shown and modified, the change will
come to Record (i+1).

Can anyone provide suggestion?
thanks.
<?php
$connect = mysql_connect("andy","kiong","Password")or
die("Could not connect : " . mysql_error()); ;
mysql_select_db("kiong_userinfo",$connect)or die("Could not
connect : " . mysql_error());
$result = mysql_query("SELECT * FROM putonServer where id2=1",
$connect);
printf("%s<br>\n", mysql_result($result,0,"body"));

?>
<table width="556" height="70" border="0"
align="center">
<tr>
<td width="59" height="66">&nbsp;</td>
</tr>
</table>
<p align="center">
<?php

$this_id=mysql_result($result,0,"id");
$next_id=$this_id+1;
$cat=mysql_result($result,0,"category");
$newline = "<br />";

if($cat==1)
{
echo "Process".$newline;
echo $newline;
}elseif($cat==2)
{
echo "Product".$newline;
echo $newline;
}else{
echo "People".$newline;
echo $newline;
}

$ori=mysql_result($result,0,"orientation");

echo "$ori";

?>

<?php
$connect = mysql_connect("Server","kiong","Password")or
die("Could not connect : " . mysql_error());
$db_selected = mysql_select_db("kiong_userinfo",
$connect);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
if($_POST[B2]){
$query_B2 ="UPDATE putonServer SET
category='$_POST[R2]',orientation='$_POST[sliderinput22]' where
id='$this_id'";
echo $this_id.$newline;
$result_B2=mysql_query($query_B2,$connect);
if (!$result_B2) {
die ('Puke, Wrong2 ' . mysql_error());
}
$query_B2_1="update putonServer SET id2=4 where
id='$this_id'";
echo $this_id.$newline;
$query_B2_2="update putonServer SET id2=1 where
id='$next_id'";
echo $this_id.$newline;
$result_B2_2=mysql_query($query_B2_2,$connect);
if (!$result_B2_2) {
die ('Puke, Wrong ' . mysql_error());
}
$result_B2_1=mysql_query($query_B2_1,$connect);
if (!$result_B2_1) {
die ('Puke, Wrong2 ' . mysql_error());
}

}elseif($_POST[B0]){
$query_B0="update putonServer SET id2=7 where id='$this_id'";
$query_B0_2="update putonServer SET id2=1 where
id='$next_id'";

$result_B0=mysql_query($query_B0,$connect);
if (!$result_B0) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B0_2=mysql_query($query_B0_2,$connect);
if (!$result_B0_2) {
die ('Puke, Wrong ' . mysql_error());
}

}elseif($_POST[B1]){

$query_B1="update putonServer SET id2=0 where id='$this_id'";
$query_B1_2="update putonServer SET id2=1 where
id='$next_id'";

$result_B1=mysql_query($query_B1,$connect);
if (!$result_B1) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B1_2=mysql_query($query_B1_2,$connect);
if (!$result_B1_2) {
die ('Puke, Wrong ' . mysql_error());
}
}
?>
You make a db conection twice, do I see correctly these are two separate
scripts you posted here? If so, where does the second get the values for
$this_id etc? They are set in the first script, I can't see if/how they
are parsed to the second one.

Besides, it's quite hard to tell what is going on in the scripts. No
documentation whatsoever and some cryptic variables/values here and
there. Tough to decipher.

Apr 2 '07 #2
thank, Schraalhans.

I modified the code for your reading.
the problem is still there.

let's begin from the very beginning, current behavior is that if
initial value from id=99, the UpdMySQL of id=99 will be perform OK
with row 99, but the $cat/$ori in row 100 will also be changed to the
same as row 99.then content("body") of row 99 will be shown
again(thepage keep no change ), although in fact, row 100's id2=1;
then when DelMySQL/UpdMySQL/AgrMySQL is performed, the update fields
go to the next row of the row which content("body") is shown on the
website.

<?php
//Connect to the database
$connect = mysql_connect("andy","xiong","KIN7game")or die("Could not
connect : " . mysql_error()); ;
mysql_select_db("xiong_userinfo",$connect)or die("Could not
connect : " . mysql_error());
//select the record to be read.
$result = mysql_query("SELECT * FROM putonServer where id2=1",
$connect);
$newline = "<br />";
$row = mysql_fetch_array($result) or die(mysql_error());
$this_id=mysql_result($result,0,"id");
$this_body=mysql_result($result,0,"body");
//show the content of the record
echo $this_id;
echo $newline;
echo $this_body;
?>

<!--Show the webpage, where the content(body field of putonServer) in
the record will be shown,
Three button: Discard(B0),Agree(B1) and Update(B0) will be shown as
well -->
<HTML>. .....<HTML>

<?php
//Make a deleted mark for the record which should be deleted from the
database
function DelMySQL($this_id,$connect)
{
$next_id=$this_id+1;
$query_B0="update putonServer SET id2=7 where id='$this_id'";
$query_B0_2="update putonServer SET id2=1 where
id='$next_id'";
$result_B0=mysql_query($query_B0,$connect);
if (!$result_B0) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B0_2=mysql_query($query_B0_2,$connect);
if (!$result_B0_2) {
die ('Puke, Wrong ' . mysql_error());
}
}
//Make an agree mark for the record which should be kept no change in
the database

function AgrMySQL($this_id,$connect)
{
$next_id=$this_id+1;
$query_B0="update putonServer SET id2=7 where id='$this_id'";
$query_B0_2="update putonServer SET id2=1 where
id='$next_id'";
$result_B0=mysql_query($query_B0,$connect);
if (!$result_B0) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B0_2=mysql_query($query_B0_2,$connect);
if (!$result_B0_2) {
die ('Puke, Wrong ' . mysql_error());
}
echo "Processoooo".$newline;
echo $this_id;
}

//modify the selected row in a database
function UpdMySQL($this_id,$connect,$cate,$orie)
{
$next_id=$this_id+1;
$query_B2 ="UPDATE putonServer SET
category='$cate',orientation='$orie' where id='$this_id'";
echo $this_id.$newline;
$result_B2=mysql_query($query_B2,$connect);
if (!$result_B2) {
die ('Puke, Wrong2 ' . mysql_error());
}
$query_B2_1="update putonServer SET id2=4 where
id='$this_id'";
echo $this_id.$newline;
$query_B2_2="update putonServer SET id2=1 where
id='$next_id'";
echo $this_id.$newline;
$result_B2_2=mysql_query($query_B2_2,$connect);
if (!$result_B2_2) {
die ('Puke, Wrong ' . mysql_error());
}
$result_B2_1=mysql_query($query_B2_1,$connect);
if (!$result_B2_1) {
die ('Puke, Wrong2 ' . mysql_error());
}
}

// When different button is clicked, do different action on the
database

if($_POST[B0]){
DelMySQL($this_id,$connect);
}
if($_POST[B1]){
AgrMySQL($this_id,$connect);
}
if($_POST[B2]){
UpdMySQL($this_id,$connect,$_POST[R2],$_POST[sliderinput22]);
}
?>

<Description for the database>
+-------------+------------------+------+-----+---------
+----------------+
| Field | Type | Null | Key | Default |
Extra |
+-------------+------------------+------+-----+---------
+----------------+
| id | int(11) | | PRI | NULL |
auto_increment |
| id2 | int(11) | | | 0
| |
| users | int(10) unsigned | | | 0
| |
| date | text | YES | | NULL
| |
| time | text | YES | | NULL
| |
| docuname | text | YES | | NULL
| |
| body | text | YES | | NULL
| |
| orientation | int(11) | | | 0
| |
| category | int(11) | | | 0
| |
+-------------+------------------+------+-----+---------
+----------------+

On Apr 2, 9:40 pm, Schraalhans Keukenmeester <bitbuc...@invalid.spam>
wrote:
Siong....@gmail.com wrote:
Dear all,
my PHP aims to update a MySQL database by selecting record
one by one and modify then save.
Here are my PHP, but I found that it doesnt work as it supposed to be,
for example, when Record (i) is shown and modified, the change will
come to Record (i+1).
Can anyone provide suggestion?
thanks.
<?php
$connect = mysql_connect("andy","kiong","Password")or
die("Could not connect : " . mysql_error()); ;
mysql_select_db("kiong_userinfo",$connect)or die("Could not
connect : " . mysql_error());
$result = mysql_query("SELECT * FROM putonServer where id2=1",
$connect);
printf("%s<br>\n", mysql_result($result,0,"body"));
?>
<table width="556" height="70" border="0"
align="center">
<tr>
<td width="59" height="66">&nbsp;</td>
</tr>
</table>
<p align="center">
<?php
$this_id=mysql_result($result,0,"id");
$next_id=$this_id+1;
$cat=mysql_result($result,0,"category");
$newline = "<br />";
if($cat==1)
{
echo "Process".$newline;
echo $newline;
}elseif($cat==2)
{
echo "Product".$newline;
echo $newline;
}else{
echo "People".$newline;
echo $newline;
}
$ori=mysql_result($result,0,"orientation");
echo "$ori";
?>
<?php
$connect = mysql_connect("Server","kiong","Password")or
die("Could not connect : " . mysql_error());
$db_selected = mysql_select_db("kiong_userinfo",
$connect);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
if($_POST[B2]){
$query_B2 ="UPDATE putonServer SET
category='$_POST[R2]',orientation='$_POST[sliderinput22]' where
id='$this_id'";
echo $this_id.$newline;
$result_B2=mysql_query($query_B2,$connect);
if (!$result_B2) {
die ('Puke, Wrong2 ' . mysql_error());
}
$query_B2_1="update putonServer SET id2=4 where
id='$this_id'";
echo $this_id.$newline;
$query_B2_2="update putonServer SET id2=1 where
id='$next_id'";
echo $this_id.$newline;
$result_B2_2=mysql_query($query_B2_2,$connect);
if (!$result_B2_2) {
die ('Puke, Wrong ' . mysql_error());
}
$result_B2_1=mysql_query($query_B2_1,$connect);
if (!$result_B2_1) {
die ('Puke, Wrong2 ' . mysql_error());
}
}elseif($_POST[B0]){
$query_B0="update putonServer SET id2=7 where id='$this_id'";
$query_B0_2="update putonServer SET id2=1 where
id='$next_id'";
$result_B0=mysql_query($query_B0,$connect);
if (!$result_B0) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B0_2=mysql_query($query_B0_2,$connect);
if (!$result_B0_2) {
die ('Puke, Wrong ' . mysql_error());
}
}elseif($_POST[B1]){
$query_B1="update putonServer SET id2=0 where id='$this_id'";
$query_B1_2="update putonServer SET id2=1 where
id='$next_id'";
$result_B1=mysql_query($query_B1,$connect);
if (!$result_B1) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B1_2=mysql_query($query_B1_2,$connect);
if (!$result_B1_2) {
die ('Puke, Wrong ' . mysql_error());
}
}
?>

You make a db conection twice, do I see correctly these are two separate
scripts you posted here? If so, where does the second get the values for
$this_id etc? They are set in the first script, I can't see if/how they
are parsed to the second one.

Besides, it's quite hard to tell what is going on in the scripts. No
documentation whatsoever and some cryptic variables/values here and
there. Tough to decipher.

Apr 9 '07 #3
thank, Schraalhans.

I modified the code for your reading.
the problem is still there.

let's begin from the very beginning, current behavior is that if
initial value from id=99, the UpdMySQL of id=99 will be perform OK
with row 99, but the $cat/$ori in row 100 will also be changed to the
same as row 99.then content("body") of row 99 will be shown
again(thepage keep no change ), although in fact, row 100's id2=1;
then when DelMySQL/UpdMySQL/AgrMySQL is performed, the update fields
go to the next row of the row which content("body") is shown on the
website.

<?php

//Connect to database

$connect = mysql_connect("andy","kiong","Password")or
die("Could not connect : " . mysql_error()); ;
mysql_select_db("kiong_userinfo",$connect)or die("Could not
connect : " . mysql_error());

// select a record

$result = mysql_query("SELECT * FROM putonServer where id2=1",
$connect);
$newline = "<br />";
$row = mysql_fetch_array($result) or die(mysql_error());
$this_id=mysql_result($result,0,"id");
$this_body=mysql_result($result,0,"body");

//show the content of the record

echo $this_id;
echo $newline;
echo $this_body;
?>

<HTML>.
<!--Show the webpage, where the content(body field of putonServer) in
the record will be shown,
Three button: Discard(B0),Agree(B1) and Update(B0) will be shown as
well -->
......<HTML>

<?php

//Make a deleted mark for the record which should be deleted from the
database

function DelMySQL($this_id,$connect)
{
$next_id=$this_id+1;
$query_B0="update putonServer SET id2=7 where id='$this_id'";
$query_B0_2="update putonServer SET id2=1 where
id='$next_id'";
$result_B0=mysql_query($query_B0,$connect);
if (!$result_B0) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B0_2=mysql_query($query_B0_2,$connect);
if (!$result_B0_2) {
die ('Puke, Wrong ' . mysql_error());
}
}

//Make an agree mark for the record which should be kept no change in
the database

function AgrMySQL($this_id,$connect)
{
$next_id=$this_id+1;
$query_B0="update putonServer SET id2=7 where id='$this_id'";
$query_B0_2="update putonServer SET id2=1 where
id='$next_id'";
$result_B0=mysql_query($query_B0,$connect);
if (!$result_B0) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B0_2=mysql_query($query_B0_2,$connect);
if (!$result_B0_2) {
die ('Puke, Wrong ' . mysql_error());
}
echo "Processoooo".$newline;
echo $this_id;
}

//modify the selected row in a database
function UpdMySQL($this_id,$connect,$cate,$orie)
{
$next_id=$this_id+1;
$query_B2 ="UPDATE putonServer SET
category='$cate',orientation='$orie' where id='$this_id'";
echo $this_id.$newline;
$result_B2=mysql_query($query_B2,$connect);
if (!$result_B2) {
die ('Puke, Wrong2 ' . mysql_error());
}
$query_B2_1="update putonServer SET id2=4 where
id='$this_id'";
echo $this_id.$newline;
$query_B2_2="update putonServer SET id2=1 where
id='$next_id'";
echo $this_id.$newline;
$result_B2_2=mysql_query($query_B2_2,$connect);
if (!$result_B2_2) {
die ('Puke, Wrong ' . mysql_error());
}
$result_B2_1=mysql_query($query_B2_1,$connect);
if (!$result_B2_1) {
die ('Puke, Wrong2 ' . mysql_error());
}
}

// When different button is clicked, do different action on the
database

if($_POST[B0]){
DelMySQL($this_id,$connect);
}
if($_POST[B1]){
AgrMySQL($this_id,$connect);
}
if($_POST[B2]){
UpdMySQL($this_id,$connect,$_POST[R2],$_POST[sliderinput22]);
}
?>

<Description for the database>
+-------------+------------------+------+-----+---------
+----------------+
| Field | Type | Null | Key | Default |
Extra |
+-------------+------------------+------+-----+---------
+----------------+
| id | int(11) | | PRI | NULL |
auto_increment |
| id2 | int(11) | | | 0
| |
| users | int(10) unsigned | | | 0
| |
| date | text | YES | | NULL
| |
| time | text | YES | | NULL
| |
| docuname | text | YES | | NULL
| |
| body | text | YES | | NULL
| |
| orientation | int(11) | | | 0
| |
| category | int(11) | | | 0
| |
+-------------+------------------+------+-----+---------
+----------------+
On Apr 2, 9:40 pm, Schraalhans Keukenmeester <bitbuc...@invalid.spam>
wrote:
Siong....@gmail.com wrote:
Dear all,
my PHP aims to update a MySQL database by selecting record
one by one and modify then save.
Here are my PHP, but I found that it doesnt work as it supposed to be,
for example, when Record (i) is shown and modified, the change will
come to Record (i+1).
Can anyone provide suggestion?
thanks.
<?php
$connect = mysql_connect("andy","kiong","Password")or
die("Could not connect : " . mysql_error()); ;
mysql_select_db("kiong_userinfo",$connect)or die("Could not
connect : " . mysql_error());
$result = mysql_query("SELECT * FROM putonServer where id2=1",
$connect);
printf("%s<br>\n", mysql_result($result,0,"body"));
?>
<table width="556" height="70" border="0"
align="center">
<tr>
<td width="59" height="66">&nbsp;</td>
</tr>
</table>
<p align="center">
<?php
$this_id=mysql_result($result,0,"id");
$next_id=$this_id+1;
$cat=mysql_result($result,0,"category");
$newline = "<br />";
if($cat==1)
{
echo "Process".$newline;
echo $newline;
}elseif($cat==2)
{
echo "Product".$newline;
echo $newline;
}else{
echo "People".$newline;
echo $newline;
}
$ori=mysql_result($result,0,"orientation");
echo "$ori";
?>
<?php
$connect = mysql_connect("Server","kiong","Password")or
die("Could not connect : " . mysql_error());
$db_selected = mysql_select_db("kiong_userinfo",
$connect);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
if($_POST[B2]){
$query_B2 ="UPDATE putonServer SET
category='$_POST[R2]',orientation='$_POST[sliderinput22]' where
id='$this_id'";
echo $this_id.$newline;
$result_B2=mysql_query($query_B2,$connect);
if (!$result_B2) {
die ('Puke, Wrong2 ' . mysql_error());
}
$query_B2_1="update putonServer SET id2=4 where
id='$this_id'";
echo $this_id.$newline;
$query_B2_2="update putonServer SET id2=1 where
id='$next_id'";
echo $this_id.$newline;
$result_B2_2=mysql_query($query_B2_2,$connect);
if (!$result_B2_2) {
die ('Puke, Wrong ' . mysql_error());
}
$result_B2_1=mysql_query($query_B2_1,$connect);
if (!$result_B2_1) {
die ('Puke, Wrong2 ' . mysql_error());
}
}elseif($_POST[B0]){
$query_B0="update putonServer SET id2=7 where id='$this_id'";
$query_B0_2="update putonServer SET id2=1 where
id='$next_id'";
$result_B0=mysql_query($query_B0,$connect);
if (!$result_B0) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B0_2=mysql_query($query_B0_2,$connect);
if (!$result_B0_2) {
die ('Puke, Wrong ' . mysql_error());
}
}elseif($_POST[B1]){
$query_B1="update putonServer SET id2=0 where id='$this_id'";
$query_B1_2="update putonServer SET id2=1 where
id='$next_id'";
$result_B1=mysql_query($query_B1,$connect);
if (!$result_B1) {
die ('Puke, Wrong2 ' . mysql_error());
}
$result_B1_2=mysql_query($query_B1_2,$connect);
if (!$result_B1_2) {
die ('Puke, Wrong ' . mysql_error());
}
}
?>

You make a db conection twice, do I see correctly these are two separate
scripts you posted here? If so, where does the second get the values for
$this_id etc? They are set in the first script, I can't see if/how they
are parsed to the second one.

Besides, it's quite hard to tell what is going on in the scripts. No
documentation whatsoever and some cryptic variables/values here and
there. Tough to decipher.

Apr 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jim Cobban | last post: by
121 posts views Thread by typingcat | last post: by
6 posts views Thread by karlarneg | last post: by
34 posts views Thread by Jorge | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.