请帮我完成这个PHP代码

时间:2015-01-28 16:56:15

标签: php html

我有一个名为editdetails.php的页面,在该页面上,用户可以通过选择宗教,教育,爱好等选项来编辑他们的详细信息。我已经完成了HTML,但我怎么能INSERT/UPDATE进入数据库名为users,其中包含用户详细信息,例如usernamepasswordprofile_picabout_me

我希望将详细信息更新到users表中,并且我希望在选择选项后在其配置文件中回显它。这是代码:

<?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>

1 个答案:

答案 0 :(得分:0)

在解决了Jay Blanchard告诉你的所有问题(他们应该是你的首要任务),以及我自己做的评论(关于清理你的SQL)之后,你可以尝试这样做: / p>

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

这会将新值插入数据库,如果记录已存在,则会更新它。您可以找到more information on the MySQL web site