php - Storing multiple input values in single field in database and retrieving separately -
so, have form so:
<form action="edit.php" method="post" id="content"> <h3>homepage</h3> <hr/> <h4>title: </h4><input type="text" name="userdata[]" value='$userdata[]'><br/> <h4>subtitle: </h4><input type="text" name="userdata[]" value='$userdata[1]'><br/> <h4>footer: </h4><input type="text" name="userdata[]" value='$userdata[2]'><br/> <input type="submit" value="save" name="datasave" id="save"> </form>
php submit:
if(isset($_post['datasave'])) { $data = $_post['userdata']; $userdata = mysqli_escape_string($con, $data); $query = "update users set userdata = '$userdata' username = '$username'"; mysqli_query($con, $query); }
getting values db display in fields:
$getuserdata = "select userdata users username = '$username'"; $updateduserdata= mysqli_query($con,$getuserdata); if (! $updateduserdata){ echo "error"; } while($row = mysqli_fetch_assoc($updateduserdata)){ $userdata = $row['userdata']; }
i should note novice php & mysql user (please bear me). problem is, doesn't work , i'm missing & not doing efficiently be... how can values of multiple inputs , store them in same db field? can see, i'd each of values automatically added respective field on page load.
edit: have taken down barebones (removing other code etc.) , restructured bit, has done trick! learn parameterized queries :d
<?php require('config.php'); session_start(); $username = $_session['username']; $getuserdata = "select userdata users username = '$username'"; $updateduserdata= mysqli_query($con,$getuserdata); $row = mysqli_fetch_assoc($updateduserdata); $data = json_decode($row["userdata"]); foreach($data $eachdata ) { $userdata[] = $eachdata; } if ( $userdata ) { echo '<form action="edit.php" method="post" id="content"> <h3>homepage</h3> <hr/> <h4>title: </h4><input type="text" name="userdata[]" value="'.$userdata[0].'"><br/> <h4>subtitle: </h4><input type="text" name="userdata[]" value="'.$userdata[1].'"><br/> <h4>footer: </h4><input type="text" name="userdata[]" value="'.$userdata[2].'"><br/> <input type="submit" value="save" name="datasave" id="save"> </form>'; } else { die("error: {$con->errno} : {$con->error}"); } if(isset($_post['datasave'])) { $data = json_encode($_post['userdata']); $query = "update users set userdata = '$data' username = '$username'"; mysqli_query($con, $query); if (mysqli_query($con, $query)) { header("location: edit.php"); } else { echo "error updating record: " . mysqli_error($con); } } if ( $con->connect_error ) { die( 'connect error: ' . $con->connect_errno . ': ' . $con->connect_error ); } $con->close(); ?>
questions first:
where
$username
variable coming from?do intend store array
users.userdata
column?
if want continue schema of yours, have figure out how store them database. encounter error if try use *_real_escape_string()
if use in array. second parameter looking strings, not array.
you can try run them , use *_real_escape_string
, restore them.
for($x = 0; $x < count($_post["userdata"]); $x++){ $data[$x] = mysqli_real_escape_string($con, $data[$x]); }
did concatenate variable html form?
echo '<form action="edit.php" method="post" id="content"> <h3>homepage</h3> <hr/> <h4>title: </h4><input type="text" name="userdata[]" value="'.$userdata[0].'"><br/> <h4>subtitle: </h4><input type="text" name="userdata[]" value="'.$userdata[1].'"><br/> <h4>footer: </h4><input type="text" name="userdata[]" value="'.$userdata[2].'"><br/> <input type="submit" value="save" name="datasave" id="save"> </form>';
@chrisbaker covers rest of storing , retrieving data database.
standard way of storing data
but advisable way store such data in database restructure table in database. separate each own column:
your users
table like:
id | username | title | subtitle | footer | ---+-------------+-----------+-----------+---------+ 1 | lankymoose | op | english | sticky | 2 | chris baker | boy scout | english | dynamic | 3 | logan wayne | whiner | multi | none |
so can store them database , fetch them easier.
dynamic storing of data
but if fields dynamic? want store more data user (that thought of when try insert array of data user).
you can create tables stores different types of data user , stores user's input. lets name first table example, data_table
:
data_id | data_type ---------+----------- 1 | title 2 | subtitle 3 | footer
then second table stores users input, lets name data_input
:
input_id | data_id | user_id | user_input ---------+---------+---------+------------ 1 | 1 | 1 | op 2 | 2 | 1 | english 3 | 3 | 1 | sticky 4 | 1 | 2 | boy scout 5 | 2 | 2 | english 6 | 3 | 2 | dynamic 7 | 1 | 3 | whiner 8 | 2 | 3 | multi 9 | 3 | 3 | none
your users
table now:
user_id | username --------+------------- 1 | lankymoose 2 | chris baker 3 | logan wayne
so example, want data lankymoose
, can try query:
select a.username, c.data_type, b.user_input users left join data_input b on a.user_id = b.user_id left join data_table c on b.data_id = c.data_id user_id = 1
result be:
username | data_type | user_input ------------+-----------+------------ lankymoose | title | op lankymoose | subtitle | english lankymoose | footer | sticky
with method, can add more fields inserting data data_table
.
i suggest use prepared statement
since using mysqli_*
extension.
Comments
Post a Comment