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
https://forums.phpfreaks.com/topic/210910-php-mysql-update-failure/
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 . . .
}

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 . . .
}
?>

It tells you how many rows were affected by the UPDATE query. An update query can return success, even if it doesn't actually change anything, so if you want to know if it actually updated any records, that would be the way to do it.

<?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%.

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>';

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.

Ah, now we're getting somewhere. The query is failing altogether it seems, so let's do this and see what we get.

$result = mysql_query($sql) or die('Query string: ' . $sql . '<br />Error: ' . mysql_error() . '<br />');

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.

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?

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.

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.

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.

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.