Jump to content

Submitting form deletes all data


aebstract

Recommended Posts

<?php


if (isset ($_POST['submit'])) {
$problem = FALSE;


include "../connect/todb.php";


if (!$problem) {

$plant2 = $_POST['dropdown2'];
$email2 = $_POST['email2'];



MYSQL_QUERY("UPDATE users SET plant='$plant2',email='$email2' WHERE user_id=$var3") or die (mysql_error());


                      header("Location: /accounthome/tools/changeusers/$var3/$var4/");

} else {
$content .= "$error";
}
}







if (isset ($_POST['submit2'])) {
$problem = FALSE;


include "../connect/todb.php";


if (!$problem) {

$plantloc2 = $_POST['plantloc2'];
$city2 = $_POST['city2'];
$state2 = $_POST['state2'];
$zip2 = $_POST['zip2'];
$street2 = $_POST['street2'];
$PT2 = $_POST['PT2'];
$prs2 = $_POST['prs2'];



MYSQL_QUERY("UPDATE plants SET plantloc='$plantloc2',city='$city2',state='$state2',zip='$zip2',street='$street2',PT='$PT2',prs='$prs2' WHERE id=$var4") or die (mysql_error());


                      header("Location: /accounthome/tools/changeusers/$var3/$var4/");

} else {
$content .= "$error";
}
}






















$result = mysql_query("SELECT * FROM users WHERE user_id = '$var3'") or DIE(mysql_error());
while ($r=mysql_fetch_array($result))
{
$plant2=$r["plant"];
$email2=$r["email"];
}


$result2 = mysql_query("SELECT * FROM plants WHERE id = '$var4'") or DIE(mysql_error());
while ($r2=mysql_fetch_array($result2))
{
$plantloc2=$r2["plantloc"];
$city2=$r2["city"];
$state2=$r2["state"];
$zip2=$r2["zip"];
$street2=$r2["street"];
$PT2=$r2["PT"];
$prs2=$r2["prs"];
}


$displayname = substr($email2,0,strpos($email2,"@"));


$content .= "
<table><tr>
<td><h2>$displayname</h2></td>
<td width=75></td>
<td><h2>$plantloc2</h2></td>
</tr>
<tr><td valign=\"top\">";

$content .= "<form action=\"/accounthome/tools/changeusers/$var3/$var4/\" method=\"post\">";
$content .= '
Email: <br /><input type="text" maxlength="100" class="textfield" name="email" size="40" value="' . $email2 . '" /><br /><br />
Plant: <br />
<select name="dropdown">';
$results = mysql_query("SELECT * FROM `plants` ORDER BY `plantloc` ASC") or DIE(mysql_error());
while($rs = mysql_fetch_array($results))
{
  $ids = $rs['id'];
  $plantlocs = $rs['plantloc'];
  if ($ids == $var4) {
  $content .= "<option value=\"{$ids}\" selected>{$plantlocs}</option>\n";
  } else {
  $content .= "<option value=\"{$ids}\">{$plantlocs}</option>\n";
  }
}
$content .= '</select>';


$content .= '<br /><br /><input type="submit" name="submit" value="Change User Information" class="textfield" /></form>';

$content .= "</td><td width=75> </td><td valign=\"top\">";





$content .= "<form action=\"/accounthome/tools/changeusers/$var3/$var4/\" method=\"post\">";

$content .= '
Plantloc: <br /><input type="text" maxlength="100" class="textfield" name="email" size="40" value="' . $plantloc2 . '" /><br /><br />
City: <br /><input type="text" maxlength="20" class="textfield" name="email" size="20" value="' . $city2 . '" /><br /><br />
State: <br /><input type="text" maxlength="2" class="textfield" name="email" size="2" value="' . $state2 . '" /><br /><br />
Zip: <br /><input type="text" maxlength="5" class="textfield" name="email" size="5" value="' . $zip2 . '" /><br /><br />
Street: <br /><input type="text" maxlength="20" class="textfield" name="email" size="20" value="' . $street2 . '" /><br /><br />
Plant Type: <br /><input type="text" maxlength="2" class="textfield" name="email" size="2" value="' . $PT2 . '" /><br /><br />
Paw Recovery System: <br />
0 - None<br />
1 - Stork<br />
2 - Meyn<br />
<input type="text" maxlength="1" class="textfield" name="email" size="1" value="' . $prs2 . '" /><br /><br />


';


$content .= '<br /><br /><input type="submit" name="submit2" value="Change Plant Information" class="textfield" /></form>';


$content .= '

<br /><br /><br />
<a href="#" id="a1">Plant Type</a></div>
    <div id="note1">
    <table>
    <tr>
    <td align=right>1</td><td width=5></td><td>Tyson Foods</td>
</tr>
<tr>
<td align=right>2</td><td width=5></td><td>Pilgrim\'s Pride</td>
</tr>
<tr>
<td align=right>3</td><td width=5></td><td>Allen Family Foods</td>
</tr>
<tr>
<td align=right>4</td><td width=5></td><td>Case Farms</td>
</tr>
<tr>
<td align=right>5</td><td width=5></td><td>Columbia Farms</td>
</tr>
<tr>
<td align=right>6</td><td width=5></td><td>Foster Farms</td>
</tr>
<tr>
<td align=right>7</td><td width=5></td><td>George\'s</td>
</tr>
<tr>
<td align=right>8</td><td width=5></td><td>Keystone Foods</td>
</tr>
<tr>
<td align=right>9</td><td width=5></td><td>Koch Foods</td>
</tr>
<tr>
<td align=right>10</td><td width=5></td><td>Perdue Farms</td>
</tr>
<tr>
<td align=right>11</td><td width=5></td><td>Wayne Farms</td>
</tr>
<tr>
<td align=right>12</td><td width=5></td><td>Mountaire Farms</td>
</tr>
<tr>
<td align=right>13</td><td width=5></td><td>Peterson Farms</td>
</tr>
<tr>
<td align=right>14</td><td width=5></td><td>Ecolab</td>
</tr>
<tr>
<td align=right>15</td><td width=5></td><td>Gold\'n Plump</td>
</tr>
<tr>
<td align=right>16</td><td width=5></td><td>Maple Lodge Farms</td>
</tr>
</table>
    </div>
    ';


$content .= "</td></tr></table>";

?>

 

 

There are two forms in this code, one using submit and the other submit2. Now, regardless of which form I submit.. instead of it updating the database with the information in the box, it is updating everything with blank information.. losing everything! Can anyone see what is causing this?

Link to comment
https://forums.phpfreaks.com/topic/100668-submitting-form-deletes-all-data/
Share on other sites

I think the SQL statement is correct, so check the user entry.

 

First, check whether your post arrives:

 

if (!empty($_POST)) {
  ... // now execute the query
}

 

Secondly, evaluate all posted values (use foreach(..)) whether they contain something or not (e. g. check for $email2!='') and execute the query only when all fields are not empty.

 

Maybe also your form fields are not declared correctly, or the form uses GET instead of POST.

 

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.