Jump to content

PHP Mysql UPDATE failure?


drdrew

Recommended Posts

I will provide the code. Im working on my script which updates a row of data in a mysql DB, the update command doesnt have all of the fields.(I don't know if this is failing it or not?)

 

The code for the form is.

<?php
include("../scripts/dbconnect.php");
mysql_select_db("rdb")or die("cannot select DB");


$sql="SELECT * FROM wo WHERE won='1'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="updatewo1.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"> </td>
<td align="center"><strong>WON</strong></td>
<td align="center"><strong>CName</strong></td>
<td align="center"><strong>CEmail</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<?php echo $rows['won']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<?php echo $rows['cname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<?php echo $rows['cemail']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<?php echo $rows['won']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>


<?php
mysql_close();
?>

 

The page the action goes to is below.

<?php
include("../scripts/dbconnect.php");
mysql_select_db("rdb")or die("cannot select DB");

$sql="UPDATE wo SET won='$_POST[won]', cname='$_POST[cname]', cemail='$_POST[cemail]'
WHERE won = '$_POST[won]'";
$result = mysql_query($sql);

if($result){
echo "Successful";
}

else {
echo "ERROR";
}
?>

 

I am fairly new to PHP, any help would be greatly appreciated. I will list all of the Mysql table's fields below.

 

won,wopword,status,dws,problem,dfin,cost,service,coupon,lmao1,lmao2,cname,caddy,cemail,cphone,compinfo,techname,comments,storeloc,clerk,solution

Link to comment
Share on other sites

After the query execution . . .

$aff_rows = mysql_affected_rows($result);
if( $aff_rows == 0 ) {
     echo 'No rows were updated.'; // Or do something else, whatever . . .
} else {
     echo $aff_rows . ' were updated'; // Or whatever . . .
}

Link to comment
Share on other sites

I just tried it and get an error.

 

 

Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\CBMS\wo\updatewo1.php on line 9

No rows were updated.

 

<?php
include("../scripts/dbconnect.php");
mysql_select_db("rdb")or die("cannot select DB");

$sql="UPDATE wo SET won='$_POST[won]', cname='$_POST[cname]', cemail='$_POST[cemail]'
WHERE won = '$_POST[won]'";
$result = mysql_query($sql);

$aff_rows = mysql_affected_rows($result);
if( $aff_rows == 0 ) {
     echo 'No rows were updated.'; // Or do something else, whatever . . .
} else {
     echo $aff_rows . ' were updated'; // Or whatever . . .
}
?>

Link to comment
Share on other sites

<?php
include("../scripts/dbconnect.php");
mysql_select_db("rdb")or die("cannot select DB");

$sql="UPDATE wo SET won='$_POST[won]', cname='$_POST[cname]', cemail='$_POST[cemail]'
WHERE won = '$_POST[won]'";
$result = mysql_query($sql);

$aff_rows = mysql_affected_rows($result);
if( $aff_rows == 0 ) {
     echo 'No rows were updated.'; // Or do something else, whatever . . .
} else {
     echo $aff_rows . ' were updated'; // Or whatever . . .
}
?>

 

The likely hood of me screwing up a simple task is more than 97.285%.

Link to comment
Share on other sites

If the form hasn't changed, there is still no $_POST['won'] being passed to this script. To see exactly what the script is getting from the form, put this at the top, then post the result of it here.

echo '<pre>';
print_r($_POST);
echo '</pre>';

Link to comment
Share on other sites

Array

(

    [won] => 1

    [wopword] => 1

    [status] => xgdfg

    [dws] => 2010-08-16

    [dfin] => 0000-00-00

    [problem] => Problem

    [cost] => asd

    [service] => Service

    [coupon] => agd

    [cname] => asdg

    [caddy] => Customers Address

    [cemail] => asd

    [cphone] => ag

    [compinfo] => Computer Information

    [techname] => agfsd

    [comments] => Comments

    [storeloc] => afhg

    [clerk] => agf

    [solution] => Solution

    [lmao1] => 1

    [lmao2] => 1

    [lmao3] => 1

    [lmao4] => 1

)

 

 

Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\CBMS\wo\updatewo1.php on line 13

No rows were updated.

Link to comment
Share on other sites

Array

(

    [won] => 1

    [wopword] => 1

    [status] => asdgag

    [dws] => 2010-08-16

    [dfin] => 0000-00-00

    [problem] => Problem

    [cost] => asd

    [service] => Service

    [coupon] => agd

    [cname] => asdg

    [caddy] => Customers Address

    [cemail] => asd

    [cphone] => ag

    [compinfo] => Computer Information

    [techname] => agfsd

    [comments] => Comments

    [storeloc] => afhg

    [clerk] => agf

    [solution] => Solution

    [lmao1] => 1

    [lmao2] => 1

    [lmao3] => 1

    [lmao4] => 1

)

 

 

Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\CBMS\wo\updatewo1.php on line 13

No rows were updated.

 

I left the print in there.

Link to comment
Share on other sites

I changed it and got:

Array

(

    [won] => 1

    [wopword] => 1

    [status] => sga

    [dws] => 2010-08-16

    [dfin] => 0000-00-00

    [problem] => Problem

    [cost] => asd

    [service] => Service

    [coupon] => agd

    [cname] => asdg

    [caddy] => Customers Address

    [cemail] => asd

    [cphone] => ag

    [compinfo] => Computer Information

    [techname] => agfsd

    [comments] => Comments

    [storeloc] => afhg

    [clerk] => agf

    [solution] => Solution

    [lmao1] => 1

    [lmao2] => 1

    [lmao3] => 1

    [lmao4] => 1

)

 

No rows were updated.

 

I then checked the DB and it didn't change?

Link to comment
Share on other sites

This really should be using a primary key id field to reference the record that needs to be updated, but let's get the update at least working first. Are you changing any of the form's values before submitting it? Try changing only the value of the last field to something different.

Link to comment
Share on other sites

Ty, I tryed to change lmao from 1 to 7

 

Array

(

    [won] => 1

    [wopword] => 1

    [status] => Status

    [dws] => 2010-08-16

    [dfin] => 0000-00-00

    [problem] => Problem

    [cost] => asd

    [service] => Service

    [coupon] => agd

    [cname] => asdg

    [caddy] => Customers Address

    [cemail] => asd

    [cphone] => ag

    [compinfo] => Computer Information

    [techname] => agfsd

    [comments] => Comments

    [storeloc] => afhg

    [clerk] => agf

    [solution] => Solution

    [lmao1] => 1

    [lmao2] => 1

    [lmao3] => 1

    [lmao4] => 7

)

 

No rows were updated.

Link to comment
Share on other sites

Your right im way to off tonight sorry. Just a quick question I have this set up and the <td> adds for new rows but no data is displayed?

 

<?php
include("../scripts/dbconnect.php");

mysql_select_db("rdb")or die("cannot select DB");

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

$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Remove Wo</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">/#/</td>
<td align="center" bgcolor="#FFFFFF"><strong>Won</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>CName</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>DWS</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>CEmail</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['won']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['won']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['cname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['dws']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['cemail']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM wo WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "Yes";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

 

Do you have an idea of what could cause that. It produces a table but the $row information doesn't show up.

Thanks so very much again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.