Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use forms to update SQL
    text
    copied!<p>I'm trying to make a form that uses a drop down, radio buttons, text field, textarea, and a hidden value(the time) then takes that information from that form and updates SQL database.</p> <p>My form is below and it all loads correctly but I'm having issues updating the values and trying to figure out how to make the radio buttons and dropdowns to work since I can't make the value php code and need to pass the value. Everything I'm finding on the web is how to do text fields where the user types something.</p> <p>When I select update it just submits the data but nothing changes. On my update.php I have a sanitize function at the very end and am unsure how to pass the variables in. Do I create an array named $var and input all my variables into it or pass each variable at a time?</p> <p>I've been searching the web for HOW TO's and am currently reading two books but they don't go into enough detail so thanks for any assistance.</p> <p>control.php</p> <pre><code> &lt;?php session_start(); if( !isset($_SESSION['myusername']) ){ header("Location: login.php"); } ?&gt; &lt;?php require("../../system/templates/includes/constants.php"); $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if(!$connection) { die("Database connection failed: " .mysql_error()); } $db_select = mysql_select_db(DB_NAME,$connection); if(!$db_select) { die("Database selection failed: " . mysql_error()); } ?&gt; &lt;form method="post" action="update.php"&gt; &lt;select name="name" required="true" value="&lt;?php echo $row['name']; ?&gt;"&gt; &lt;?php $query="SELECT id, name FROM modules"; $result=mysql_query($query); while ($row=mysql_fetch_array($result)) { echo "&lt;option value=\"" . $row['id'] . "\"&gt;" . $row['name'] . "&lt;/option&gt;"; } ?&gt; &lt;/select&gt; &lt;br /&gt; Select Status: Red &lt;input type="radio" value="red" name="status" /&gt; Yellow &lt;input type="radio" value="yellow" name="status" /&gt; Green &lt;input type="radio" checked="checked" value="green" name="status" /&gt; &lt;br /&gt; Reason: &lt;br /&gt; &lt;select name="reason" required="true"&gt; &lt;option value="0" selected="selected" value=""&gt;Select Reason&lt;/option&gt; &lt;option value="ONLINE"&gt;Online&lt;/option&gt; &lt;option value="MAINTENANCE"&gt;Maintenance&lt;/option&gt; &lt;option value="ERROR"&gt;Error&lt;/option&gt; &lt;option value="OFFLINE"&gt;Offline&lt;/option&gt; &lt;option value=""&gt;No Reason&lt;/option&gt; &lt;/select&gt; &lt;br /&gt; ETA: &lt;br /&gt; &lt;input type="text" name="eta" value="&lt;?php echo $row['eta']; ?&gt;" maxlength="8" /&gt; &lt;br /&gt; Description: &lt;br /&gt; &lt;textarea rows="5" cols="30" name="explanation" wrap="hard" required="true" maxlength="320" value="&lt;?php echo $row['description']; ?&gt;" /&gt;&lt;/textarea&gt; &lt;br /&gt; &lt;div align="right"&gt; &lt;input name="update" type="submit" value="Update"/&gt; &lt;input type="hidden" name="last_updated" value="&lt;?php $mysqldate = date ('H:i'); $phpdate = strtotime ( $mysqldate );?&gt; /&gt; &lt;/form&gt; </code></pre> <p>update.php</p> <pre><code>&lt;?php print_r(_POST); if(isset($POST['update'])) { $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if(! $connection) { die('Could not connect: ' .mysql_error()); } $name = $POST['name']; $status = $POST['status']; $reason = $POST['reason']; $eta = $POST['eta']; $description = $POST['description']; $last_updated = $POST['last_updated']; $updated_by = $POST['updated_by']; $sql = "UPDATE module SET status = $status , reason = $reason , eta = $eta , description = $description , last_updated = $last_updated , updated_by = $updated_by WHERE name = $name"; mysql_select_db('status'); $retval = mysql_query ( $sql, $connection); if (!retval) { die('Could not update data: ' . mysql_error()); } echo "Updated data successfully"; mysql_close($connection); } else { // not sure what to do here } function sanitizeString($var) { $var = stripslashes($var); $var = htmlentities($var); $var = strip_tags($var); return $var; } function sanitizeMySQL($var) { $var = mysql_real_escape_string($var); $var = satnizeString($var); return $var; } header("Location: control.php"); ?&gt; </code></pre> <p>As always I greatly appreciate any assistance anyone can offer. I'm still in the very early stages of learning this and this website and community has helped me more than any book/tutorial I've read so far.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload