I have a php page which extracts data from a dbms which contains email addy. This works. It then displays the email addy and other stored data data. The user then has the option of putting an “X” in a field designed in the php page called emailselected. This also works. I now want to update the dbms with the new field based on the stored email addy but the update statement doesn’t work. Please help. This portion of the php file is here.
include("db.php");
if (isset($_POST['ssubmit']))
{
$id_save = $test['id'];
$emailselected_save = $_POST['emailselected'];
$email_save = $test['email'];
mysql_query("UPDATE emails SET selected='$emailselected_save' WHERE id = 'id'");
echo "<input type='button' value='Email(s) sent' onclick='goBack()' />";
mysql_close($conn);
} else {echo "hello";}
?>
</head>
<form method='post'>
<div id='headd'>
<br />
<input type='button' value='Close this window without Sending' onclick='goBack()' />
<input type='submit' name='ssubmit' id='ssubmit' value='Send Email Now!!!' />
<p>Place an "X" in the emails you wish to send!!!</p>
</div>
<body>
<br /><br/>
<?php
include("db.php");
$result = mysql_query("SELECT * FROM emails WHERE unsubscribe != 'x' ORDER BY lastname ASC");
while($test = mysql_fetch_array($result))
{
?>
<table border='1' width='78%'>
<tr align=\"left\">
<td width='4%'><font color='black'><input type='text' size='1' id='emailselected' name='emailselected' /></font></td>
<td width='15%'><font color='black'><?php echo $test['lastname']?></font></td>
<td width='15%'><font color='black'><?php echo $test['firstname']?></font></td>
<td width='40%'><font color='black'><?php echo $test['email']?></font></td>
<td width='4%'><font color='black'><?php echo $test['id']?></font></td>
</tr>
</table>
<?php
}
?>
</form>
At the top of the form is an include statement. This works because it extracts the email addy and displays it in the php file.
What am I doing wrong?
You’re pretty much limited to the following possibilities if the data didn’t update:
To find out which, you should check the return code from
mysql_queryto see if it failed or not:If it didn’t fail, you should then check how many rows it affected with
mysql_affected_rows():Checking return values from functions that return them is good programming practice.
It might also be a good idea to output or log all those variables that you’re using in the SQL statement to ensure they’re what you think they are.
And, though it should go without saying by now, use of the
mysql_*functions are discouraged, you should be usingmysqli_*(orPDO) instead.And I’ve only just noticed that your hierarchy of HTML elements doesn’t seem quite right. You have effectively:
which doesn’t gel for me. I’m not sure that
formis valid outside of the body. According to the w3c specs, the html element must contain: