-6

I have a page called editdetails.php and on that page users can edit their details by selecting options, like religion, education, hobbies etc. I have done the HTML but how I can INSERT/UPDATE it into the database called users where it contains the users details such as username, password, profile_pic, about_me.

I want the details to be updated into the users table and also I want to echo it on their profile after they select the options. Here is the code:

<?php include("inc/header.inc.php");
$updateinfo =@$_POST['updateinfo'];
    //This is what I want to echo on their profile
    $get_info = mysql_query("SELECT relationship, height, body_type, hair_color, eye_color, smokes, drinks, religion, children, living, education, job FROM users WHERE username='$user'");
    $get_row = mysql_fetch_assoc($get_info);

    $db_relationship =$get_row['relationship'];
    $db_height =$get_row['height'];
    $db_body_type =$get_row['body_type'];
    $db_hair_color =$get_row['hair_color'];
    $db_eye_color =$get_row['eye_color'];
    $db_smokes =$get_row['smokes'];
    $db_drinks =$get_row['drinks'];
    $db_religion =$get_row['religion'];
    $db_children =$get_row['children'];
    $db_living =$get_row['living'];
    $db_education =$get_row['education'];
    $db_job =$get_row['job'];
 ?>
<?php
if ($updateinfo) {
    //variables for submitted data
    $relationship = @$_POST['relationship'];
    $height = @$_POST['height'];
    $body_type = @$_POST['body_type'];
    $hair_color = @$_POST['hair_color'];
    $eye_color = @$_POST['eye_color'];
    $smokes = @$_POST['smokes'];
    $drinks = @$_POST['drinks'];
    $religion = @$_POST['religion'];
    $children = @$_POST['children'];
    $living = @$_POST['living'];
    $education = @$_POST['education'];
    $job = @$_POST['job'];

    //submit details to database
   //Guys this is where my problem is should I INSERT INTO 'users' or   UPDATE 'users'    
 $info_submit_query = mysql_query("UPDATE users SET relationship='$relationship', height='$height', body_type='$body_type', hair_color='$hair_color', eye_color='$eye_color', smokes='$smokes', drinks='$drinks', religion='$religion', children='$children', living='$living', education='$education', job='$job'");
echo "<div class='alert alert-success'>Your profile information has been updated</div>";
    }
    else
    {
        //do nothing
    }
?>
<form action="" method="POST">
<div class="form-group">
<label>Relationship</label>
<div class="row">
<div class="col-xs-5">
<select name="relationship"  class="form-control">
<option value="-1">-- Choose --</option>
<option value="16">Single</option>
<option value="17">In a relaitionship</option>
<option value="18">Married</option>
<option value="151">In relationship, but I'm open minded</option>
<option value="152">I'm married</option>
<option value="153">Open for relationship</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Height (cm)</label>
<div class="row">
<div class="col-xs-5">
<select name="height" class="form-control">
<option value="-1">-- Choose --</option>
<option value="45">130</option>
<option value="60">131</option>
<option value="61">132</option>
<option value="62">133</option>
<option value="63">134</option>
<option value="64">135</option>
<option value="65">136</option>
<option value="66">137</option>
<option value="67">138</option>
<option value="68">139</option>
<option value="69">140</option>
<option value="70">141</option>
<option value="71">142</option>
<option value="72">143</option>
<option value="73">144</option>
<option value="74">145</option>
<option value="75">146</option>
<option value="76">147</option>
<option value="77">148</option>
<option value="78">149</option>
<option value="79">150</option>
<option value="80">151</option>
<option value="81">152</option>
<option value="82">153</option>
<option value="83">154</option>
<option value="84">155</option>
<option value="85">156</option>
<option value="86">157</option
option value="87">158</option>
<option value="88">159</option>
<option value="89">160</option>
<option value="90">161</option>
<option value="91">162</option>
<option value="92">163</option>
<option value="93">164</option>
<option value="94">165</option>
<option value="95">166</option>
<option value="96">167</option>
<option value="97">168</option>
<option value="98">169</option>
<option value="99">170</option>
<option value="100">171</option>
<option value="101">172</option>
<option value="102">173</option>
<option value="103">174</option>
<option value="104">175</option>
<option value="105">176</option>
<option value="106">177</option>
<option value="107">178</option>
<option value="108">179</option>
<option value="109">180</option>
<option value="110">181</option>
<option value="111">182</option>
<option value="112">183</option>
<option value="113">184</option>
<option value="114">185</option>
<option value="115">186</option>
<option value="116">187</option>
<option value="117">188</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Body Type</label>
<div class="row">
<div class="col-xs-5">
<select name="body_type" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Hair Color</label>
<div class="row">
<div class="col-xs-5">
<select name="hair_color" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Eye Color</label>
<div class="row">
<div class="col-xs-5">
<select name="eye_color" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Smokes</label>
<div class="row">
<div class="col-xs-5">
<select name="smokes" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">No</option>
<option value="33">Yes</option>
<option value="164">Trying to quite</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Drinks</label>
<div class="row">
<div class="col-xs-5">
<select name="drinks" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Relegion</label>
<div class="row">
<div class="col-xs-5">
<select name="religion" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Children</label>
<div class="row">
<div class="col-xs-5">
<select name="children" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Living</label>
<div class="row">
<div class="col-xs-5">
<select name="living" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Education</label>
<div class="row">
<div class="col-xs-5">
<select name="education" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label>Job</label>
<div class="row">
<div class="col-xs-5">
<select name="job" class="form-control">
<option value="-1">-- Choose --</option>
<option value="30">Alone</option>
<option value="33">With partner</option>
<option value="164">With roomate</option>
<option value="165">With parents</option>
<option value="166">Other</option>
</select>
</div>
</div>
</div>
<input type="submit" class="btn btn-primary" name="updateinfo" value="Submit">
</form>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 4
    **[DANGER! You need to prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)** **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 28 '15 at 16:58
  • 1
    It's surprising the amount of people who are starting and use `mysql_*` functions. I wonder if the problem is that the institutions are not updating their learning materials and teaching old (and obsolete) theory – Alvaro Montoro Jan 28 '15 at 17:02
  • 1
    By the way, another big issue: your `update` statement is missing a `where` clause, so you'll update all the user records with the new information. – Alvaro Montoro Jan 28 '15 at 17:04
  • 1
    Aside: your question is being heavily downvoted because it seems that you want someone to finish your work for you (even though that interpretation may be just down to how you have phrased the question). On Stack Overflow, requests for free work tend not to be received well - I recommend you seek help instead, emphasising your willingness to do the necessary implementation based on any assistance given. – halfer Jan 28 '15 at 17:22

1 Answers1

0

After fixing all the issues that Jay Blanchard told you (they should be your top priority), and the comment that I made myself (about cleaning your SQL), you can try doing this:

INSERT INTO users ( column_name1, column_name2, .... )
VALUES ( value1, value2, .... )
ON DUPLICATE KEY 
UPDATE column_name1 = value1, column_name2 = value2, ....

This will insert the new values into the database and, if the record already exists, it will update it. You can find more information on the MySQL web site.

Alvaro Montoro
  • 28,081
  • 7
  • 57
  • 86