html - How to pass a value with $_POST[...] which contains dots and spaces to PHP page? -
i'm writing php program reads data mysql database , show records on html page. it's dynamic. added different functions, among edit record. when click on edit button, program loads page name of columns , value. click on submit button , load php page values $_post[...]. it's pity if column name contains dots or spaces, value of variable has been valorized $_post blank. if try edit record using phpmyadmin works perfectly. how phpmyadmin able this? how can solve problem? thanks.
here code of 2 pages. first, "edit.php" collects data needed modify record. second, "changerow.php", updates database.
edit.php:
<?php $rowid = $_get['id']; $host = "localhost"; $user = "inventory"; $pass = "1234"; $db_name = "inventory"; //create connection $connection = mysqli_connect($host, $user, $pass, $db_name); mysqli_set_charset( $connection, 'utf8mb4'); //test if connection failed if(mysqli_connect_errno()){ die("connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ")"); } //get results database $result = mysqli_query($connection,"select * `pc` id=$rowid"); $all_property = array(); //declare array saving property echo '<html>'; echo '<head>'; echo '<style type="text/css">'; // set properties of tables have same cell spaces , left echo 'table { width:250px;table-layout:fixed; float:left; }'; echo 'table tr { height:2em; }'; echo 'td { overflow:hidden;white-space:nowrap; }'; echo 'input[type="text"] {'; echo 'float: center'; echo '}'; echo '</style>'; echo '</head>'; echo '<body>'; echo '<form action="changerow.php" method="post">'; echo '<input type="hidden" name="id" value=' . $rowid . '>'; echo '<table border="1">'; echo "<tr>"; //echo "<tr>"; while ($property = mysqli_fetch_field($result)) { echo '<td>' . $property->name . '</td></tr>'; //get field name header array_push($all_property, $property->name); //save array } echo '</tr>'; //end tr tag echo '</table>'; echo '<table border="1">'; // save column names use in input type name field $names = array(); $sql2 = "show columns pc"; $columns = mysqli_query($connection,$sql2); while($row = mysqli_fetch_array($columns)){ array_push($names, $row['field']); } $totcolumns = count($names); $i=0; //showing data while ($row = mysqli_fetch_array($result)) { echo "<tr>"; foreach ($all_property $item) { echo "<td><input type=\"text\" value=\"$row[$item]\""; echo " name=\"$names[$i]\"></td></tr>"; $i++; } echo '</tr>'; } echo "</table>"; echo "<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"; echo '<input type="submit" value="edit">'; echo '    '; echo "<input type=\"button\" value=\"cancel\" onclick=\"window.location.href='inventory.php'\">"; echo '</form>'; // closing connection $connection->close(); echo '</body>'; echo '</html>'; ?>
changerow.php:
<?php $db_host = 'localhost'; $db_user = 'inventory'; $db_pass = '1234'; $db_name = 'inventory'; // create connection $connection = new mysqli($db_host, $db_user, $db_pass, $db_name); mysqli_set_charset( $connection, 'utf8mb4'); // test connection if ($connection->connect_errno) { echo "connection failed: ". $connection->connect_error . "."; exit(); } // save column names use in input type name field $names = array(); $sql2 = "show columns pc"; $columns = mysqli_query($connection,$sql2); while($row = mysqli_fetch_array($columns)){ array_push($names, $row['field']); } $totcolumns = count($names); $i=0; $values = array(); ($i = 0; $i < $totcolumns; $i++) { echo "$i: $names[$i]<br/>"; $addtovalues = "'"; $namex = $names[$i]; array_push($values, $_post[$namex]); $values[$i] = $addtovalues .= $values[$i] .= $addtovalues; // add quotes value sql echo $values[$i]; } // have create 1 string compose update command $i=0; $j=1; // value of + 1, because id not updated $k=0; $mystring='update `pc` set '; ($i = 0, $j=1; $i < $totcolumns, $j <$totcolumns; $i++, $j++) { $mystring=($mystring.'`'.$names[$j].'`'.'='.$values[$j].','.' '); } // remove final comma before adding $mystring=rtrim($mystring,", "); $mystring=$mystring.' id='.$values[0]; // update database record $sql = $mystring; // query run if (!$connection->query($sql)) { echo "query error: " . $connection->error . "."; } // closing connection $connection->close(); ?>
wiki
Comments
Post a Comment