Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Dynamic SQL Query from Dynamic POSTs
    primarykey
    data
    text
    <p>First time question, long time reader :)</p> <p>I am building forms dynamically from Columns in a MYSQL DB. These columns are created/ deleted etc.. elsewhere on my App. My form runs a query against a SQL View and pulls in the column names and count of columns. Simple! build the form, with the HTML inputs built with a PHP for loop, and it echos out the relevant HTML for the new form fields. All very easy so far.</p> <p>Now i want a user to update these dynamically added fields and have the data added to the relevant columns - same table as existing columns. So, as the input fields are named the same as the columns, they are posted to a PHP script for processing.</p> <p>Problem is, while i have the actual field names inserted in to the SQL INSERT query, i cannot figure out how to extract the POST data from the POST dynamically and add this to the VALUEs section of the query.</p> <p>Here is my attempt.... The Query works without the variables added to it. </p> <p>It works like this, first section/ is to retrieve the columns names from earlier created VIEW - as these are identical to POST names from the form. Then output to array and variable for insertion to Query. It looks like the implode function works, in that the relevant column names are correct in the statement, but i fear that my attempt to inject the column names on to the POST variables is not working.</p> <pre><code> $custq = "SELECT * FROM customProperties"; $result = $database-&gt;query($custq); $num_rows = mysql_numrows($result); while (list($temp) = mysql_fetch_row($result)) { $columns[] = $temp; } $query = ''; foreach($columns as $key=&gt;$value) { if(!empty($columns[$key])) { $values .= "'".'$_POST'."['".$value."'], "; } } $q = "INSERT INTO nodes (deviceName, deviceInfo, ".implode(", ", $columns).", nodeDateAdded, status ) VALUES ('" . $_POST['deviceName'] . "', '" . $_POST['deviceInfo'] . "', ".$values." CURDATE(), '1' )"; $result = $database-&gt;query($q) </code></pre> <p>Any help is much appreciated. I will feed back as much as i can. Please note, relativity new to PHP, so if i am all wrong on this, i will be glad for any tips/ advice</p> <p>Regards</p> <p>Stephen</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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